Evaluate a postfix expression with a stack MEDIUM Liste stive cozi (BAC) 25 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

Read from standard input an **arithmetic expression in postfix notation** (reverse Polish notation), made up of natural numbers and the operators `+`, `-`, `*`, `/`. Tokens (numbers and operators) are separated by single spaces. Compute the value of the expression using a **stack of numbers** implemented on an array with index `top`. The algorithm is: - when you encounter a number, push it onto the stack; - when you encounter an operator, pop two values from the stack (`b` then `a`), compute `a op b`, and push the result back onto the stack. At the end, exactly one value remains on the stack: the result. Do not use STL containers.

Input format

Input input.txt

A single line contains the postfix expression, with tokens separated by spaces. - the expression has at most `5000` characters; - all intermediate numbers and the final result fit in `long long`; - division is integer division between strictly positive numbers; - the expression is always valid (no error checking is required).

Output format

Output output.txt

The program prints to standard output the value of the expression, followed by a newline.

Example

input
3 4 + 2 *
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