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

#include <bits/stdc++.h>
#define int long long
#define N 100010
#define S str[pos]
#define Sp str[pos++]
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
int Max(int &a, int b) { return a = max(a, b); }
int Min(int &a, int b) { return a = min(a, b); }
typedef complex<double> P;
typedef pair<P, P> L;
string str;
int pos;

double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
P proj(P p, L l) {
  return l.first + dot(p - l.first, l.second - l.first) /
                       norm(l.second - l.first) * (l.second - l.first);
}
P reflect(P p, L l) { return p + (proj(p, l) - p) * 2.0; }

P crossPoint(L l, L m) {
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if (fabs(A) < EPS && fabs(B) < EPS)
    return m.first;
  else if (fabs(A) >= EPS)
    return m.first + B / A * (m.second - m.first);
}

bool isPoint(L a) { return a.first == a.second; }

double getNumber() {
  assert(S == '-' || isdigit(S));
  double res = 0, f = 1;
  if (S == '-')
    pos++, f = -1;
  while (isdigit(S))
    res = res * 10 + Sp - '0';
  return f * res;
}

P getPoint() {
  assert(Sp == '(' && (isdigit(S) || S == '-'));
  double real = getNumber();
  assert(Sp == ',');
  double imag = getNumber();
  assert(Sp == ')');
  return P(real, imag);
}

L expression();

L calc() {
  P a;
  L res;
  if (S == '(' && (isdigit(str[pos + 1]) || str[pos + 1] == '-'))
    a = getPoint(), res = L(a, a);
  else if (S == '(' && str[pos + 1] == '(')
    pos++, res = expression(), pos++;
  else
    assert(!"calc error");
  return res;
}

L calc(L a, L b) {
  if (isPoint(a) && isPoint(b))
    return L(a.first, b.first);
  if (!isPoint(a) && !isPoint(b))
    return L(crossPoint(a, b), crossPoint(a, b));
  if (isPoint(a))
    return L(reflect(a.first, b), reflect(a.first, b));
  if (isPoint(b))
    return L(reflect(b.first, a), reflect(b.first, a));
  assert(!"@ calc error");
}

L expression() {
  L res = calc();
  while (pos < (int)str.size()) {
    if (S == '@')
      pos++, res = calc(res, calc());
    else if (S == ')')
      break;
    else
      assert(!"expression error S");
  }
  return res;
}

signed main() {
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    pos = 0;
    L ans = expression();
    printf("%f %f\\n", ans.first.real(), ans.first.imag());
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define N 100010
#define S str[pos]
#define Sp str[pos++]
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
int Max(int &a, int b) { return a = max(a, b); }
int Min(int &a, int b) { return a = min(a, b); }
typedef complex<double> P;
typedef pair<P, P> L;
string str;
int pos;

double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
P proj(P p, L l) {
  return l.first + dot(p - l.first, l.second - l.first) /
                       norm(l.second - l.first) * (l.second - l.first);
}
P reflect(P p, L l) { return p + (proj(p, l) - p) * 2.0; }

P crossPoint(L l, L m) {
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if (fabs(A) < EPS && fabs(B) < EPS)
    return m.first;
  else if (fabs(A) >= EPS)
    return m.first + B / A * (m.second - m.first);
}

bool isPoint(L a) { return a.first == a.second; }

double getNumber() {
  assert(S == '-' || isdigit(S));
  double res = 0, f = 1;
  if (S == '-')
    pos++, f = -1;
  while (isdigit(S))
    res = res * 10 + Sp - '0';
  return f * res;
}

P getPoint() {
  assert(Sp == '(' && (isdigit(S) || S == '-'));
  double real = getNumber();
  assert(Sp == ',');
  double imag = getNumber();
  assert(Sp == ')');
  return P(real, imag);
}

L expression();

L calc() {
  P a;
  L res;
  if (S == '(' && isdigit(str[pos + 1]))
    a = getPoint(), res = L(a, a);
  else if (S == '(' && str[pos + 1] == '(')
    pos++, res = expression(), pos++;
  else
    assert(!"calc error");
  return res;
}

L calc(L a, L b) {
  if (isPoint(a) && isPoint(b))
    return L(a.first, b.first);
  if (!isPoint(a) && !isPoint(b))
    return L(crossPoint(a, b), crossPoint(a, b));
  if (isPoint(a))
    return L(reflect(a.first, b), reflect(a.first, b));
  if (isPoint(b))
    return L(reflect(b.first, a), reflect(b.first, a));
  assert(!"calc error");
}

L expression() {
  L res = calc();
  while (pos < (int)str.size()) {
    if (S == '@')
      pos++, res = calc(res, calc());
    else if (S == ')')
      break;
    else
      assert(!"expression error S");
  }
  return res;
}

signed main() {
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    pos = 0;
    L ans = expression();
    printf("%f %f\\n", ans.first.real(), ans.first.imag());
  }
  return 0;
}