SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Standard Template Library
Exercise:
Programming Projects
Question:13 | ISBN:9780132846813 | Edition: 5

Question

Reverse Polish Notation (RPN) or postfix notation is a format to specify math-

mematical 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


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.