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

#include <bits/stdc++.h>
#define int long long
#define range(i, x, y)                                                         \\
  i = (x);                                                                     \\
  i < (y);                                                                     \\
  i++
#define rep(i, n) for (int i = 0, i##_len = n; i < i##_len; i++)
#define all(x) begin(x), end(x)
#define lim(x, r, l) (r <= x && x < l)
typedef long long ll;
typedef long double ld;
using namespace std;

int c = -1;
string s;

/*
<eq> = <expr> '='
<expr> = <expr2> (('+'|'-') <expr2>)*
<expr2> = <expr3> (('*'|'/') <expr3>)*
<expr3> = <number> | '(' <expr> ')'
<number> = ['0' - '9']+
*/

char seek() {
  assert(c < s.size());
  return s[c];
}
char next() {
  assert(c < s.size());
  char res = s[c];
  c++;
  return res;
}
int number() {
  int res = 0;
  while (isdigit(seek())) {
    res *= 10;
    res += next() - '0';
  }
  return res;
}
int expr();

int expr3() {
  char p = seek();
  if (p == '(') {
    next();
    int res = expr();
    assert(next() == ')');
    return res;
  }
  return number();
}
int expr2() {
  int res = expr3();
  while (true) {
    char op = seek();
    if (op == '*') {
      next();
      int other = expr3();
      res *= other;
    } else if (op == '/') {
      next();
      int other = expr3();
      res /= other;
    } else {
      return res;
    }
  }
}
int expr() {
  int res = expr2();
  while (true) {
    char op = seek();
    if (op == '+') {
      next();
      int other = expr2();
      res += other;
    } else if (op == '-') {
      next();
      int other = expr2();
      res -= other;
    } else {
      return res;
    }
  }
}

int eq() {
  int e = expr();
  char eqa = next();
  assert(eqa == '=');
  return e;
}
int solve(string str) {
  c = 0;
  s = str;
  return eq();
}
signed main() {
  int n;
  cin >> n;
  rep(i, n) {
    string str;
    cin >> str;
    cout << solve(str) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define range(i, x, y)                                                         \\
  i = (x);                                                                     \\
  i < (y);                                                                     \\
  i++
#define rep(i, n) for (int i = 0, i##_len = n; i < i##_len; i++)
#define all(x) begin(x), end(x)
#define lim(x, r, l) (r <= x && x < l)
typedef long long ll;
typedef long double ld;
using namespace std;

int c = -1;
string s;

/*
<eq> = <expr> '='
<expr> = <expr2> (('+'|'-') <expr2>)*
<expr2> = <expr3> (('*'|'/') <expr3>)*
<expr3> = <number> | '(' <expr> ')'
<number> = ['0' - '9']+
*/

char seek() {
  assert(c < s.size());
  return s[c];
}
char next() {
  assert(c < s.size());
  char res = s[c];
  c++;
  return res;
}
int number() {
  int res = 0;
  if (isdigit(seek())) {
    res *= 10;
    res += next() - '0';
  }
  return res;
}
int expr();

int expr3() {
  char p = seek();
  if (p == '(') {
    next();
    int res = expr();
    assert(next() == ')');
    return res;
  }
  return number();
}
int expr2() {
  int res = expr3();
  while (true) {
    char op = seek();
    if (op == '*') {
      next();
      int other = expr2();
      res *= other;
    } else if (op == '/') {
      next();
      int other = expr2();
      res /= other;
    } else {
      return res;
    }
  }
}
int expr() {
  int res = expr2();
  while (true) {
    char op = seek();
    if (op == '+') {
      next();
      int other = expr2();
      res += other;
    } else if (op == '-') {
      next();
      int other = expr2();
      res -= other;
    } else {
      return res;
    }
  }
}

int eq() {
  int e = expr();
  char eqa = next();
  assert(eqa == '=');
  return e;
}
int solve(string str) {
  c = 0;
  s = str;
  return eq();
}
signed main() {
  int n;
  cin >> n;
  rep(i, n) {
    string str;
    cin >> str;
    cout << solve(str) << endl;
  }
}