An RPN calculator computes expressions written in Reverse Polish Notation.
An RPN expression or postfix expression is one of the following :
- a number
X, in wich case the value of the expression is that of X, - a sequence of form
E1 E2 OPwhereE1andE2are RPN expressions andOPis an arithmetic operation.
Samples :
20 5 / => 20/5 = 4
4 2 + 3 - => (4+2)-3 = 3
3 5 8 * 7 + * => 3*((5*8)+7) = 141
7 2 - 3 4 => 5 3 4
Write a program that take an expression (e.g. "20 5 /") and prints the result (e.g. "4")
The following operators should be supported: +, -, /, *
- program can optionally support floating point number
- program can optionally support negative number
- Add the
SQRToperator:
9 SQRT => √9 = 3
- Add the
SWAPoperator:
1 2 3 SWAP => 1 3 2
- Add the
DUPoperator:
2 4 DUP => 2 4 4
- Add the
MAXoperator:
5 3 4 2 9 1 MAX => MAX(5, 3, 4, 2, 9, 1) = 9
4 5 MAX 1 2 MAX * => MAX(4,5) * MAX(1,2) = 10