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


#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

std::string S;
size_t i = 0;
int32_t func();
int32_t getvalue() {
  bool minus = false;
  if (S[i] == '-') {
    minus = true;
    ++i;
  }
  int32_t nowv = 0;
  if (S[i] == '(') {
    ++i;
    nowv = func();
  } else {
    while ('0' <= S[i] && S[i] <= '9') {
      nowv *= 10;
      nowv += S[i] - '0';
      ++i;
    }
  }
  return nowv * (minus ? -1 : 1);
}
int32_t func() {
  int32_t tmpv = 0;
  int32_t tmpmulv = getvalue();
  while (S[i] != ')') {
    char ope = S[i];
    ++i;
    int32_t nowv = getvalue();
    if (ope == '+') {
      tmpv += tmpmulv;
      tmpmulv = nowv;
    } else if (ope == '-') {
      tmpv += tmpmulv;
      tmpmulv = -nowv;
    } else if (ope == '*') {
      tmpmulv *= nowv;
    } else if (ope == '/') {
      tmpmulv /= nowv;
    }
  }
  ++i;
  tmpv += tmpmulv;
  tmpmulv = 1;
  return tmpv;
}

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  int32_t Q;
  in >> Q;
  while (Q--) {
    in >> S;
    S.pop_back();
    S.push_back(')');
    i = 0;
    out << func() << endl;
  }

  return 0;
}
#endif


Error: 0

Fixed submission:


#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

std::string S;
size_t i = 0;
int32_t func() {
  int32_t tmpv = 0;
  int32_t tmpmulv;
  if (S[i] == '(') {
    ++i;
    tmpmulv = func();
    ++i;
  } else {
    int32_t nowv = 0;
    while ('0' <= S[i] && S[i] <= '9') {
      nowv *= 10;
      nowv += S[i] - '0';
      ++i;
    }
    tmpmulv = nowv;
  }
  while (S[i] != ')') {
    char ope = S[i];
    ++i;
    int32_t nowv = 0;
    if (S[i] == '(') {
      ++i;
      nowv = func();
    } else {
      while ('0' <= S[i] && S[i] <= '9') {
        nowv *= 10;
        nowv += S[i] - '0';
        ++i;
      }
    }
    if (ope == '+') {
      tmpv += tmpmulv;
      tmpmulv = nowv;
    } else if (ope == '-') {
      tmpv += tmpmulv;
      tmpmulv = -nowv;
    } else if (ope == '*') {
      tmpmulv *= nowv;
    } else if (ope == '/') {
      tmpmulv /= nowv;
    }
  }
  ++i;
  tmpv += tmpmulv;
  tmpmulv = 1;
  return tmpv;
}

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  int32_t Q;
  in >> Q;
  while (Q--) {
    in >> S;
    S.pop_back();
    S.push_back(')');
    i = 0;
    out << func() << endl;
  }

  return 0;
}
#endif