#include #include"stack.h" using namespace std; int main() { Stack s; char c; float v1, v2, v3; bool done=false; // The loop below reads the input, one operator at a time, // and carries out the operation depending on the operator. // It stops upon receiving 'q' or if the read fails. while (!done) { if (!(cin >> c)) { cout << "Error: cannot read input\n"; break; } switch (c) { case '?': if (!(cin >> v1)) { // Read the float input cout << "Error: cannot read float\n"; // cin failed to read the float done = true; // so we stop } else if (!s.Push(v1)) cout<<"Stack Overlow!\n"; break; case '+': // Handle all the arithmetic operators similarly case '-': case '*': case '/': if(!s.Pop(v1)) { cout<<"Stack Empty!\n"; break; } if(!s.Pop(v2)) { s.Push(v1); cout<<"Stack Has Only One Entry!\n"; break; } // We already know c is an arithmetic operator; // v1 and v2 are the operands // now perform the operation and put the result in v3 switch (c) { case '+': v3=v2+v1; break; case '-': v3=v2-v1; break; case '/': v3=v2/v1; break; case '*': v3=v2*v1; break; } if (!s.Push(v3)) cout<<"Stack Overlow!\n"; break; case '=': // Print the top of the stack by pop-print-push if(!s.Pop(v1)) { cout<<"Stack Empty!\n"; break; } cout << v1 << "\n"; if(!s.Push(v1)) cout<<"Stack Overlow!\n"; break; case 'p': // Pop the top if(!s.Pop(v1)) cout<<"Stack Empty!\n"; break; case 'q': // Quit done=true; break; default: cout<<"Invalid Entry!\n"; break; } } return 0; }