Untitled
unknown
plain_text
2 years ago
3.5 kB
1
Indexable
Never
#include "Scanner.h" #include "Stack.h" // GENERIC STACK #include <iostream> using namespace std; int main () { Scanner S(cin); Token t; Stack<Token> numstack, opstack; // 2x Stacks of type Token t = S.getnext(); while (!(t.tt == eof && opstack.isEmpty())) { cout << t.tt; if (t.tt == integer) { numstack.push(t); t = S.getnext(); } else if (t.tt == lptok){ opstack.push(t); t = S.getnext(); } else if (t.tt == rptok){ if(opstack.peek().tt == lptok){ Token temp = opstack.pop(); t = S.getnext(); }else{ Token ans; ans.tt = integer; Token num2 = numstack.pop(); Token num1 = numstack.pop(); Token op = opstack.pop(); if(op.tt == pltok){ ans.val = num1.val + num2.val; }else if(op.tt == mitok){ ans.val = num1.val - num2.val; }else if(op.tt == asttok){ ans.val = num1.val * num2.val; }else if(op.tt == slashtok){ ans.val = num1.val / num2.val; } ans.text = to_string(ans.val); numstack.push(ans); } } else if (t.tt == pltok || t.tt == mitok || t.tt == eof) { if(!opstack.isEmpty()){ Token temp = opstack.peek(); if(temp.tt == pltok || temp.tt == mitok || temp.tt == asttok || temp.tt == slashtok){ Token ans; ans.tt = integer; Token num2 = numstack.pop(); Token num1 = numstack.pop(); Token op = opstack.pop(); if(op.tt == pltok){ ans.val = num1.val + num2.val; }else if(op.tt == mitok){ ans.val = num1.val - num2.val; }else if(op.tt == asttok){ ans.val = num1.val * num2.val; }else if(op.tt == slashtok){ ans.val = num1.val / num2.val; } ans.text = to_string(ans.val); numstack.push(ans); } } else { if(t.tt != eof){ opstack.push(t); t = S.getnext(); } } } else if(t.tt == asttok || t.tt == slashtok){ if(!opstack.isEmpty()){ Token temp = opstack.peek(); if(temp.tt == asttok || temp.tt == slashtok){ Token ans; ans.tt = integer; Token num2 = numstack.pop(); Token num1 = numstack.pop(); Token op = opstack.pop(); if(op.tt == asttok){ ans.val = num1.val * num2.val; }else if(op.tt == slashtok){ ans.val = num1.val / num2.val; } ans.text = to_string(ans.val); numstack.push(ans); } } else { opstack.push(t); t = S.getnext(); } } } cout << numstack.peek().val; return 0; }