Evaluate postfix expression MEDIUM Expresii (BAC) 25 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

The program reads from standard input an expression written in **postfix notation** (also known as *Reverse Polish Notation* or *RPN*). In this notation, the operator is written **after** its two operands, and tokens are separated by single spaces. For example, the infix expression `(10 - 2) * 3` is written in postfix as `10 2 - 3 *`. The program evaluates the expression and prints its value. Supported operators are `+`, `-`, `*`, and `/` (integer division, as in C++).

Input format

Input input.txt

The program reads from standard input a single line containing the postfix expression. Numbers are non-negative integers (at most 9 digits) and are separated from operators by spaces. - The expression is well-formed; no division by zero occurs. - The number of tokens is at most 200. - All intermediate values and the result fit in `long long`. - For a binary operator `op`, the two tokens at the top of the stack are its operands: the first popped is the right operand.

Output format

Output output.txt

The program prints to standard output a single integer: the value of the expression.

Example

input
2 3 4 * +
output
14

Ready to solve this challenge?

Create a free account to write code, submit solutions, and track your progress.

⌨️ Keyboard Shortcuts

Code Editor
Run Code
Ctrl Enter
Submit Code
Ctrl Shift Enter
Format Code
Shift Alt F
Toggle Comment
Ctrl /
Undo
Ctrl Z
Redo
Ctrl Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc

🔍 Interactive Debugger

0 / 0

Analyzing your code...

📦 Variables

No variables yet

📚 Call Stack
main() line 1
📤 Output
We use cookies

Essential cookies are always active. You can choose to enable preference and analytics cookies. Learn more