#include #include #include #include #include #include #include using namespace std; list op; stack st; int n; #define POP -1 #define INV -2 #define DUP -3 #define SWP -4 #define ADD -5 #define SUB -6 #define MUL -7 #define DIV -8 #define MOD -9 bool _pop() { if (st.empty()) return false; st.pop(); return true; } bool _inv() { if (st.empty()) return false; int v = st.top(); st.pop(); st.push(-v); return true; } bool _dup() { if (st.empty()) return false; int v = st.top(); st.push(v); return true; } bool _swp() { if (st.empty()) return false; int v1 = st.top(); st.pop(); if (st.empty()) return false; int v2 = st.top(); st.pop(); st.push(v1); st.push(v2); return true; } bool _add() { if (st.empty()) return false; int v1 = st.top(); st.pop(); if (st.empty()) return false; int v2 = st.top(); st.pop(); int res = v2 + v1; if (-1000000000 <= res && res <= 1000000000) st.push(res); else return false; return true; } bool _sub() { if (st.empty()) return false; int v1 = st.top(); st.pop(); if (st.empty()) return false; int v2 = st.top(); st.pop(); int res = v2 - v1; if (-1000000000 <= res && res <= 1000000000) st.push(res); else return false; return true; } bool _mul() { if (st.empty()) return false; int v1 = st.top(); st.pop(); if (st.empty()) return false; int v2 = st.top(); st.pop(); int res = v2 * v1; if (-1000000000 <= res && res <= 1000000000) st.push(res); else return false; return true; } bool _mod() { if (st.empty()) return false; int v1 = st.top(); st.pop(); if (st.empty()) return false; int v2 = st.top(); st.pop(); if (v1 == 0) return false; int res = abs(v2) % abs(v1); if (res <= 1000000000) { if (v2 < 0) res *= -1; st.push(res); } else return false; return true; } bool _div() { if (st.empty()) return false; int v1 = st.top(); st.pop(); if (st.empty()) return false; int v2 = st.top(); st.pop(); if (v1 == 0) return false; int res = v2 / v1; if (-1000000000 <= res && res <= 1000000000) st.push(res); else return false; return true; } int main (int argc, char ** argv) { char op_name[10]; scanf("%s", op_name); while (strncmp(op_name, "QUIT",4)) { op.clear(); while (strncmp(op_name, "END",3)) { if (!strcmp(op_name, "NUM")) { int num; scanf("%d", &num); op.push_back(num); } else if (!strcmp(op_name, "POP")) { op.push_back(POP); } else if (!strcmp(op_name, "INV")) { op.push_back(INV); } else if (!strcmp(op_name, "DUP")) { op.push_back(DUP); } else if (!strcmp(op_name, "SWP")) { op.push_back(SWP); } else if (!strcmp(op_name, "ADD")) { op.push_back(ADD); } else if (!strcmp(op_name, "SUB")) { op.push_back(SUB); } else if (!strcmp(op_name, "MUL")) { op.push_back(MUL); } else if (!strcmp(op_name, "DIV")) { op.push_back(DIV); } else if (!strcmp(op_name, "MOD")) { op.push_back(MOD); } scanf("%s", op_name); //printf("%s \n", op_name); } scanf("%d", &n); for (int i =0; i < n;i++) { while(!st.empty()) st.pop(); int initial; scanf ("%d", &initial); st.push(initial); list::iterator it = op.begin(); bool succ = true; while (it != op.end() && succ) { int cur_op = *it; if (cur_op>=0) { st.push(cur_op); } else { switch (cur_op) { case POP: succ = _pop(); break; case INV: succ = _inv(); break; case DUP: succ = _dup(); break; case SWP: succ = _swp(); break; case ADD: succ = _add(); break; case SUB: succ = _sub(); break; case MUL: succ = _mul(); break; case DIV: succ = _div(); break; case MOD: succ = _mod(); break; } } it++; } if (succ && st.size() == 1) printf("%d\n", st.top()); else printf("ERROR\n"); } printf("\n"); scanf("%s", op_name); } return 0; }