#include #include #include #include #include #include #include #include #define FOR(i,n) for(int i=0; i PII; const int MAX = 1100; const int MAX_I=100100; const int INF=1000000000; const LL INF_LL=INF; vector dane; struct stack { int tab[MAX]; int size; void num(int a) { tab[size]=a; size++; } bool pop() {size--; if(size<0) return 0; return 1;} bool inv() {if(!size) return 0; tab[size-1]*=-1; return 1;} bool dup() {if(!size) return 0; tab[size]=tab[size-1]; size++; return 1;} bool swp() {if(size<2) return 0; swap(tab[size-1],tab[size-2]); return 1;} bool add() { if(size<2) return 0; tab[size-2]=tab[size-1]+tab[size-2]; if(abs(tab[size-2])>INF) return 0; size--; return 1; } bool sub() { if(size<2) return 0; tab[size-2]=tab[size-2]-tab[size-1]; if(abs(tab[size-2])>INF) return 0; size--; return 1; } bool mul() { if(size<2 || abs(LL(tab[size-1]) * LL(tab[size-2]))>INF_LL) return 0; tab[size-2]=tab[size-1]*tab[size-2]; size--; return 1; } bool div() { if(size<2 || tab[size-1]==0) return 0; tab[size-2]=tab[size-2]/tab[size-1]; size--; return 1; } bool mod() { if(size<2 || tab[size-1]==0) return 0; tab[size-2]=tab[size-2]%tab[size-1]; size--; return 1; } void clear() {size=0;} int play(int a) { num(a); FORI(it,dane) { if(it->frs == 0) num(it->sec); else if(it-> frs == 1) {if(!pop()) return INF+1;} else if(it-> frs == 2) {if(!inv()) return INF+1;} else if(it-> frs == 3) {if(!dup()) return INF+1;} else if(it-> frs == 4) {if(!swp()) return INF+1;} else if(it-> frs == 5) {if(!add()) return INF+1;} else if(it-> frs == 6) {if(!sub()) return INF+1;} else if(it-> frs == 7) {if(!mul()) return INF+1;} else if(it-> frs == 8) {if(!div()) return INF+1;} else if(it-> frs == 9) {if(!mod()) return INF+1;} } if(size==1) return tab[size-1]; return INF+1; } }; char str[MAX]; stack ans; int main() { int a,n,b; while(strncmp(str,"QUIT",4)) { while(strncmp(str,"END",3)) { scanf("%s",str); if(strncmp(str,"QUIT",4)==0) break; if(strncmp(str,"NUM",3)==0) { scanf("%d", &a); dane.psh(mkp(0,a)); continue; } if(strncmp(str,"POP",3)==0) dane.psh(mkp(1,0)); else if(strncmp(str,"INV",3)==0) dane.psh(mkp(2,0)); else if(strncmp(str,"DUP",3)==0) dane.psh(mkp(3,0)); else if(strncmp(str,"SWP",3)==0) dane.psh(mkp(4,0)); else if(strncmp(str,"ADD",3)==0) dane.psh(mkp(5,0)); else if(strncmp(str,"SUB",3)==0) dane.psh(mkp(6,0)); else if(strncmp(str,"MUL",3)==0) dane.psh(mkp(7,0)); else if(strncmp(str,"DIV",3)==0) dane.psh(mkp(8,0)); else if(strncmp(str,"MOD",3)==0) dane.psh(mkp(9,0)); } if(strncmp(str,"QUIT",4)==0) break; scanf("%d", &n); while(n--) { scanf("%d", &a); b=ans.play(a); if(b==INF+1) printf("ERROR\n"); else printf("%d\n", b); ans.clear(); } printf("\n"); str[0]=0; dane.clear(); } //FORI(it,dane) printf("%d ", it->frs); }