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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

string tranceRPN(string in_formula, string out_formula, stack<char> st) {

  if (in_formula.size() == 0) {
    while (!st.empty()) {
      out_formula += st.top();
      st.pop();
    }
    // cout<<st.top()<<endl;;;
    return out_formula; // 処理終了
  }

  char top = in_formula[0];
  in_formula.erase(0, 1);
  // cout<<in_formula<<endl;

  if (top == '=')
    return tranceRPN(in_formula, out_formula, st);

  if (('0' <= top && top <= '9') || top == ' ')
    out_formula += top;
  else if (top == ')') {
    while (1) {
      if (st.top() == '(') {
        st.pop();
        break;
      }
      out_formula += st.top();
      st.pop();
    }
  } else if (top == '(')
    st.push(top);
  else if (st.empty())
    st.push(top);
  else if (((st.top() == '*' || st.top() == '/') &&
            (top == '+' || top == '-')) ||
           ((st.top() == '*' || st.top() == '/') &&
            (top == '*' || top == '/')) ||
           ((st.top() == '+' || st.top() == '-') &&
            (top == '+' || top == '-'))) {
    out_formula += st.top();
    st.pop();
    in_formula = top + in_formula;
  } else {
    st.push(top);
  }
  return tranceRPN(in_formula, out_formula, st);
}

int RPN(string s) {
  int x = 0;
  stack<int> st;
  bool flag = false;
  while (1) {
    if (s.size() == 0) {
      break;
    } else if ('0' <= s[0] && s[0] <= '9') {
      x = x * 10 + s[0] - '0';
      flag = true;
      s.erase(0, 1);
    } else if (s[0] == ' ' && flag) {
      flag = false;
      st.push(x);
      x = 0;
      s.erase(0, 1);
    } else if (s[0] == '+') {
      s.erase(0, 1);
      int y, z;
      y = st.top();
      st.pop();
      z = st.top();
      st.pop();
      st.push(y + z);
    } else if (s[0] == '-') {
      s.erase(0, 1);
      int y, z;
      y = st.top();
      st.pop();
      z = st.top();
      st.pop();
      st.push(z - y);
    } else if (s[0] == '*') {
      s.erase(0, 1);
      int y, z;
      y = st.top();
      st.pop();
      z = st.top();
      st.pop();
      st.push(y * z);
    } else if (s[0] == '/') {
      s.erase(0, 1);
      int y, z;
      y = st.top();
      st.pop();
      z = st.top();
      st.pop();
      st.push(z / y);
    } else {
      s.erase(0, 1);
    }
  }
  return st.top();
}
/*
(1+4)*(3+7)/5


*/

int main() {

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s, rs;
    stack<char> st;
    cin >> s;

    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '+' || s[i] == '*' || s[i] == '-' || s[i] == '/' ||
          s[i] == ')' || s[i] == '(' || s[i] == '=')
        s.insert(i++, " ");
    }
    s += " ";
    // cout<<s<<endl;

    rs = tranceRPN(s, rs, st);
    // cout<<rs<<endl;

    cout << RPN(rs) << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

int main() {
  int n;
  string s;
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> s;
      int j = 0;

      queue<string> qu;
      stack<string> st;

      while (j < s.length()) {
        // cout<<s[j]<<endl;
        // 数値の処理
        if (s[j] >= '0' && s[j] <= '9') {
          string s_temp;

          while (s[j] >= '0' && s[j] <= '9') {
            s_temp += s[j];
            j++;
          }

          qu.push(s_temp);

        }

        // 括弧の処理
        else if (s[j] == ')') {
          while (1) {
            string s_temp;
            s_temp = st.top();
            st.pop();
            if (s_temp == "(") {
              break;
            } else {
              qu.push(s_temp);
            }
          }
          j++;
        }

        else if (s[j] == '(') {
          string s_temp;
          s_temp += s[j];
          st.push(s_temp);
          j++;
        }

        // 演算子の処理
        else if (s[j] == '-' || s[j] == '+' || s[j] == '*' || s[j] == '/') {
          string s_temp;
          s_temp += s[j];
          if (st.empty()) {
            st.push(s_temp);
            j++;
          } else {
            if (!((s_temp == "*" || s_temp == "/") &&
                  (st.top() == "-" || st.top() == "+"))) {
              s_temp = st.top();
              st.pop();
              qu.push(s_temp);
            } else {
              st.push(s_temp);
              j++;
            }
          }
        }
        //=の処理
        else if (s[j] == '=') {
          j++;
        }
      }
      while (!st.empty()) {
        qu.push((st.top()));
        st.pop();
      }

      stack<int> num;
      while (!qu.empty()) {

        // cout<<qu.front()<<endl;

        if (qu.front() == "*") {
          int a, b;
          a = num.top();
          num.pop();
          b = num.top();
          num.pop();
          a = b * a;
          num.push(a);
        } else if (qu.front() == "/") {
          int a, b;
          a = num.top();
          num.pop();
          b = num.top();
          num.pop();
          a = b / a;
          num.push(a);

        } else if (qu.front() == "-") {
          int a, b;
          a = num.top();
          num.pop();
          b = num.top();
          num.pop();
          a = b - a;
          num.push(a);
        } else if (qu.front() == "+") {
          int a, b;
          a = num.top();
          num.pop();
          b = num.top();
          num.pop();
          a = b + a;
          num.push(a);
        } else {
          stringstream stream;
          stream << qu.front();
          int temp;
          stream >> temp;
          num.push(temp);
        }
        qu.pop();
      }
      cout << num.top() << endl;
    }
  }
}