HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Sections > Linear Data Structures: Arrays > problem:


51119 - Evaluating Prefix expressions

Guest
• Review clarifications (1)

Section problems

• 51145 - Graduation Exam
• 51117 - The Most Crowded
• 51146 - Popular Baby Names
• 51131 - Running After a Thief
• 51143 - Departments Competition
• 51132 - Problem Solving Competition
• 51118 - Place 7 to the 5th position
• 51121 - Complexity power of a nu...
• 51119 - Evaluating Prefix expr...
• 50541 - Binary to decimal
• 50563 - Long Modulus
• 50567 - Input Data Normalization
• 50592 - Matrix Multiplication
• 50594 - Transformations
• 50606 - Long product
• 50607 - Long sum
• 50611 - Maximum of the minima

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.
Question by Ibrahim Mesecan.

Evaluating Prefix expressions

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

  • Infix e.g. (3*6) + (4*7) = 46
  • Prefix e.g. + * 3 6 * 4 7 = 46 (first operator, then the numbers come)
  • Postfix e.g. 3 6 * 4 7 * + = 46 (first operands, then the operators come)
Question:Write a program that is going to read a prefix expression and then evaluate and show the result on screen.

Input specification: A prefix 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 40 consecutive numbers in the given expression.

Output specification: Calculate and show the result on screen.

Sample Input I Sample Input II
+ * #3 #6 * #4 #7 =
- * / #15 - #8 + #1 #1 #3 + #2 + #1 #1 =
Sample Output I Sample Output II
46 3.5

Explanation: The expression given in the second sample can be written as: ((15 / (8 - (1 + 1))) * 3) - (2 + (1 + 1)) = 3.5



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

www.contester.ru