#C12378. Reverse Polish Notation Calculator
Reverse Polish Notation Calculator
Reverse Polish Notation Calculator
This problem involves implementing a simplified stack-based calculator which uses Reverse Polish Notation (RPN). In RPN, every operator follows all of its operands. For example, to add 3 and 4, one would write 3 4 + rather than 3 + 4. This eliminates the need for parentheses to define order of operations.
Your task is to implement an RPN calculator that supports the four basic arithmetic operators: addition (+), subtraction (-), multiplication (*) and division (/). Note that division should perform integer division with truncation toward zero. For example, the equation
\(\frac{-7}{3} = -2\)
should evaluate to -2
.
inputFormat
The input will be provided as a single line read from stdin. The line contains space-separated tokens. Each token is either an integer or one of the four operators: +
, -
, *
, or /
.
For example: 2 1 + 3 *
outputFormat
Output a single integer that is the result of evaluating the RPN expression, printed to stdout.
## sample2 1 + 3 *
9