#include #include #include #include #include #include using namespace std; #define MAXVAL 1000000000 struct ins { enum tp { NUM, POP, INV, DUP, SWP, ADD, SUB, MUL, DIV, MOD }; tp type; int arg; }; static ins::tp parse_ins(const char *buf) { if(strcmp(buf, "NUM") == 0) return ins::NUM; if(strcmp(buf, "POP") == 0) return ins::POP; if(strcmp(buf, "INV") == 0) return ins::INV; if(strcmp(buf, "DUP") == 0) return ins::DUP; if(strcmp(buf, "SWP") == 0) return ins::SWP; if(strcmp(buf, "ADD") == 0) return ins::ADD; if(strcmp(buf, "SUB") == 0) return ins::SUB; if(strcmp(buf, "MUL") == 0) return ins::MUL; if(strcmp(buf, "DIV") == 0) return ins::DIV; if(strcmp(buf, "MOD") == 0) return ins::MOD; assert(false); } static void solve() { vector INS; vector stack; for(;;) { static char buf[8]; scanf("%s", buf); if(strcmp(buf, "QUIT") == 0) exit(0); if(strcmp(buf, "END") == 0) break; ins ins; ins.type = parse_ins(buf); if(ins.type == ins::NUM) scanf("%d", &ins.arg); INS.push_back(ins); } int n; scanf("%d",&n); while(n--) { int res; long long resres; scanf("%d",&res); stack.clear(); stack.push_back(res); for(int i=0; i MAXVAL) goto armageddon; stack.pop_back(); stack.back() = res; break; case ins::SUB: if(stack.size()<2) goto armageddon; res = stack[stack.size()-2] - stack[stack.size()-1]; if(abs(res) > MAXVAL) goto armageddon; stack.pop_back(); stack.back() = res; break; case ins::MUL: if(stack.size()<2) goto armageddon; resres = 1LL*stack[stack.size()-2] * 1LL*stack[stack.size()-1]; if(abs(resres) > MAXVAL) goto armageddon; stack.pop_back(); stack.back() = resres; break; case ins::DIV: if(stack.size()<2) goto armageddon; if(stack.back() == 0) goto armageddon; res = stack[stack.size()-2] / stack[stack.size()-1]; stack.pop_back(); stack.back() = res; break; case ins::MOD: if(stack.size()<2) goto armageddon; if(stack.back() == 0) goto armageddon; res = stack[stack.size()-2] % stack[stack.size()-1]; stack.pop_back(); stack.back() = res; break; } if(stack.size() != 1) goto armageddon; printf("%d\n", stack.back()); continue; armageddon: printf("ERROR\n"); } printf("\n"); } int main(void) { for(;;) solve(); }