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

#include <iostream>
#include <stack>

int OR[3][3] = {
    {0, 1, 2},
    {1, 1, 2},
    {2, 2, 2},
};
int AND[3][3] = {
    {0, 0, 0},
    {0, 1, 1},
    {0, 1, 2},
};

int conv(char a, int p, int q, int r) {
  switch (a) {
  case 'P':
    return p;
  case 'Q':
    return q;
  case 'R':
    return r;
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  }
}

bool calc(char *s, int P, int Q, int R) {
  std::stack<char> st;

  for (int i = 0; s[i] != '\\0'; i++) {

    if (s[i] == ')') {
      int m;

      char rc = st.top();
      st.pop();
      int r = conv(rc, P, Q, R);
      for (m = 0; st.top() == '-'; m++)
        st.pop();
      if (m % 2)
        r = 2 - r;

      char o = st.top();
      st.pop();

      char lc = st.top();
      st.pop();
      int l = conv(lc, P, Q, R);
      for (m = 0; st.top() == '-'; m++)
        st.pop();
      if (m % 2)
        l = 2 - l;
      st.pop();

      int tmp;
      if (o == '*')
        tmp = AND[r][l];
      else
        tmp = OR[r][l];

      for (m = 0; !st.empty() && st.top() == '-'; m++)
        st.pop();
      if (m % 2)
        tmp = 2 - tmp;

      st.push((char)(tmp + '0'));
    } else
      st.push(s[i]);
  }

  int tmp = conv(st.top(), P, Q, R);
  st.pop();
  int m;
  for (m = 0; !st.empty() && st.top() == '-'; m++)
    st.pop();
  if (m % 2)
    tmp = 2 - tmp;

  if (tmp == 2)
    return true;
  return false;
}

int solve(char *s) {
  int ans = 0;
  for (int p = 0; p < 3; p++)
    for (int q = 0; q < 3; q++)
      for (int r = 0; r < 3; r++)
        if (calc(s, p, q, r))
          ans++;
  return ans;
}

int main() {

  char x[100];

  while (true) {
    std::cin >> x;

    if (x[0] == '.')
      break;

    std::cout << solve(x) << std::endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>

int OR[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
int AND[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};

int conv(char a, int p, int q, int r) {
  switch (a) {
  case 'P':
    return p;
  case 'Q':
    return q;
  case 'R':
    return r;
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  }
}

bool calc(char *s, int P, int Q, int R) {
  std::stack<char> st;

  for (int i = 0; s[i] != '\\0'; i++) {

    if (s[i] == ')') {
      int m;

      char rc = st.top();
      st.pop();
      int r = conv(rc, P, Q, R);
      for (m = 0; st.top() == '-'; m++)
        st.pop();
      if (m % 2)
        r = 2 - r;

      char o = st.top();
      st.pop();

      char lc = st.top();
      st.pop();
      int l = conv(lc, P, Q, R);
      for (m = 0; st.top() == '-'; m++)
        st.pop();
      if (m % 2)
        l = 2 - l;
      st.pop();

      int tmp;
      if (o == '*')
        tmp = AND[r][l];
      else
        tmp = OR[r][l];

      if (!st.empty() && st.top() == '-') {
        for (m = 0; !st.empty(); m++)
          st.pop();
        if (m % 2)
          tmp = 2 - tmp;
      }

      st.push((char)(tmp + '0'));
    } else
      st.push(s[i]);
  }

  if (st.top() == '2')
    return true;
  return false;
}

int solve(char *s) {
  int ans = 0;
  for (int p = 0; p < 3; p++)
    for (int q = 0; q < 3; q++)
      for (int r = 0; r < 3; r++)
        if (calc(s, p, q, r))
          ans++;
  return ans;
}

int main() {

  char x[100];

  while (true) {
    std::cin >> x;

    if (x[0] == '.')
      break;

    std::cout << solve(x) << std::endl;
  }

  return 0;
}