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
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'.