Question: C++ USE GETLINE. For this lab you are to use the STL stack which you can obtain by putting #include at the top of your

C++ USE GETLINE.

For this lab you are to use the STL stack which you can obtain by putting #include at the top of your program, and having the std namespace open, of course.

Write a program that evaluates an arithmetic expression in postfix (RPN) notation. The basic algorithm is to use a single stack of integers. If, when parsing the input you encounter a number you push the number onto the stack. If you encounter an operator (+, -, *, / are the only ones we are working with) you apply the operator to the top two elements on the stack. The result is then pushed back onto the stack. At the end of the whole operation there should be a single number on the stack which is a single number, the value of the expression.

In the files calc_useful.h and calc_useful.cc, I have given you a couple of simple functions, one which identifies if the char it receives is or is not an operator, and the other which evaluates two operands when given a character that is an operator. I have also written a main to help you with parsing the input.

The calculator should be embedded in a loop in the main that runs until the user chooses to quit. (Everything that you write for this assignment can be done in the main, just #include calc_useful.h and compile both .cc files.)

Use your calculator to evaluate:

12 679 5 + 4 * 8 / + (Answer = 354)

34 6 + 16 * 18 (Answer = 622)

12 6 8 + 7 * + (Should produce an error)

56 78 84 + 33 * (Should produce an error)

Plus: Two problems that you make up on your own. These should each have a minimum of four operands.

Run a script of the above interaction including the two problems that you made up on your own.

calc_useful.h--------------------

double evaluate(double num1, double num2, char op); int evaluate(int num1, int num2, char op); bool isop(char op); 

calc_useful.cc------------------------

bool isop(char op){ return op =='+' || op == '-' || op == '*' || op == '/'; } int evaluate(int num1, int num2, char op){ if(op == '+') return num1 + num2; if(op == '-') return num1-num2; if(op == '*') return num1*num2; if(op == '/') return num1/num2; else return 0; } double evaluate(double num1, double num2, char op){ if(op == '+') return num1 + num2; if(op == '-') return num1-num2; if(op == '*') return num1*num2; if(op == '/') return num1/num2; else return 0; } 

stackmain.cc----------------------------------------

#include  #include  #include "calc_useful.h" using namespace std; int main(){ // declare your stack here char c; int onenum, twonum; cout<<"Please enter your expression: "; c = cin.get(); // priming read for the sentinel loop. while(c != ' '){ if(isdigit(c)){ cin.putback(c); cin>>onenum; // stack operation here. } else if(isop(c)){ // if the stack is empty here you have an error. // here is where you have to pop a couple of numbers, // apply the operator to the numbers // and then push the result back into the stack } c = cin.get(); // reading at the bottom of the sentinel loop } // this is where you get your final answer off the stack // it should be the only number left on the stack at this point if(! nums.empty()){ cout<<"Error. Insufficient operators for operands. "; return -1; } cout<<"The answer is: "<< onenum<                        

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!