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

#include <cctype>
#include <iostream>
#include <string>

using namespace std;

typedef string::const_iterator State;
class ParseError {};

int expression(State &base);
int number(State &base) {
  int ret = 0;
  while (isdigit(*base)) {
    ret = ret * 10 + (*base - '0');
    base++;
  }
  return ret;
}
int factor(State &base) {
  int ret;
  if (*base == '(') {
    base++;
    ret = expression(base);
    base++;
    return ret;
  } else
    return number(base);
}
int term(State &base) {
  int ret = factor(base);
  while (1) {
    if (*base == '*') {
      base++;
      ret *= factor(base);
    } else if (*base == '/') {
      base++;
      ret /= factor(base);
    } else
      break;
  }
  return ret;
}
int expression(State &base) {
  int ret = term(base);
  while (1) {
    if (*base == '+') {
      base++;
      ret += term(base);
    } else if (*base == '-') {
      base++;
      ret -= term(base);
    } else
      break;
  }
  return ret;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    State base = str.begin();
    cout << expression(base) << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int lp = 0;
int solve(string str) {
  vector<int> op;
  vector<char> ope;
  // cout<<str.length()<<endl;
  cout << "str: " << str << endl;
  int i = 0;
  if (str[0] == '-') {
    i++;
    int g = 0;
    while (str[i] >= '0' && str[i] <= '9') {
      g = g * 10 + str[i] - '0';
      i++;
    }
    op.push_back(g * (-1));
    if (ope.size())
      if (ope[ope.size() - 1] == '*' || ope[ope.size() - 1] == '/') {
        int a = op[op.size() - 1];
        op.pop_back();
        int b = op[op.size() - 1];
        op.pop_back();
        if (ope[ope.size() - 1] == '*')
          op.push_back(a * b);
        else
          op.push_back(b / a);
        ope.pop_back();
      }
    if (str[i])
      ope.push_back(str[i]);
  }
  for (; i < str.length(); i++) {
    // cout<<"KK";
    if (str[i] == '(') {
      string t;
      i++;
      lp++;
      while (lp) {
        t += str[i];
        i++;
        if (str[i] == '(')
          lp++;
        if (str[i] == ')')
          lp--;
      }
      i++;
      op.push_back(solve(t));
      // cout<<"(): "<<ope[ope.size()-1]<<endl;
      if (ope.size())
        if (ope[ope.size() - 1] == '*' || ope[ope.size() - 1] == '/') {
          int a = op[op.size() - 1];
          op.pop_back();
          int b = op[op.size() - 1];
          op.pop_back();
          if (ope[ope.size() - 1] == '*')
            op.push_back(a * b);
          else
            op.push_back(b / a);
          ope.pop_back();
        }
      if (str[i])
        ope.push_back(str[i]);
    } else if (str[i] >= '0' && str[i] <= '9') {
      int g = 0;
      while (str[i] >= '0' && str[i] <= '9') {
        g = g * 10 + str[i] - '0';
        i++;
      }
      op.push_back(g);
      if (ope.size())
        if (ope[ope.size() - 1] == '*' || ope[ope.size() - 1] == '/') {
          int a = op[op.size() - 1];
          op.pop_back();
          int b = op[op.size() - 1];
          op.pop_back();
          if (ope[ope.size() - 1] == '*')
            op.push_back(a * b);
          else
            op.push_back(b / a);
          ope.pop_back();
        }
      if (str[i])
        ope.push_back(str[i]);
    }
  }
  cout << "!" << endl;
  int las = op[op.size() - 1];
  op.pop_back();
  while (ope.size()) {
    cout << "las: " << las << endl;
    char c = ope[ope.size() - 1];
    ope.pop_back();
    if (c == '=')
      continue;
    int pre = op[op.size() - 1];
    cout << "pre: " << pre << endl;
    cout << ope.size() << "   c: " << c << endl;
    op.pop_back();
    if (c == '+')
      las = las + pre;
    else
      las = pre - las;
  }
  cout << "returned : " << las << endl;
  return las;
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    cout << solve(str) << endl;
  }
}