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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int calc(string s) {
  // ©gªÊÉÍÜêÄ¢éêÆAÅ éÆ«Ì
  if (s[0] >= '0' && s[0] <= '9') {
    bool f = false;
    for (int i = 0; i < s.size(); i++) {
      if (!(s[i] >= '0' && s[i] <= '9')) {
        f = true;
        break;
      }
    }
    if (!f) {
      return atoi(s.c_str());
    }
  } else if (s[0] == '(') {
    stack<char> sc;
    sc.push('(');
    for (int i = 1; i < s.size(); i++) {
      if (s[i] == ')')
        sc.pop();
      else if (s[i] == '(') {
        sc.push('(');
      }
      if (sc.size() == 0) {
        if (i == s.size() - 1) {
          return calc(s.substr(1, s.size() - 2));
        } else {
          break;
        }
      }
    }
  }
  stack<char> sc;
  // \\¶ðÍÆÓ¡ðÍ
  for (int i = s.size() - 1; i >= 0; i--) {
    // ÊÌÌZqÍ³·é
    if (s[i] == '(')
      sc.pop();
    else if (s[i] == ')') {
      sc.push(')');
    } else if (s[i] == '+' && sc.size() == 0) {
      return calc(s.substr(0, i)) + calc(s.substr(i + 1, s.size() - i - 1));
    } else if (s[i] == '-' && sc.size() == 0) {
      return calc(s.substr(0, i)) - calc(s.substr(i + 1, s.size() - i - 1));
    }
  }

  for (int i = s.size() - 1; i >= 0; i--) {
    // ÊÌÌZqÍ³·é
    if (s[i] == '(')
      sc.pop();
    else if (s[i] == ')') {
      sc.push(')');
    } else if (s[i] == '*' && sc.size() == 0) {
      return calc(s.substr(0, i)) * calc(s.substr(i + 1, s.size() - i - 1));
    } else if (s[i] == '/' && sc.size() == 0) {
      return calc(s.substr(0, i)) / calc(s.substr(i + 1, s.size() - i - 1));
    }
  }
}

int main() {

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    cout << calc(str.substr(0, str.size() - 1)) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int getPriority(char ch) {
  switch (ch) {
  case '+':
    return 0;
  case '-':
    return 1;
  case '/':
    return 2;
  case '*':
    return 3;
  default:
    return -1;
  }
}

vector<string> toReversePorland(string str) {

  vector<string> hand;
  vector<string> side;

  for (int i = 0; i < str.size(); i++) {
    if (str[i] == ' ')
      continue;
    else if (str[i] == '=')
      break;
    if (str[i] >= '0' && str[i] <= '9') {
      string num;
      while (i < str.size()) {
        if (str[i] < '0' || str[i] > '9') {
          break;
        }
        num += str[i];
        i++;
      }
      hand.push_back(num);
      i--;
    } else if (str[i] == '(') {
      string tmp;
      tmp += str[i];
      side.push_back(tmp);
    } else if (str[i] == ')') {
      // ツ右ツ環古環づ慊づ可可算ツ子ツづーツ暗ェツづつつクツづつ偲ィツ個ウツづ可姪淞つキ
      for (int j = side.size() - 1;; j--) {
        if (side[j] == "(") {
          if (j != 0) {
            vector<string> tmp;
            for (int k = 0; k < j; k++) {
              tmp.push_back(side[k]);
            }
            side = tmp;
          } else
            side.clear();
          break;
        } else {
          string tmp = side[j];
          hand.push_back(tmp);
        }
      }
    } else {
      if (side.size() == 0) {
        string tmp;
        tmp += str[i];
        side.push_back(tmp);
      } else {
        char cs = side[side.size() - 1][0];
        if ((cs == '*' || cs == '/') && (str[i] == '*' || str[i] == '/')) {
          hand.push_back(side[side.size() - 1]);
          string tmp;
          tmp += str[i];
          side[side.size() - 1] = tmp;
        }
        if ((cs == '+' || cs == '-') && (str[i] == '+' || str[i] == '-')) {
          hand.push_back(side[side.size() - 1]);
          string tmp;
          tmp += str[i];
          side[side.size() - 1] = tmp;
        } else if (cs != '+' && cs != '-' && cs != '*' && cs != '/') {
          string tmp;
          tmp += str[i];
          side.push_back(tmp);
        } else {
          if (getPriority(str[i]) < getPriority(cs)) {
            hand.push_back(side[side.size() - 1]);
            string tmp;
            tmp += str[i];
            side[side.size() - 1] = tmp;
          } else {
            string tmp;
            tmp += str[i];
            side.push_back(tmp);
          }
        }
      }
    }
  }

  for (int i = side.size() - 1; i >= 0; i--) {
    string tmp;
    hand.push_back(side[i]);
  }
  return hand;
}

long long calc(vector<string> str) {
  stack<long long> st;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == "+" || str[i] == "-" || str[i] == "*" || str[i] == "/") {
      long long val[2];
      val[0] = st.top();
      st.pop();
      val[1] = st.top();
      st.pop();
      if (str[i] == "+") {
        st.push(val[0] + val[1]);
      }
      if (str[i] == "-") {
        st.push(val[1] - val[0]);
      }
      if (str[i] == "*") {
        st.push(val[0] * val[1]);
      }
      if (str[i] == "/") {
        st.push(val[1] / val[0]);
      }
    } else {
      st.push(atoi(str[i].c_str()));
    }
  }
  return st.top();
}

int main() {

  int n;
  string s;
  cin >> n;

  vector<string> input;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str;
    input.push_back(str);
  }

  for (int i = 0; i < n; i++) {
    str = input[i];
    // vector<string> vec = toReversePorland(str);
    // for(int i = 0; i < vec.size(); i++)
    //   cout << vec[i] << endl;
    cout << calc(toReversePorland(str)) << endl;
  }

  return 0;
}