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

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct P {
  int r, i;
  P() {}
  P(int r, int i) : r(r), i(i) {}
  P operator+(P p) { return P(r + p.r, i + p.i); }
  P operator-(P p) { return P(r - p.r, i - p.i); }
  P operator*(P p) { return P(r * p.r - i * p.i, r * p.i + i * p.r); }
};

P expr(string s, int &p);
P factor(string s, int &p);
P term(string s, int &p);
P number(string s, int &p);

bool ov;

void check(P p) {
  ov |= abs(p.r) > 10000;
  ov |= abs(p.i) > 10000;
}

P expr(string s, int &p) {
  // cout<<"e "<<p<<endl;
  P res = factor(s, p);
  check(res);
  while (p < (int)s.size()) {
    // cout<<res.r<<" "<<res.i<<endl;
    check(res);
    if (s[p] == '+') {
      p++;
      P tmp = factor(s, p);
      check(tmp);
      res = res + tmp;
      continue;
    }
    if (s[p] == '-') {
      p++;
      P tmp = factor(s, p);
      check(tmp);
      res = res - tmp;
      continue;
    }
    break;
  }
  check(res);
  return res;
}
P factor(string s, int &p) {
  // cout<<"f "<<p<<endl;
  P res = term(s, p);
  check(res);
  while (p < (int)s.size()) {
    check(res);
    if (s[p] == '*') {
      p++;
      P tmp = term(s, p);
      check(tmp);
      res = res * tmp;
      continue;
    }
    break;
  }
  check(res);
  return res;
}
P term(string s, int &p) {
  // cout<<"t "<<p<<endl;
  P res;
  if (s[p] == '(') {
    p++;
    res = expr(s, p);
    assert(s[p] == ')');
    p++;
  } else
    res = number(s, p);
  check(res);
  return res;
}
P number(string s, int &p) {
  if (s[p] == 'i') {
    p++;
    return P(0, 1);
  }
  int res = 0;
  while (p < (int)s.size() && isdigit(s[p])) {
    res = res * 10 + (s[p++] - '0');
    check(P(res, 0));
  }
  return P(res, 0);
}

signed main() {
  string s;
  while (getline(cin, s)) {
    ov = 0;
    int p = 0;
    P ans = expr(s, p);
    assert(p == (int)s.size());

    if (ov) {
      cout << "overflow" << endl;
      continue;
    }

    if (ans.r == 0 && ans.i == 0) {
      cout << 0 << endl;
      continue;
    }

    if (ans.r != 0)
      cout << ans.r;
    if (ans.r != 0 && ans.i > 0)
      cout << "+";
    if (ans.i != 0)
      cout << ans.i << "i";
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef complex<int> P;

string S;
int overflow;
int p;
P bnf();

bool isOverflow(P a) {
  return abs(a.real()) >= 10000 || abs(a.imag()) >= 10000;
}

P getNum() {
  int res = 0;
  while (isdigit(S[p]))
    res = res * 10 + S[p++] - '0', overflow |= isOverflow(res);

  if (S[p] == '*' && S[p + 1] == 'i') {
    p += 2;
    return P(0, res);
  }

  return P(res, 0);
}

P calc() {
  P num = P(0, 0);

  if (S[p] == '(') {
    p++;
    num = bnf();
    p++;
    return num;
  } else if (isdigit(S[p]))
    num = getNum();
  else
    assert(0 == 1);

  if (S[p] == '*')
    p++, num *= bnf();
  overflow |= isOverflow(num);
  return num;
}

P bnf() {
  P res = calc();
  while (p < S.size()) {
    overflow |= isOverflow(res);
    if (overflow)
      return P(0, 0);

    char ch = S[p];
    if (ch == '(')
      p++, res = calc(), p++;
    else if (ch == '+')
      p++, res += calc();
    else if (ch == '-')
      p++, res -= calc();
    else if (ch == '*')
      p++, res *= calc();
    else if (ch != ')')
      assert(ch == ')');
    else
      break;
  }
  return res;
}

signed main() {
  while (cin >> S) {
    overflow = 0;
    p = 0;

    P ans = bnf();
    if (overflow)
      cout << "overflow" << endl;
    else if (ans.real() == 0 && ans.imag() == 0)
      cout << 0 << endl;
    else if (ans.real() == 0)
      cout << ans.imag() << "i" << endl;
    else if (ans.imag() == 0)
      cout << ans.real() << endl;
    else
      cout << ans.real() << "+" << ans.imag() << "i" << endl;
  }

  return 0;
}