// // File: execute.cc // Author: cteam21 // // Created on November 13, 2011, 9:22 AM // #include #include #include // // // #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 #define FAIL {printf("ERROR\n");return;} int programme [100000]; int stack[1000]; void execute(int n){ //printf("execute\n"); int temp; long long int temp2; int pointer=1; for (int i=0; i1000000000)FAIL pointer--; } break; } case SUB:{ if (pointer<2)FAIL else{ stack[pointer-2]-=stack[pointer-1]; if (stack[pointer-2]<-1000000000)FAIL pointer--; } break; } case MUL:{ if (pointer<2)FAIL else{ temp2=(long long int)stack[pointer-2]*stack[pointer-1]; if (temp2>1000000000||temp2<-1000000000)FAIL stack[pointer-2]=temp2; pointer--; } break; } case DIV:{ if (pointer<2)FAIL else{ if (stack[pointer-1]==0)FAIL stack[pointer-2]/=stack[pointer-1]; pointer--; } break; } case MOD:{ if (pointer<2)FAIL else{ if (stack[pointer-1]==0)FAIL stack[pointer-2]%=stack[pointer-1]; pointer--; } break; } default:{ stack[pointer++]=programme[i]-10; } } } if (pointer == 1) printf("%d\n", stack[pointer-1]); else FAIL } int main(int argc, char** argv) { char command[4]; int index; int n; command [3]='\0'; while (42 ){ index=0; while (42){ scanf("%c%c%c", &command[0], &command[1], &command[2]); //printf("command %s\n", command); if (command[0]=='Q')return 0; if (!strcmp(command, "POP")) programme [index++]=POP; else if (!strcmp(command, "INV")) programme [index++]=INV; else if (!strcmp(command, "DUP")) programme [index++]=DUP; else if (!strcmp(command, "SWP")) programme [index++]=SWP; else if (!strcmp(command, "ADD")) programme [index++]=ADD; else if (!strcmp(command, "SUB")) programme [index++]=SUB; else if (!strcmp(command, "MUL")) programme [index++]=MUL; else if (!strcmp(command, "DIV")) programme [index++]=DIV; else if (!strcmp(command, "MOD")) programme [index++]=MOD; else if (!strcmp(command, "END")) { break; } else { int num; //getchar(); scanf("%d", &num); //printf("cislo %d\n",num); programme[index++]=num+10; } getchar(); } scanf("%d", &n); for (int i=0; i