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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
typedef string::iterator State;
bool isdigit(State &pos);
int number(State &pos);
int factor(State &pos);
int expression(State &pos);
int term(State &pos);

bool isdigit(State &pos) {
  if ('0' <= *pos && *pos <= '9')
    return true;
  else
    return false;
}
int number(State &pos) {
  int res = 0;
  while (isdigit(pos)) {
    res *= 10;
    res += (*pos) - '0';
    pos++;
  }
  return res;
}
int term(State &pos) {
  int ret = factor(pos);
  while (*pos == '*' || *pos == '/') {
    bool mul = (*pos == '*');
    pos++;
    int k = factor(pos);
    if (mul)
      ret *= k;
    else
      ret /= k;
  }
  return ret;
}
int factor(State &pos) {
  int ret;
  if (*pos == '(') {
    pos++;
    ret = expression(pos);
    pos++;
  } else
    ret = number(pos);
  return ret;
}
int expression(State &pos) {
  int ret = term(pos);
  while (*pos == '+' || *pos == '-') {
    bool plus = (*pos == '+');
    pos++;
    int k = term(pos);
    if (plus)
      ret += k;
    else
      ret -= k;
  }
  return ret;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    State pos = s.begin();
    cout << expression(pos) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
vector<char> buf;
stack<char> tmp;
char t;
void add_buf(char x) {
  buf.pb(x);
  return;
}
void add_tmp(char x) {
  tmp.push(x);
  return;
}
void move() {
  char x = tmp.top();
  tmp.pop();
  buf.pb(x);
  return;
}
void reset() {
  while (1) {
    char x = tmp.top();
    if (x == '(') {
      tmp.pop();
      break;
    }
    buf.pb(x);
    tmp.pop();
  }
  return;
}
int priority(char x) {
  if (x == '+' || x == '-')
    return 0;
  if (x == '*' || x == '/')
    return 1;
  if (x == ')')
    return 2;
  if (x == '(')
    return 3;
  return 4;
}
void end() {
  while (!tmp.empty()) {
    char x = tmp.top();
    tmp.pop();
    buf.pb(x);
  }
  return;
}
bool is_num(char x) { return priority(x) == 4; }
int calc() {
  stack<char> c;
  for (int i = 0; i < buf.size(); i++) {
    if (is_num(buf[i])) {
      c.push(buf[i] - '0');
    } else {
      int a = c.top();
      c.pop();
      int b = c.top();
      c.pop();
      cout << a << ' ' << b << endl;
      switch (buf[i]) {
      case '+':
        c.push(b + a);
        break;
      case '-':
        c.push(b - a);
        break;
      case '*':
        c.push(a * b);
        break;
      case '/':
        c.push(b / a);
        break;
      }
    }
  }
  return c.top();
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    buf.clear();
    while (!tmp.empty()) {
      tmp.pop();
    }
    char s[101];
    scanf("%s", s);
    for (int i = 0; i < strlen(s) - 1; i++) {
      if (is_num(s[i])) {
        add_buf(s[i]);
        continue;
      }
      if (priority(s[i]) == 2) {
        reset();
        continue;
      }
      if (priority(s[i]) == 3) {
        add_tmp(s[i]);
        continue;
      }
      while (!tmp.empty()) {
        if (priority(tmp.top()) == 1 && priority(s[i]) == 0) {
          move();
        } else
          break;
      }
      add_tmp(s[i]);
    }
    end();
    for (int i = 0; i < buf.size(); i++) {
      cout << buf[i] << ' ';
    }
    cout << endl;
    cout << calc() << endl;
  }
  return 0;
}