#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define SIZEOF(a) (sizeof(a)/sizeof(a[0])) #define FILL(a, b) fill (a,a+SIZEOF(a),b) #define FOR(a,b,c) for(int a=b; a<=c; a++) #define FORARR(i,a) for(usigned i=0; i 1000000000 || st.top() < -1000000000) goto out inline long long sign(long long arg) { return (arg > 0 ? 1 : (arg < 0 ? -1 : 0)); } op makeOp(const string &opstr) { if (opstr == "NUM") return NUM; if (opstr == "POP") return POP; if (opstr == "INV") return INV; if (opstr == "DUP") return DUP; if (opstr == "SWP") return SWP; if (opstr == "ADD") return ADD; if (opstr == "SUB") return SUB; if (opstr == "MUL") return MUL; if (opstr == "DIV") return DIV; if (opstr == "MOD") return MOD; return QUIT; } int main(void) { string first; while (cin >> first && first != "QUIT") { op ops[100001]; long long numargs[100001]; ops[0] = makeOp(first); if (ops[0] == NUM) { cin >> numargs[0]; } int opscnt = 1; string opstr; for ( ; cin >> opstr && opstr != "END" ; opscnt++) { ops[opscnt] = makeOp(opstr); if (ops[opscnt] == NUM) { cin >> numargs[opscnt]; } } // DEBUG /* cout << "instrukce: " << endl; FOR(i,0,opscnt-1) { cout << (int)ops[i]; if (ops[i] == NUM) cout << "(" << numargs[i] << ")"; cout << endl; } cout << endl; */ // END DEBUG int N; scanf("%d", &N); FOR(i,0,N-1) { // one execution long long V; scanf("%lld", &V); // DEBUG /* cout << "computing for " << V << endl; */ // END DEBUG stack st; st.push(V); long long t, first, second; FOR(pc,0,opscnt-1) { // perform op number pc switch (ops[pc]) { case NUM: st.push(numargs[pc]); break; case POP: REQ; st.pop(); break; case INV: REQ; t = st.top(); st.pop(); st.push(-t); break; case DUP: REQ; t = st.top(); st.pop(); st.push(t); st.push(t); break; case SWP: REQ2; first = st.top(); st.pop(); second = st.top(); st.pop(); st.push(first); st.push(second); break; case ADD: REQ2; first = st.top(); st.pop(); second = st.top(); st.pop(); st.push(first + second); CHKTOP; break; case SUB: REQ2; first = st.top(); st.pop(); second = st.top(); st.pop(); st.push(second - first); CHKTOP; break; case MUL: REQ2; first = st.top(); st.pop(); second = st.top(); st.pop(); st.push(second * first); CHKTOP; break; case DIV: REQ2; first = st.top(); st.pop(); if (first == 0) goto out; second = st.top(); st.pop(); t = sign(first)*sign(second)*abs(second / first); st.push(t); break; case MOD: REQ2; first = st.top(); st.pop(); if (first == 0) goto out; second = st.top(); st.pop(); t = sign(second)*abs(second % first); st.push(t); break; default: cerr << "unknown instruction" << endl; } } if (st.size() == 1) { printf("%lld\n", st.top()); continue; } out: printf("ERROR\n"); } printf("\n"); } return 0; }