#include #include #include #include #include #include #include using namespace std; enum OPcode { NUM, POP, INV, DUP, SWP, ADD, SUB, MUL, DIV, MOD }; struct Instr { OPcode op; int arg; }; vector instrQueue; vector st; void pushInstr(string instrStr) { Instr newInstr; if (instrStr/*.substr(0,3)*/ == "NUM") { newInstr.op = NUM; //newInstr.arg = atoi(instrStr.substr(4).c_str()); cin >> newInstr.arg; } else if (instrStr == "POP") { newInstr.op = POP; } else if (instrStr == "INV") { newInstr.op = INV; } else if (instrStr == "DUP") { newInstr.op = DUP; } else if (instrStr == "SWP") { newInstr.op = SWP; } else if (instrStr == "ADD") { newInstr.op = ADD; } else if (instrStr == "SUB") { newInstr.op = SUB; } else if (instrStr == "MUL") { newInstr.op = MUL; } else if (instrStr == "DIV") { newInstr.op = DIV; } else if (instrStr == "MOD") { newInstr.op = MOD; } instrQueue.push_back(newInstr); } void errorFnc() { printf("ERROR\n"); } void execute() { vector::iterator instrIt = instrQueue.begin(); vector::iterator instrItEnd = instrQueue.end(); OPcode op; for ( ; instrIt != instrItEnd ; instrIt++) { op = (*instrIt).op; if (op == NUM) { st.push_back((*instrIt).arg); } else if (op == POP) { if (!st.empty()) st.pop_back(); else { errorFnc(); return; } } else if (op == INV) { if (!st.empty()) { st[st.size()-1] *= -1; } else { errorFnc(); return; } } else if (op == DUP) { if (!st.empty()) st.push_back(st.back()); else { errorFnc(); return; } } else if (op == SWP) { if (st.size() >=2) { int tmp = st[st.size()-1]; st[st.size()-1] = st[st.size()-2]; st[st.size()-2] = tmp; } else { errorFnc(); return; } } else if (op == ADD) { if (st.size() >=2) { long long tmp = (long long)st[st.size()-2] + (long long)st[st.size()-1]; if (tmp <= 1000000000 && tmp >= -1000000000) { st[st.size()-2] += st[st.size()-1]; st.pop_back(); } else { errorFnc(); return; } } else { errorFnc(); return; } } else if (op == SUB) { if (st.size() >=2) { long long tmp = (long long)st[st.size()-2] - (long long)st[st.size()-1]; if (tmp <= 1000000000 && tmp >= -1000000000) { st[st.size()-2] -= st[st.size()-1]; st.pop_back(); } else { errorFnc(); return; } } else { errorFnc(); return; } } else if (op == MUL) { if (st.size() >=2) { /*int radA = 0; // top int radB = 0; // top -1 int tmpA = abs(st[st.size()-1]); int tmpB = abs(st[st.size()-2]); while (tmpA > 0) { tmpA /= 10; radA++; } while (tmpB > 0) { tmpB /= 10; radB++; } int tmpAll = radA + radB -2; if (tmpAll > 9) { errorFnc(); return; } if (tmpAll == 9) { long long tmp = (long long)st[st.size()-1] * (long long)st[st.size()-2]; if (tmp > 1000000000 || tmp < -1000000000) { errorFnc(); return; } }*/ double tmp = (double)st[st.size()-2] * (double)st[st.size()-1]; if (tmp <= 1000000000 && tmp >= -1000000000) { st[st.size()-2] *= st[st.size()-1]; st.pop_back(); } else { errorFnc(); return; } /*st[st.size()-2] *= st[st.size()-1]; st.pop_back();*/ } else { errorFnc(); return; } } else if (op == DIV) { if (st.size() >=2) { if (st[st.size()-1] == 0) { errorFnc(); return; } st[st.size()-2] /= st[st.size()-1]; st.pop_back(); /* bool invert = false; if ((st[st.size()-2] < 0 && (st[st.size()-1] >=0 )) || (st[st.size()-2] >= 0 && (st[st.size()-1] < 0))) { invert = true; } int a = abs(st[st.size()-2]); int b = abs(st[st.size()-1]); int result = a%b; if (invert) result *= -1; st[st.size()-2] = result; st.pop_back();*/ } else { errorFnc(); return; } } else if (op == MOD) { if (st.size() >=2) { if (st[st.size()-1] == 0) { errorFnc(); return; } bool inv = st[st.size()-2] < 0; st[st.size()-2] = abs(st[st.size()-2]) % abs(st[st.size()-1]); if (inv) st[st.size()-2] *= -1; st.pop_back(); } else { errorFnc(); return; } } } if (st.size() != 1) { errorFnc(); return; } printf("%d\n", st[0]); } int main() { instrQueue.reserve(100*1000); st.reserve(1000); string instructStr; // getline(cin, instructStr); cin >> instructStr; while(instructStr != "QUIT") { while(instructStr != "END") { pushInstr(instructStr); //getline(cin, instructStr); cin >> instructStr; } int numExec; cin >> numExec; int value; for (int i = 0 ; i < numExec ; ++i) { st.clear(); cin >> value; st.push_back(value); execute(); } instrQueue.clear(); printf("\n"); while(cin.get() != '\n'); //getline(cin, instructStr); cin >> instructStr; } return 0; }