#include #include #include #include #include #include #include #include using namespace std; typedef struct { int c; int p; } command; #define BUFLEN 1024 vector prog; void solve( int input ); int main(int argc, char *argv[]) { argc = argc; argv = argv; bool quit = true; char buffer[BUFLEN]; while(quit) { bool end = false; while( true ) { string s; fgets(buffer, BUFLEN, stdin); s = buffer; if ( buffer[0] == 'Q') { return 0; } else { command com; if (strlen(buffer) < 3) continue; s = s.substr(0, 3); if ( s.compare( "NUM" ) == 0) { com.c = 0; sscanf(buffer +3 , "%d", &com.p); } else if ( s.compare("POP" ) == 0 ) com.c = 1; else if ( s.compare("INV" ) == 0 ) com.c = 2; else if ( s.compare("DUP" ) == 0 ) com.c = 3; else if ( s.compare("SWP" ) == 0 ) com.c = 4; else if ( s.compare("ADD" ) == 0 ) com.c = 5; else if ( s.compare("SUB" ) == 0 ) com.c = 6; else if ( s.compare("MUL" ) == 0 ) com.c = 7; else if ( s.compare("DIV" ) == 0 ) com.c = 8; else if ( s.compare("MOD" ) == 0 ) com.c = 9; else if ( s.compare("END" ) == 0 ) { break; } prog.push_back( com ); } } int aux = 0; fgets(buffer, BUFLEN, stdin); sscanf(buffer, "%d", &aux); for ( int i = 0; i < aux; i++ ) { int in; fgets(buffer, BUFLEN, stdin); sscanf( buffer, "%d", &in); solve( in ); } fgets(buffer, BUFLEN, stdin); printf("\n"); prog.clear(); } return 0; } void solve( int input ) { stack memory; memory.push( input ); for ( unsigned int i = 0; i < prog.size(); i++ ) { switch( prog[i].c ) { case 0 : memory.push( prog[i].p );break; case 1: memory.pop(); break; case 2: { long long aux = memory.top(); memory.pop(); memory.push( aux * ( -1 ) ); }break; case 3: memory.push( memory.top() ); break; case 4: { if ( memory.size() < 2 ) { printf("ERROR\n" ); return; } long long aux = memory.top(); memory.pop(); long long aux2 = memory.top(); memory.pop(); memory.push( aux ); memory.push( aux2); }break; case 5: { if ( memory.size() < 2 ) { printf("ERROR" ); return; } long long aux = memory.top(); memory.pop(); long long aux2 = memory.top(); memory.pop(); if ( llabs( aux + aux2 ) > 1000000000 ) { printf("ERROR\n" ); return; } else memory.push( aux + aux2 ); }break; case 6: { if ( memory.size() < 2 ) { printf("ERROR\n" ); return; } long long aux = memory.top(); memory.pop(); long long aux2 = memory.top(); memory.pop(); if ( llabs( aux - aux2 ) > 1000000000 ) { printf("ERROR\n" ); return; } else memory.push( aux - aux2 ); }break; case 7: { if ( memory.size() < 2 ) { printf("ERROR" ); return; } long long aux = memory.top(); memory.pop(); long long aux2 = memory.top(); memory.pop(); if ( llabs( aux * aux2 ) > 1000000000 ) { printf("ERROR\n" ); return; } else memory.push( aux * aux2 ); }break; case 8: { if ( memory.size() < 2 ) { printf("ERROR\n" ); return; } long long aux = memory.top(); memory.pop(); long long aux2 = memory.top(); memory.pop(); if ( ( aux2== 0 ) || (llabs( aux / aux2 ) > 1000000000 ) ) { printf("ERROR\n" ); return; } else memory.push( aux / aux2 ); }break; case 9: { if ( memory.size() < 2 ) { printf("ERROR\n" ); return; } long long aux = memory.top(); memory.pop(); long long aux2 = memory.top(); memory.pop(); if ( llabs( aux % aux2 ) > 1000000000 ) { printf("ERROR\n" ); return; } else memory.push( aux % aux2 ); }break; } } if ( memory.size() > 1 ) { printf("ERROR\n" ); return; } else printf("%d\n", memory.top() ); }