#include #include #include #include #include #include using namespace std; typedef long long LL; char buf[256]; vectorop; vectorargs; stackS; const LL inf = 1000000000; void comp(LL x){ while(S.size()>0) S.pop(); bool error = false; LL tmp = 0, tmp2=0; S.push(x); for(int i=0; i<(int)op.size(); i++){ if(op[i]==string("NUM")){ S.push(args[i]); } else if(op[i]==string("POP")){ if(S.empty()) error = true; else { S.pop(); } } else if(op[i]==string("INV")){ if(S.empty()) error = true; else { tmp = S.top(); S.pop(); S.push(-tmp); } } else if(op[i]==string("DUP")){ if(S.empty()) error = true; else { tmp = S.top(); S.push(tmp); } } else if(op[i]==string("SWP")){ if(S.size()<2) error = true; else { tmp = S.top(); S.pop(); tmp2 = S.top(); S.pop(); S.push(tmp); S.push(tmp2); } } else if(op[i]==string("ADD")){ if(S.size()<2) error = true; else { tmp = S.top(); S.pop(); tmp2 = S.top(); S.pop(); S.push(tmp+tmp2); } } else if(op[i]==string("SUB")){ if(S.size()<2) error = true; else { tmp = S.top(); S.pop(); tmp2 = S.top(); S.pop(); S.push(tmp2-tmp); } } else if(op[i]==string("MUL")){ if(S.size()<2) error = true; else { tmp = S.top(); S.pop(); tmp2 = S.top(); S.pop(); S.push(tmp2*tmp); } } else if(op[i]==string("DIV")){ //XXX if(S.size()<2) error = true; else { tmp = S.top(); S.pop(); tmp2 = S.top(); S.pop(); if(tmp==0) error = true; else { S.push(tmp2/tmp); } } } else if(op[i]==string("MOD")){ //XXX if(S.size()<2) error = true; else { tmp = S.top(); S.pop(); tmp2 = S.top(); S.pop(); if(tmp==0) error = true; else { S.push(tmp2%tmp); } } } if(!S.empty() && abs(S.top())>inf) error = true; if(error){ printf("ERROR\n"); return; } } if(S.size()!=1){ printf("ERROR\n"); } else { tmp = S.top(); S.pop(); printf("%lld\n", tmp); } } bool solve(bool fst){ op.clear(); args.clear(); int k = 0; LL tmp; while(1){ scanf("%s",buf); if(string(buf)=="QUIT") return false; if(string(buf)=="END") break; op.push_back(buf); if(string(buf)==string("NUM")) { scanf("%lld",&tmp); args.push_back(tmp); } else { args.push_back(0); } } scanf("%d", &k); //printf("siz = %d\n",(int)op.size()); //printf("k=%d\n",k); LL x; if(!fst) printf("\n"); for(int i=0; i