Problem

Reverse Polish Notation (RPN) or postfix notation is a format to specify mathematical expr...

Reverse Polish Notation (RPN) or postfix notation is a format to specify mathematical expressions. In RPN the operator comes after the operands instead of the more common format in which the operator is between the operands (this is called infix notation). Starting with an empty stack, a RPN calculator can be implemented with the following rules:

• If a number is input, push it on the stack.

• If + is input, then pop the last two operands off the stack, add them, and push the result on the stack.

• If - is input, then pop value1, pop value2, then push value2 - value1 on the stack.

• If * is input, then pop the last two operands off the stack, multiply them, and push the result on the stack.

• If / is input, then pop value1, pop value2, then push value2 / value1 on the stack.

• If q is input, then stop inputting values, print out the top of the stack, and exit the program.

Use the stack template class to implement a RPN calculator. Output an appropriate error message if there are not two operands on the stack when given an operator. Here is sample input and output that is equivalent to ((10 − (2 + 3))*2)/5:

10

2

3

+

2

*

5

/

q

The top of the stack is: 2

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 19