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


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <bitset>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c) __typeof__((c).begin())
#define FOREACH(it, c) for (ITER(c) it = (c).begin(); it != (c).end(); ++it)
#define RITER(c) __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it = (c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n) REPEAT(i, 0, n)
#define RREP(i, n) RREPEAT(i, 0, n)
#define REPEAT(i, k, n) for (int i = (k); i < (k + n); ++i)
#define RREPEAT(i, k, n) for (int i = (k) + (n)-1; i >= (k); --i)
#define FROMTO(i, f, t) for (int i = f; i < t; i++)
#define ALL(c) (c).begin(), (c).end()
#define LLPOW(p1, p2) ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A) (sizeof(A) / sizeof((A)[0]))
#define CIN_NO_SYNC                                                            \\
  do {                                                                         \\
    cin.tie(0);                                                                \\
    ios_base::sync_with_stdio(false);                                          \\
  } while (0)
#define GETSTR(p) fgets((p), sizeof(p), stdin)
#define CHOMP(p)                                                               \\
  do {                                                                         \\
    char *_q = (p) + strlen(p) - 1;                                            \\
    if (*_q == '\\n' || *_q == '\\r')                                            \\
      *_q = 0;                                                                 \\
  } while (0)
#define FILL(m, v) memset(m, v, sizeof(m))
#define mp make_pair
#define pb push_back
template <class _T> _T MAX(_T p1, _T p2) { return (p1 > p2) ? p1 : p2; }
template <class _T> _T MIN(_T p1, _T p2) { return (p1 < p2) ? p1 : p2; }
template <class _Tv> inline string join(_Tv &v, string sep = " ") {
  ostringstream s;
  FOREACH(it, v) {
    if (it != v.begin())
      s << sep;
    s << *it;
  };
  return s.str();
}

typedef long long ll;
typedef unsigned long long ull;

#define X real()
#define Y imag()
typedef double D;
typedef complex<D> P;
#define EPS (1e-9)

#ifdef _DEBUG
template <class _Tv>
inline void _prfe(const char *_n, _Tv _c, bool _p = false) {
  ITER(_c) _it = _c.begin();
  if (_p) {
    cout << _n << " = [" << endl;
    for (; _it != _c.end(); ++_it)
      cout << "    " << *_it << endl;
    cout << "]" << endl;
  } else {
    cout << _n << " = [ " << *_it++;
    for (; _it != _c.end(); ++_it)
      cout << ", " << *_it;
    cout << " ]" << endl;
  }
}
#define pf printf
#define pr(n)                                                                  \\
  do {                                                                         \\
    cout << #n " = " << (n) << endl;                                           \\
  } while (0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n)                                                              \\
  do {                                                                         \\
    if (n)                                                                     \\
      throw;                                                                   \\
  } while (0)
#else
#define pf(...)     /* */
#define pr(...)     /* */
#define prfe(...)   /* */
#define prfep(...)  /* */
#define dbgchk(...) /* */
#endif

template <class _Tt> class calculate {
public:
  enum op { NUM, ADD, SUB, MUL, DIV, LPA, RPA };

private:
  typedef pair<op, _Tt> pt;
  deque<pt> mq;
  void popf(deque<pt> &q, op &type, _Tt &num) {
    type = q.front().first;
    num = q.front().second;
    q.pop_front();
  }
  void popb(deque<pt> &q, op &type, _Tt &num) {
    type = q.back().first;
    num = q.back().second;
    q.pop_back();
  }
  _Tt cal2(deque<pt> &q) {
    op sop, dop;
    _Tt snum, dnum, num = 0;
    while (!q.empty()) {
      popf(q, sop, snum);
      switch (sop) {
      case NUM:
        num = snum;
        break;
      case ADD:
      case SUB:
        popf(q, dop, dnum);
        dbgchk(dop != NUM);
        num = (sop == ADD) ? num + dnum : num - dnum;
        break;
      default:
        throw;
      }
    }
    return num;
  }
  _Tt cal(deque<pt> &q) {
    deque<pt> s;
    while (!q.empty()) {
      op qop;
      _Tt num;
      popf(q, qop, num);
      switch (qop) {
      case NUM:
      case ADD:
      case SUB:
        s.pb(mp(qop, num));
        break;
      case LPA:
        s.pb(mp(NUM, cal(q)));
        break;
      case RPA:
        return cal2(s);
        break;
      case MUL:
      case DIV:
        op sop, dop;
        _Tt snum, dnum;
        popb(s, sop, snum);
        dbgchk(sop != NUM);
        popf(q, dop, dnum);
        dbgchk(dop != NUM && dop != LPA);
        if (dop == LPA)
          dnum = cal(q);
        _Tt r = (qop == MUL) ? snum * dnum : snum / dnum;
        s.pb(mp(NUM, r));
        break;
      }
    }
    return cal2(s);
  }

public:
  void add(_Tt n) { mq.pb(mp(NUM, n)); }
  void add(op opr) { mq.pb(mp(opr, 0)); }
  _Tt result() { return cal(mq); }
};

void solve(string s) {
  int n = s.length();
  calculate<int> calc;
  int num = 0;
  bool f = false;
  REP(i, n) {
    if (isdigit(s[i])) {
      num = num * 10 + (s[i] - '0');
      f = true;
      continue;
    }
    if (f)
      calc.add(num);
    if (s[i] == '=')
      break;
    switch (s[i]) {
    case '+':
      calc.add(calc.ADD);
      break;
    case '-':
      calc.add(calc.SUB);
      break;
    case '*':
      calc.add(calc.MUL);
      break;
    case '/':
      calc.add(calc.DIV);
      break;
    case '(':
      calc.add(calc.LPA);
      break;
    case ')':
      calc.add(calc.RPA);
      break;
    }
    num = 0;
    f = false;
  }
  cout << calc.result() << endl;
}

int main() {
  int n;
  string s;
  getline(cin, s);
  istringstream ss(s);
  ss >> n;
  REP(i, n) {
    getline(cin, s);
    solve(s);
  }
  return 0;
}

Error: 0

Fixed submission:


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <bitset>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c) __typeof__((c).begin())
#define FOREACH(it, c) for (ITER(c) it = (c).begin(); it != (c).end(); ++it)
#define RITER(c) __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it = (c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n) REPEAT(i, 0, n)
#define RREP(i, n) RREPEAT(i, 0, n)
#define REPEAT(i, k, n) for (int i = (k); i < (k + n); ++i)
#define RREPEAT(i, k, n) for (int i = (k) + (n)-1; i >= (k); --i)
#define FROMTO(i, f, t) for (int i = f; i < t; i++)
#define ALL(c) (c).begin(), (c).end()
#define LLPOW(p1, p2) ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A) (sizeof(A) / sizeof((A)[0]))
#define CIN_NO_SYNC                                                            \\
  do {                                                                         \\
    cin.tie(0);                                                                \\
    ios_base::sync_with_stdio(false);                                          \\
  } while (0)
#define GETSTR(p) fgets((p), sizeof(p), stdin)
#define CHOMP(p)                                                               \\
  do {                                                                         \\
    char *_q = (p) + strlen(p) - 1;                                            \\
    if (*_q == '\\n' || *_q == '\\r')                                            \\
      *_q = 0;                                                                 \\
  } while (0)
#define FILL(m, v) memset(m, v, sizeof(m))
#define mp make_pair
#define pb push_back
template <class _T> _T MAX(_T p1, _T p2) { return (p1 > p2) ? p1 : p2; }
template <class _T> _T MIN(_T p1, _T p2) { return (p1 < p2) ? p1 : p2; }
template <class _Tv> inline string join(_Tv &v, string sep = " ") {
  ostringstream s;
  FOREACH(it, v) {
    if (it != v.begin())
      s << sep;
    s << *it;
  };
  return s.str();
}

typedef long long ll;
typedef unsigned long long ull;

#define X real()
#define Y imag()
typedef double D;
typedef complex<D> P;
#define EPS (1e-9)

#ifdef _DEBUG
template <class _Tv>
inline void _prfe(const char *_n, _Tv _c, bool _p = false) {
  ITER(_c) _it = _c.begin();
  if (_p) {
    cout << _n << " = [" << endl;
    for (; _it != _c.end(); ++_it)
      cout << "    " << *_it << endl;
    cout << "]" << endl;
  } else {
    cout << _n << " = [ " << *_it++;
    for (; _it != _c.end(); ++_it)
      cout << ", " << *_it;
    cout << " ]" << endl;
  }
}
#define pf printf
#define pr(n)                                                                  \\
  do {                                                                         \\
    cout << #n " = " << (n) << endl;                                           \\
  } while (0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n)                                                              \\
  do {                                                                         \\
    if (n)                                                                     \\
      throw;                                                                   \\
  } while (0)
#else
#define pf(...)     /* */
#define pr(...)     /* */
#define prfe(...)   /* */
#define prfep(...)  /* */
#define dbgchk(...) /* */
#endif

template <class _Tt> class calculate {
public:
  enum op { NUM, ADD, SUB, MUL, DIV, LPA, RPA };

private:
  typedef pair<op, _Tt> pt;
  deque<pt> mq;
  void popf(deque<pt> &q, op &type, _Tt &num) {
    type = q.front().first;
    num = q.front().second;
    q.pop_front();
  }
  void popb(deque<pt> &q, op &type, _Tt &num) {
    type = q.back().first;
    num = q.back().second;
    q.pop_back();
  }
  _Tt cal2(deque<pt> &q) {
    op sop, qop, dop;
    _Tt snum, dnum;
    popf(q, sop, snum);
    dbgchk(sop != 0);
    while (!q.empty()) {
      popf(q, qop, dnum);
      dbgchk(qop != 1 && qop != 2);
      popf(q, dop, dnum);
      dbgchk(dop != 0);
      snum = (qop == 1) ? snum + dnum : snum - dnum;
    }
    return snum;
  }
  _Tt cal(deque<pt> &q) {
    deque<pt> s;
    while (!q.empty()) {
      op qop;
      _Tt num;
      popf(q, qop, num);
      switch (qop) {
      case NUM:
      case ADD:
      case SUB:
        s.pb(mp(qop, num));
        break;
      case LPA:
        s.pb(mp(NUM, cal(q)));
        break;
      case RPA:
        return cal2(s);
        break;
      case MUL:
      case DIV:
        op sop, dop;
        _Tt snum, dnum;
        popb(s, sop, snum);
        dbgchk(sop != 0);
        popf(q, dop, dnum);
        dbgchk(dop != NUM && dop != LPA);
        if (dop == LPA)
          dnum = cal(q);
        _Tt r = (qop == MUL) ? snum * dnum : snum / dnum;
        s.pb(mp(NUM, r));
        break;
      }
    }
    return cal2(s);
  }

public:
  void add(_Tt n) { mq.pb(mp(NUM, n)); }
  void add(op opr) { mq.pb(mp(opr, 0)); }
  _Tt result() { return cal(mq); }
};

void solve(string s) {
  int n = s.length();
  calculate<int> calc;
  REP(i, n) {
    if (isdigit(s[i])) {
      calc.add(atoi(s.c_str() + i));
      continue;
    }
    if (s[i] == '=')
      break;
    switch (s[i]) {
    case '+':
      calc.add(calc.ADD);
      break;
    case '-':
      calc.add(calc.SUB);
      break;
    case '*':
      calc.add(calc.MUL);
      break;
    case '/':
      calc.add(calc.DIV);
      break;
    case '(':
      calc.add(calc.LPA);
      break;
    case ')':
      calc.add(calc.RPA);
      break;
    }
  }
  cout << calc.result() << endl;
}

int main() {
  int n;
  string s;
  getline(cin, s);
  istringstream ss(s);
  ss >> n;
  REP(i, n) {
    getline(cin, s);
    solve(s);
  }
  return 0;
}