HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Sections > Linear Data structures: Stacks, Queues, Linked Lists, etc > problem:


50291 - Postfix Arithmetic Expressions

Guest
• Discussion of problem (1)

Section problems

• 50262 - Brackets
• 50291 - Postfix Arithmetic Expr...
• 50286 - Remove Duplicates
• 50775 - Balanced Parenthesis
• 50340 - Game 19
• 50877 - Friendly Queue
• 51086 - Top popular student
• 50774 - Hot Potato
• 50777 - Dwarfs Maze
• 50781 - ReversesreveR

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Prepared by Ibrahim Mesecan.

Postfix Arithmetic Expressions

In computer science arithmetic expressions can be written in three different styles.

  1. Infix e.g. (3*6) + (4*7) = 46
  2. Prefix e.g. + * 3 6 * 4 7 = 46
  3. Postfix e.g. 3 6 * 4 7 * + = 46

Question: Write a program that is going to read a post fix expression and then evaluate and show the result on screen.
Note: Use Stacks to solve the problem.

Input specification
A postfix arithmetic expression will be given ending with an equal operator (=).
The expression may contain only numbers and 5 arithmetic operators: '+', '-', '*', '/', and '='.
The numbers will be preceded by '#'. Operators and operands are separated by spaces.
There are at most 10 consecutive numbers in the given expression.

Output specification
Calculate and show one integer: the result of postfix expression.

Sample Input I
  #6 #7 #12 #6 / + * #21 - =
Sample Input II
  #3 #5 #6 * + #13 - #18 #2 / + =
Sample Output I
  33
Sample Output II
  29

Output Explanation :
The expression given in the first sample can be written as:    6 * (7 + (12 / 6)) - 21 = 33


Для отправки решений необходимо выполнить вход.

www.contester.ru