#K8316. Evaluate Reverse Polish Notation Expression
Evaluate Reverse Polish Notation Expression
Evaluate Reverse Polish Notation Expression
You are given an expression in Reverse Polish Notation (RPN), also known as postfix notation. In this notation, every operator follows all of its operands. For example, to add 3 and 4, the expression is written as 3 4 +
rather than 3 + 4
.
Your task is to write a program to evaluate a given RPN expression and output the result as an integer.
The expression will contain integers and the operators +
, -
, *
, and /
. Division should be handled as integer division which truncates towards zero. For example, the evaluation of the expression should adhere to the formula:
\[
\text{result} = \left\lfloor \frac{a}{b} \right\rfloor
\]
where the division is applied after popping the top two numbers from the stack (with the second popped being the left operand).
You may assume that the given expression is always valid.
inputFormat
The input consists of a single line containing a valid postfix expression. The tokens in the expression (operands and operators) are separated by spaces.
outputFormat
Output a single integer which is the result of evaluating the expression.
## sample3 4 +
7