Cod sursa(job #2577832)

Utilizator AlexNeaguAlexandru AlexNeagu Data 9 martie 2020 21:51:18
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
ifstream in("evaluare.in");
ofstream out("evaluare.out");
const int N = 100005;
char c[N];
string s;
stack < int > nums;
stack < char > op;
bool is_op(char ch) {
  if (ch == '+' || ch == '-' || ch == '/' || ch == '*') {
    return true;
  }
  return false;
}
int val(char ch) {
  if (ch == '+' || ch == '-') {
    return 1;
  }
  if (ch == '*' || ch == '/') {
    return 2;
  }
  return -1;
}
int do_op(char op) {
  int a = nums.top();
  nums.pop();
  int b = nums.top();
  nums.pop();
  if (op == '+') {
    b += a;
  }
  if (op == '-') {
    b -= a;
  }
  if (op == '*') {
    b *= a;
  }
  if (op == '/') {
    b /= a;
  }
  nums.push(b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  in >> s;
  for (int i = 0; i < s.size(); i++) {
    c[i + 1] = s[i];
  }
  int n = s.size();
  for (int i = 1; i <= n; i++) {
    if (c[i] == '(') {
      op.push('(');
    }
    else if (c[i] == ')') {
      while(op.top() != '(') {
        char oper = op.top();
        op.pop();
        do_op(oper);
      }
      op.pop();
    }
    else if (is_op(c[i])) {
      while(op.size() && val(c[i]) <= val(op.top())) {
        do_op(op.top());
        op.pop();
      }
      op.push(c[i]);
    }
    else {
      int nr = 0;
      int j = i;
      while(c[j] >= '0' && c[j] <= '9') {
        nr = nr * 10 + (c[j] - '0');
        j++;
      }
      nums.push(nr);
      i = j - 1;
    }
  }
  while(op.size()) {
    do_op(op.top());
    op.pop();
  }
  out << nums.top() << '\n';
  return 0;
}