#include #include #include #include using namespace std; int MIN = -1000000000; int MAX = 1000000000; int main() { int param[10000]; char instruction[10000][5]; for(;;) { int i = 0; for(;;) { scanf("%s", instruction[i]); if (!strcmp(instruction[i], "QUIT")) goto end; if (!strcmp(instruction[i], "END")) break; if (!strcmp(instruction[i], "NUM")) scanf("%d", ¶m[i]); i++; } int n; scanf("%d", &n); int start; for (int j = 0; j < n; j++) { stack *s = new stack(); scanf("%d", &start); s->push(start); for (int k = 0; k < i; k++) { if (!strcmp(instruction[k], "NUM")) { s->push(param[k]); continue; } if (!strcmp(instruction[k], "POP")) { if (s->size() == 0) goto next; s->pop(); continue; } if (!strcmp(instruction[k], "INV")) { int temp = -s->top(); s->pop(); s->push(temp); continue; } if (!strcmp(instruction[k], "DUP")) { s->push(s->top()); continue; } if (s->size() < 2) { printf("ERROR\n"); goto next; } int temp1 = s->top(); s->pop(); int temp2 = s->top(); s->pop(); if (!strcmp(instruction[k], "SWP")) { s->push(temp2); s->push(temp1); } if (!strcmp(instruction[k], "ADD")) s->push(temp1 + temp2); if (!strcmp(instruction[k], "SUB")) s->push(temp2 - temp1); if (!strcmp(instruction[k], "MUL")) s->push(temp1 * temp2); if (!strcmp(instruction[k], "DIV")) { if (temp1 == 0) goto next; s->push(temp2 / temp1); } if (!strcmp(instruction[k], "MOD")) { if (temp1 == 0) goto next; s->push(temp2 % temp1); } if (s->top() > MAX || s->top() < MIN) goto next; } if (s->size() == 1) printf("%d\n", s->top()); else next: printf("ERROR\n"); } printf("\n"); } end: return 0; }