UTS Programming Competition 2013 Problem 4

From ProgSoc Wiki

Revision as of 03:53, 4 May 2013 by Tomchristmas (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Problem 4: Reverse Polish Notation Calculator

Problem Description

Basic arithmetic is easy, but every now and then humans make mistakes. The solution to human error is an inhuman machine that given the right input flawlessly returns the correct output. In this case, the input is a collection of equations written in reverse Polish notation. The output should be the solution to each equation, if such a solution exists.

To keep things simple, input numbers are limited to single digits 0-9, and only addition, subtraction, multiplication and division need to be calculated. Reverse Polish notation is when the operators for an equation are written to the right of the numbers.

For example, 3 + 4 would be written 3 4 + in reverse Polish notation. This means there is no need for brackets, as (1 + 2) * 3 is written 1 2 + 3 * and the brackets are already accounted for by the placement of the operator.

(Without brackets 1 + 2 * 3 would be written 1 2 3 * +)

Problem Task

Your job is to write such a calculator that can solve these equations, without the typical mistakes that humans introduce. Your calculator should read each equation on a new line, beginning with the name of the equation to solve, an equal sign, and then the equation itself. The output should be the name of the equation, an equals sign and the result of computing the equation or "ERROR!" if the equation is unsolvable. Discard remainders after division. There shall be a maximum of six operands and five operators for any one equation.

Sample Input

a = 12+
b = 32-
c = 23*
d = 82/
w = 2222+
x = 4321*-+
y = 111-/
z = 000000*****

Sample Output

a = 3
b = 1
c = 6
d = 4
w = ERROR!
x = 5
y = ERROR!
z = 0

Solutions

Put your solutions here!

Personal tools