Lab Assignment #10 Solution



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

Write a program that evaluates an arithmetic expression in postfix 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 the value of the expression (the answer).

Some important hints:

  • In the files calc_useful.h and I have provided a couple of functions that make this whole lab a lot easier.

  • I have also written enough of the main to show you how to do the input for the expression. It is called

  • This lab does not involve writing a class – only completing the necessary stack operations in the main

The calculator should be embedded in a loop that runs until the user chooses to quit.

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.

Submit to Blackboard that script and the source code for your program.