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

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define M_PI 3.141592
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " " << x << endl;

#define a n[0]
#define b n[1]
#define c n[2]
#define d n[3]

int calc(int x, int y, int op) {
  switch (op) {
  case '+':
    return x + y;
  case '-':
    return x - y;
  case '*':
    return x * y;
  }
}

int main() {
  vector<int> n(4);
  string op = "+++---***";
  while (cin >> a >> b >> c >> d && a || b || c || d) {
    bool flag = true;
    sort(all(n));
    do {
      sort(all(op));
      do {
        if (calc(calc(calc(a, b, op[0]), c, op[1]), d, op[2]) == 10) {
          printf("(((%d%c%d)%c%d)%c%d)\\n", a, op[0], b, op[1], c, op[2], d);
          flag = false;
          break;
        }

        if (calc(calc(a, b, op[0]), calc(c, d, op[2]), op[1]) == 10) {
          printf("((%d%c%d)%c(%d%c%d))\\n", a, op[0], b, op[1], c, op[2], d);
          flag = false;
          break;
        }

        if (calc(calc(a, calc(b, c, op[1]), op[0]), d, op[2]) == 10) {
          printf("((%d%c(%d%c%d))%c%d)\\n", a, op[0], b, op[1], c, op[2], d);
          flag = false;
          break;
        }

        if (calc(a, calc(calc(b, c, op[1]), d, op[2]), op[0]) == 10) {
          printf("(%d%c((%d%c%d)%c%d))\\n", a, op[0], b, op[1], c, op[2], d);
          flag = false;
          break;
        }

        if (calc(a, calc(b, calc(c, d, op[2]), op[1]), op[0]) == 10) {
          printf("(%d%c(%d%c(%d%c%d)))\\n", a, op[0], b, op[1], c, op[2], d);
          flag = false;
          break;
        }

      } while (next_permutation(all(op)));

    } while (next_permutation(all(n)) && flag);
    if (flag) {
      cout << 0 << endl;
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
bool check(char a, char b) {
  if (a > b)
    return true;
  return false;
}
int cc(int a, int b, char c) {
  switch (c) {
  case '+':
    return a + b;
  case '-':
    return a - b;
  case '*':
    return a * b;
  }
}

int calc(string a) {
  stack<char> s;
  string buf;
  cout << a << endl;
  for (int i = 0; i < a.size(); i++) {
    cout << buf << endl;
    if ('0' <= a[i] && a[i] <= '9') {
      buf += a[i];
      continue;
    } else if (a[i] == ')') {
      while (!s.empty()) {
        if (s.top() == '(') {
          s.pop();
          break;
        } else {
          buf += s.top();
          s.pop();
        }
      }
      continue;
    } else if (a[i] == '(') {
      s.push('(');
      continue;
    };
    ;
  P:;
    ;
    if (s.empty()) {
      s.push(a[i]);
      continue;
    } else {
      int t = 0;
      for (t = 0; s.top() == '('; t++, s.pop())
        ;
      if (s.top() < a[i]) {
        buf += s.top();
        s.pop();
        for (int l = 0; l < t; l++)
          s.push('(');
        goto P;
      } else {
        for (int l = 0; l < t; l++)
          s.push('(');
        s.push(a[i]);
      }
    }
  }
  while (!s.empty()) {
    buf += s.top();
    s.pop();
  };
  cout << buf << endl;
  stack<int> ss;
  int ans = 0;
  for (int i = 0; i < buf.size(); i++) {
    if ('0' <= buf[i] && buf[i] <= '9') {
      ss.push(buf[i] - '0');
    } else {
      int A, B;
      A = ss.top();
      ss.pop();
      B = ss.top();
      ss.pop();
      ss.push(cc(A, B, buf[i]));
    }
  }
  cout << ss.top() << endl;
  return ss.top();
}

int main() {
  while (true) {
    vector<int> a;
    for (int i = 0; i < 4; i++) {
      int x;
      cin >> x;
      a.push_back(x);
    }
    for (int i = 0; i < 4; i++) {
      if (a[i] != 0)
        break;
      if (i == 3)
        return 0;
    }
    string op = "+-*";
    do {
      for (int i = 0; i < 3; i++) {
        for (int l = 0; l < 3; l++) {
          for (int k = 0; k < 3; k++) {
            char str[150];
            string s;

            sprintf(str, "%d%c%d%c%d%c%d", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("%d%c%d%c%d%c%d\\n", a[0], op[i], a[1], op[l], a[2], op[k],
                     a[3]);
              goto end;
            }

            sprintf(str, "(%d%c%d)%c%d%c%d", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("(%d%c%d)%c%d%c%d\\n", a[0], op[i], a[1], op[l], a[2],
                     op[k], a[3]);
              goto end;
            }

            sprintf(str, "%d%c(%d%c%d)%c%d", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("%d%c(%d%c%d)%c%d\\n", a[0], op[i], a[1], op[l], a[2],
                     op[k], a[3]);
              goto end;
            }

            sprintf(str, "%d%c%d%c(%d%c%d)", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("%d%c%d%c(%d%c%d)\\n", a[0], op[i], a[1], op[l], a[2],
                     op[k], a[3]);
              goto end;
            }

            sprintf(str, "((%d%c%d)%c%d)%c%d", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("((%d%c%d)%c%d)%c%d\\n", a[0], op[i], a[1], op[l], a[2],
                     op[k], a[3]);
              goto end;
            }

            sprintf(str, "(%d%c(%d%c%d))%c%d", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("(%d%c(%d%c%d))%c%d\\n", a[0], op[i], a[1], op[l], a[2],
                     op[k], a[3]);
              goto end;
            }

            sprintf(str, "%d%c((%d%c%d)%c%d)", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("%d%c((%d%c%d)%c%d)\\n", a[0], op[i], a[1], op[l], a[2],
                     op[k], a[3]);
              goto end;
            }

            sprintf(str, "%d%c(%d%c(%d%c%d))", a[0], op[i], a[1], op[l], a[2],
                    op[k], a[3]);
            if (calc((string)str) == 10) {
              printf("%d%c(%d%c(%d%c%d))\\n", a[0], op[i], a[1], op[l], a[2],
                     op[k], a[3]);
              goto end;
            }
          }
        }
      }
    } while (next_permutation(a.begin(), a.end()));
    ;
  end:;
  }
}