#include #include #define END 0 #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 NUM 10 int stack[1001]; int program[100001]; int *ip; int *sp; void execute() { int cases = 0; scanf( "%d", &cases ); for( int i = 0; i < cases; i++ ) { sp = stack; ip = program; scanf( "%d", sp ); int s, d; for( ;; ) { switch( *ip ) { case NUM: sp++; ip++; *sp = *ip; break; case DUP: s = *sp; sp++; *sp = s; break; case DIV: d = *sp; sp--; *sp = *sp / d; break; case MUL: d = *sp; sp--; *sp = *sp * d; if( abs( *sp ) > 1000000000 ) goto error; break; case ADD: d = *sp; sp--; *sp = *sp + d; if( abs( *sp ) > 1000000000 ) goto error; break; case SUB: d = *sp; sp--; *sp = *sp - d; if( abs( *sp ) > 1000000000 ) goto error; break; case MOD: d = *sp; sp--; *sp = *sp % d; break; case POP: sp--; break; case INV: *sp = -*sp; break; case SWP: s = *sp; sp--; d = *sp; *sp = s; sp++; *sp = d; break; case END: if( sp < stack || sp > stack ) { error: printf("ERROR\n"); } else { printf("%d\n", *sp ); } goto next; } // switch ip++; } // for(;;) next: ; } printf( "\n" ); } int main() { char ins[5]; start: ip = program; for( ;; ) { scanf( "%s", ins ); int num; switch( ins[0] ) { case 'N': scanf( "%d", &num ); *ip = NUM; ip++; *ip = num; ip++; break; case 'P': *ip = POP; ip++; break; case 'I': *ip = INV; ip++; break; case 'D': if( ins[1] == 'U' ) { *ip = DUP; } else { *ip = DIV; } ip++; break; case 'S': if( ins[1] == 'W' ) { *ip = SWP; } else { *ip = SUB; } ip++; break; case 'A': *ip = ADD; ip++; break; case 'M': if( ins[1] == 'U' ) { *ip = MUL; } else { *ip = MOD; } ip++; break; case 'E': *ip = END; execute(); goto start; case 'Q': return 0; } } }