#include #include #include #include using namespace std; typedef long long int LLI; #define NUM 1 #define POP 2 #define INV 3 #define DUP 4 #define SWP 5 #define ADD 6 #define SUB 7 #define MUL 8 #define DIV 9 #define MOD 10 #define MAX 1000000000 int main() { char command[10]; bool quit = false; int n; LLI f, a, b; while (true) { vector program; vector num; while (true) { scanf("%s", command); if (strcmp(command, "QUIT") == 0) { quit = true; break; } else if (strcmp(command, "NUM") == 0) { scanf("%lld", &a); program.push_back(NUM); num.push_back(a); } else if (strcmp(command, "POP") == 0) { program.push_back(POP); } else if (strcmp(command, "INV") == 0) { program.push_back(INV); } else if (strcmp(command, "DUP") == 0) { program.push_back(DUP); } else if (strcmp(command, "SWP") == 0) { program.push_back(SWP); } else if (strcmp(command, "ADD") == 0) { program.push_back(ADD); } else if (strcmp(command, "SUB") == 0) { program.push_back(SUB); } else if (strcmp(command, "MUL") == 0) { program.push_back(MUL); } else if (strcmp(command, "DIV") == 0) { program.push_back(DIV); } else if (strcmp(command, "MOD") == 0) { program.push_back(MOD); } else if (strcmp(command, "END") == 0) { break; } } #if 0 for (vector::iterator i = program.begin(); i != program.end(); i++) { printf("%d ", *i); } puts(""); printf("%lld\n", num); #endif if (quit) break; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &f); stack s; s.push(f); bool error = false; int inum = 0; for (vector::iterator i = program.begin(); i != program.end(); i++) { if (*i == NUM) { s.push(num[inum++]); } else if (*i == POP) { if (!s.empty()) { s.pop(); } else { puts("ERROR"); error = true; break; } } else if (*i == INV) { if (!s.empty()) { a = s.top(); s.pop(); s.push(-a); } else { puts("ERROR"); error = true; break; } } else if (*i == DUP) { if (!s.empty()) { s.push(s.top()); } else { puts("ERROR"); error = true; break; } } else if (*i == SWP) { if ((int)s.size() >= 2) { a = s.top(); s.pop(); b = s.top(); s.pop(); s.push(a); s.push(b); } else { puts("ERROR"); error = true; break; } } else if (*i == ADD) { if ((int)s.size() >= 2) { a = s.top(); s.pop(); b = s.top(); s.pop(); a += b; if (a < -MAX || a > MAX) { puts("ERROR"); error = true; break; } s.push(a); } else { puts("ERROR"); error = true; break; } } else if (*i == SUB) { if ((int)s.size() >= 2) { a = s.top(); s.pop(); b = s.top(); s.pop(); b -= a; if (b < -MAX || b > MAX) { puts("ERROR"); error = true; break; } s.push(b); } else { puts("ERROR"); error = true; break; } } else if (*i == MUL) { if ((int)s.size() >= 2) { a = s.top(); s.pop(); b = s.top(); s.pop(); b *= a; if (b < -MAX || b > MAX) { puts("ERROR"); error = true; break; } s.push(b); } else { puts("ERROR"); error = true; break; } } else if (*i == DIV) { if ((int)s.size() >= 2) { a = s.top(); s.pop(); b = s.top(); s.pop(); if (a == 0) { puts("ERROR"); error = true; break; } b /= a; s.push(b); } else { puts("ERROR"); error = true; break; } } else if (*i == MOD) { if ((int)s.size() >= 2) { a = s.top(); s.pop(); b = s.top(); s.pop(); bool sign = false; if (a < 0 || b < 0) sign = true; if (a == 0) { puts("ERROR"); error = true; break; } b %= a; if (sign) { if (b > 0) b = -b; } s.push(b); } else { puts("ERROR"); error = true; break; } } } if (!error) { if (s.size() == 1) printf("%lld\n", s.top()); else puts("ERROR"); } } puts(""); } return 0; }