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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

struct Info {
  Info() {
    least = most = number = -1;
    isVariable = false;
  }
  Info(int arg_least, int arg_most, int arg_number, bool arg_is_val) {
    least = arg_least;
    most = arg_most;
    number = arg_number;
    isVariable = arg_is_val;
  }

  char name[21];
  int least, most, number;
  bool isVariable;
};

bool strCmp(char *base, char *comp) {
  int length1 = 0, length2 = 0;
  for (int i = 0; base[i] != '\\0'; i++)
    length1++;
  for (int i = 0; comp[i] != '\\0'; i++)
    length2++;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\\0';
  }
}

int main() {

  int num, N;
  scanf("%d", &num);
  Info vals[num];

  for (int i = 0; i < num; i++) {
    scanf("%s %d %d", vals[i].name, &vals[i].least, &vals[i].most);
  }

  stack<Info> S;

  scanf("%d", &N);

  int tmp, least, most;
  char buf[21];
  bool correct = true;
  Info op1, op2;

  for (int i = 0; i < N; i++) {
    scanf("%s", buf);

    if (buf[0] == '+' || buf[0] == '*' || buf[0] == '/' || buf[0] == '-') {
      if (S.empty()) {
        correct = false;
        break;
      }
      op2 = S.top();
      S.pop();
      if (S.empty()) {
        correct = false;
        break;
      }
      op1 = S.top();
      S.pop();

      switch (buf[0]) {
      case '+':
        if (op1.isVariable == false && op2.isVariable == false) {
          Info info;
          info.isVariable = false;
          info.number = (op1.number + op2.number) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k + p) % 256);
                most = max(most, (k + p) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (k + op1.number) % 256);
              most = max(most, (k + op1.number) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k + op2.number) % 256);
              most = max(most, (k + op2.number) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      case '/':
        if (op1.isVariable == false && op2.isVariable == false) {
          if (op2.number == 0) {
            correct = false;
            break;
          }

          Info info;
          info.isVariable = false;
          info.number = (op1.number / op2.number) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            if (op2.least == 0) {
              correct = false;
              break;
            }

            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k / p) % 256);
                most = max(most, (k / p) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            if (op2.least == 0) {
              correct = false;
              break;
            }

            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (op1.number / k) % 256);
              most = max(most, (op1.number / k) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            if (op2.number == 0) {
              correct = false;
              break;
            }
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k / op2.number) % 256);
              most = max(most, (k / op2.number) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      case '-':
        if (op1.isVariable == false && op2.isVariable == false) {
          Info info;
          info.isVariable = false;
          info.number = (op1.number - op2.number + 256) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k - p + 256) % 256);
                most = max(most, (k - p + 256) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (op1.number - k + 256) % 256);
              most = max(most, (op1.number - k + 256) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k - op2.number + 256) % 256);
              most = max(most, (k - op2.number + 256) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      case '*':
        if (op1.isVariable == false && op2.isVariable == false) {
          Info info;
          info.isVariable = false;
          info.number = (op1.number * op2.number) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k * p) % 256);
                most = max(most, (k * p) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (op1.number * k) % 256);
              most = max(most, (op1.number * k) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k * op2.number) % 256);
              most = max(most, (k * op2.number) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      }

    } else {

      if (buf[0] >= '0' && buf[0] <= '9') {
        tmp = 0;
        for (int k = 0; buf[k] != '\\0'; k++) {
          tmp = 10 * tmp + buf[k] - '0';
        }
        Info info;
        info.isVariable = false;
        info.number = tmp;
        S.push(info);
      } else {
        for (int k = 0; k < num; k++) {
          if (strCmp(buf, vals[k].name)) {
            Info info;
            info.isVariable = true;
            info.least = vals[k].least;
            info.most = vals[k].most;
            strcpy(info.name, buf);
            S.push(info);
            break;
          }
        }
      }
    }
    if (!correct)
      break;
  }

  if (correct) {
    printf("correct\\n");
  } else {
    printf("error\\n");
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

struct Info {
  Info() {
    least = most = number = -1;
    isVariable = false;
  }
  Info(int arg_least, int arg_most, int arg_number, bool arg_is_val) {
    least = arg_least;
    most = arg_most;
    number = arg_number;
    isVariable = arg_is_val;
  }

  char name[21];
  int least, most, number;
  bool isVariable;
};

bool strCmp(char *base, char *comp) {
  int length1 = 0, length2 = 0;
  for (int i = 0; base[i] != '\\0'; i++)
    length1++;
  for (int i = 0; comp[i] != '\\0'; i++)
    length2++;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\\0';
  }
}

int main() {

  int num, N;
  scanf("%d", &num);
  Info vals[num];

  for (int i = 0; i < num; i++) {
    scanf("%s %d %d", vals[i].name, &vals[i].least, &vals[i].most);
  }

  stack<Info> S;

  scanf("%d", &N);

  int tmp, least, most;
  char buf[21];
  bool correct = true;
  Info op1, op2;

  for (int i = 0; i < N; i++) {
    scanf("%s", buf);

    if (buf[0] == '+' || buf[0] == '*' || buf[0] == '/' || buf[0] == '-') {
      op2 = S.top();
      S.pop();
      op1 = S.top();
      S.pop();

      switch (buf[0]) {
      case '+':
        if (op1.isVariable == false && op2.isVariable == false) {
          Info info;
          info.isVariable = false;
          info.number = (op1.number + op2.number) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k + p) % 256);
                most = max(most, (k + p) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (k + op1.number) % 256);
              most = max(most, (k + op1.number) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k + op2.number) % 256);
              most = max(most, (k + op2.number) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      case '/':
        if (op1.isVariable == false && op2.isVariable == false) {
          if (op2.number == 0) {
            correct = false;
            break;
          }

          Info info;
          info.isVariable = false;
          info.number = (op1.number / op2.number) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            if (op2.least == 0) {
              correct = false;
              break;
            }

            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k / p) % 256);
                most = max(most, (k / p) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            if (op2.least == 0) {
              correct = false;
              break;
            }

            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (op1.number / k) % 256);
              most = max(most, (op1.number / k) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            if (op2.number == 0) {
              correct = false;
              break;
            }
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k / op2.number) % 256);
              most = max(most, (k / op2.number) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      case '-':
        if (op1.isVariable == false && op2.isVariable == false) {
          Info info;
          info.isVariable = false;
          info.number = (op1.number - op2.number + 256) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k - p + 256) % 256);
                most = max(most, (k - p + 256) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (op1.number - k) % 256);
              most = max(most, (op1.number - k) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k - op2.number + 256) % 256);
              most = max(most, (k - op2.number + 256) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      case '*':
        if (op1.isVariable == false && op2.isVariable == false) {
          Info info;
          info.isVariable = false;
          info.number = (op1.number * op2.number) % 256;
          S.push(info);
        } else {
          if (op1.isVariable == true && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              for (int p = op2.least; p <= op2.most; p++) {
                least = min(least, (k * p) % 256);
                most = max(most, (k * p) % 256);
              }
            }
          } else if (op1.isVariable == false && op2.isVariable == true) {
            least = 256, most = -1;
            for (int k = op2.least; k <= op2.most; k++) {
              least = min(least, (op1.number * k) % 256);
              most = max(most, (op1.number * k) % 256);
            }
          } else { // op1.isVariable == true && op2.isVariable = false;
            least = 256, most = -1;
            for (int k = op1.least; k <= op1.most; k++) {
              least = min(least, (k * op2.number) % 256);
              most = max(most, (k * op2.number) % 256);
            }
          }
          Info info;
          info.isVariable = true;
          info.least = least;
          info.most = most;
          S.push(info);
        }
        break;
      }

    } else {

      if (buf[0] >= '0' && buf[0] <= '9') {
        tmp = 0;
        for (int k = 0; buf[k] != '\\0'; k++) {
          tmp = 10 * tmp + buf[k] - '0';
        }
        Info info;
        info.isVariable = false;
        info.number = tmp;
        S.push(info);
      } else {
        for (int k = 0; k < num; k++) {
          if (strCmp(buf, vals[k].name)) {
            Info info;
            info.isVariable = true;
            info.least = vals[k].least;
            info.most = vals[k].most;
            strcpy(info.name, buf);
            S.push(info);
            break;
          }
        }
      }
    }
    if (!correct)
      break;
  }

  if (correct) {
    printf("correct\\n");
  } else {
    printf("error\\n");
  }

  return 0;
}