HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Volumes > Data Structures > problem:


50291 - Postfix Arithmetic Expressions

Guest
• Discussion of problem (1)

Volume problems

• 50692 - How much money?
• 50790 - Fire Alarm
• 50710 - Permutations
• 50709 - k-ary Strings
• 50708 - Binary Strings
• 50694 - The Cheapest Flight
• 50996 - Checkers - the shortest path
• 50770 - Average Depth
• 50291 - Postfix Arithmetic Expr...
• 50775 - Balanced Parenthesis
• 50736 - Top N Donors - 2
• 50796 - KNN
• 50340 - Game 19
• 50735 - Top M Products
• 50697 - Base Stations
• 50383 - Noisy Mornings
• 50479 - Bit Compressor

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