//team42 (Lukasz Zatorski, Damian Rusak, Krzysztof Piecuch) #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; typedef pair pi; typedef vector vi; typedef vector< vi > vii; #define ft first #define sd second #define mp make_pair #define pb push_back #define REP(i,a,b) for(register int i = a ; i < b; i++) #define DOWN(i,a,b) for(register int i = a; i>=b ; i--) #define WSK(C) typeof((C).begin()) #define FOREACH(wsk, C) for(WSK(C) wsk = (C).begin(); wsk!=(C).end; wsk++) #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 MUST(a) { if (top < a) { error = true; break; } } int main() { int top; char INS[10]; ll STOS[1001]; int PROG[100001]; ll NUMS[100001]; while(true) { int it = 0; while (true) { scanf("%s", INS); if (!strcmp(INS, "QUIT")) return 0; if (!strcmp(INS, "NUM")) { scanf("%lld", &NUMS[it]); PROG[it] = NUM; } else if (!strcmp(INS, "POP")) PROG[it] = POP; else if (!strcmp(INS, "INV")) PROG[it] = INV; else if (!strcmp(INS, "DUP")) PROG[it] = DUP; else if (!strcmp(INS, "SWP")) PROG[it] = SWP; else if (!strcmp(INS, "ADD")) PROG[it] = ADD; else if (!strcmp(INS, "SUB")) PROG[it] = SUB; else if (!strcmp(INS, "MUL")) PROG[it] = MUL; else if (!strcmp(INS, "DIV")) PROG[it] = DIV; else if (!strcmp(INS, "MOD")) PROG[it] = MOD; else if (!strcmp(INS, "END")) break; else printf("ERROR! :-( \"%s\"\n", INS); it++; } int N; scanf("%d", &N); for (int k = 0; k < N; k++) { int W; scanf("%d", &W); bool error = false; top = 1; STOS[0] = W; for (int j = 0; j < it; j++) { if (PROG[j] == NUM) STOS[top++] = NUMS[j]; else if (PROG[j] == POP) { MUST(1); top--; } else if (PROG[j] == INV) { MUST(1); STOS[top-1] = - STOS[top-1]; } else if (PROG[j] == DUP) { MUST(1); STOS[top] = STOS[top-1]; top++; } else if (PROG[j] == SWP) { MUST(2); ll v = STOS[top-1]; STOS[top-1] = STOS[top-2]; STOS[top-2] = v; } else if (PROG[j] == ADD) { MUST(2); STOS[top-2] += STOS[top-1]; top--; } else if (PROG[j] == MUL) { MUST(2); STOS[top-2] *= STOS[top-1]; top--; } else if (PROG[j] == DIV) { MUST(2); if (STOS[top-1] == 0) { error = true; break; } STOS[top-2] /= STOS[top-1]; top--; } else if (PROG[j] == MOD) { MUST(2); if (STOS[top-1] == 0) { error = true; break; } STOS[top-2] %= STOS[top-1]; top--; } if (top > 0) if (STOS[top-1] < -1000000000 || 1000000000 < STOS[top-1]) { error = true; break; } } if (top == 1 && !error) printf("%lld\n", STOS[0]); else printf("ERROR\n"); } printf("\n"); } return 0; }