Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)
typedef string::const_iterator State;

string s;

int eval(State &beg);

int number(State &beg) {
  if (*beg == '(') {
    beg++;
    int ret = eval(beg);
    beg++;
    return ret;
  }

  string ret = "";
  while (1) {
    if (isdigit(*beg)) {
      ret += *beg;
      beg++;
    } else
      break;
  }

  return stoi(ret);
}

int term(State &beg) {
  int ret = number(beg);

  while (1) {
    if (*beg == '*') {
      beg++;
      ret *= number(beg);
    } else if (*beg == '/') {
      beg++;
      ret /= number(beg);
    } else
      break;
  }

  return ret;
}

int eval(State &beg) {
  int ret = term(beg);

  while (*beg != '=' && *beg != ')') {
    beg++;
    if (*(beg - 1) == '+')
      ret += term(beg);
    else if (*(beg - 1) == '-')
      ret -= term(beg);
    else
      throw("error");
  }
  return ret;
}
int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> s;

    State beg = s.begin();
    try {
      cout << eval(beg) << endl;
    } catch (string s) {
      cout << s << endl;
    }
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

string toRPN(string s) {
  stack<char> ope;
  string ret;

  rep(i, s.size()) {
    if (isdigit(s[i]))
      ret += s[i];

    else {
      //			switch(s[i]){
      //				case '(' :
      //					ope.push(s[i]);
      //					break;
      //				case ')' :
      //					while(ope.top()!='('){
      //						ret+=ope.top();
      //						ope.pop();
      //					}
      //					ope.pop();
      //					break;
      //				case '*' :
      //				case '/' :
      //					while( !ope.empty() &&
      //(ope.top()=='*' || ope.top()=='/') ){ 						ret+=ope.top(); 						ope.pop();
      //					}
      //					ope.push(s[i]);
      //					break;
      //				case '+' :
      //				case '-' :
      //					while( !ope.empty() &&
      //(ope.top()=='*' || ope.top()=='/' || ope.top()=='+' || ope.top()=='-')
      //){ 						ret+=ope.top(); 						ope.pop();
      //					}
      //					ope.push(s[i]);
      //					break;
      //				default :
      //					return "invalid";
      //					break;
      //			}
      if (s[i] == '(') {
        ope.push(s[i]);

      } else if (s[i] == ')') {
        while (ope.top() != '(') {
          ret += ope.top();
          ope.pop();
        }
        ope.pop();

      } else if (s[i] == '*' || s[i] == '/') {
        while (!ope.empty() && (ope.top() == '*' || ope.top() == '/')) {
          ret += ope.top();
          ope.pop();
        }
        ope.push(s[i]);

      } else if (s[i] == '+' || s[i] == '-') {
        while (!ope.empty() && (ope.top() == '*' || ope.top() == '/' ||
                                ope.top() == '+' || ope.top() == '-')) {
          ret += ope.top();
          ope.pop();
        }
        ope.push(s[i]);

      } else {
        return "invalid";
      }
    }
  }
  while (ope.size()) {
    ret += ope.top();
    ope.pop();
  }

  return ret;
}

string solveRPN(string s) {
  stack<int> stk;

  for (int i = 0; i < s.size(); i++) {
    if (isdigit(s[i]))
      stk.push(s[i] - '0');
    else {
      int b = stk.top();
      stk.pop();
      int a = stk.top();
      stk.pop();

      if (s[i] == '+')
        stk.push(a + b);
      if (s[i] == '-')
        stk.push(a - b);
      if (s[i] == '*')
        stk.push(a * b);
      if (s[i] == '/')
        stk.push(a / b);
    }
  }

  stringstream ret;
  ret << stk.top();

  return ret.str();
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    string s;
    cin >> s;

    s.erase(s.size() - 1);

    s = toRPN(s);
    s = solveRPN(s);

    cout << s << endl;
  }

  return 0;
}