Loading...
Loading...
Loading...
In postfix form the operator comes after its two numbers: 2 3 4 * + means 2 + 3 × 4. Read a postfix expression with the operators +, - and * and print its value.
Example
Input
5 2 3 4 * +
Output
14
3 × 4 = 12, then 2 + 12 = 14.
Try the problem first. Open one hint at a time.
A number goes on the stack. An operator pops two numbers, applies itself and pushes the result.
Pop the right operand first, then the left operand. The order matters for -.
A token is an operator only if it is exactly +, - or *; -5 is a number.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor