Homework 7, A Fractional Calculator

For this program you will implement a postfix notation calculator for rational numbers.

Goals

Formal Description

Implement a postfix notation calculator which performs calculations on rational numbers. Postfix notation, which is also called Reverse Polish Notation or RPN, is a way to write expressions so no parentheses are required. Evaluating RPN expressions involves a stack.

The expression 3 4 + is equivalent to 3 + 4. This expression would be evaluated parsing left to right as follows:

 Start with an empty stack.
 Read the number 3
    Push it on the stack
 Read the number 4
    Push it on the stack
 Read the operator +
    o2 = Pop()
    o1 = Pop()
    Push (o1+o2)
Wikipedia currently has the example : ((15 / (7 - (1 + 1))) × 3) - (2 + (1 + 1)) which is 15 7 1 1 + - / 3 × 2 1 1 + + - in RPN.

Note that the expression 3 6 / produces 1/2, and 3 6 - produces -3.

You are to implement a RPN calculator which performs calculations on rational numbers.

The algorithm for evaluating an expression in RPN is

for each token in the expression
   if the token (op)  is an operator (+,-,*,/)
      arg2 = Pop()
      arg1 = Pop()
      result =  arg1 op arg2
      Push(result)
   else if the token is a rational number
      Push(token)
   else
      Print error
      Exit algorithm

Prompt

Your program should prompt the user with: Initially the stack is empty so the prompt should be:
(0) % 
If the command 3 2 - is executed, the stack will contain a single item so the prompt should be
(1) % 

Commands

Your program should execute the following commands: If no command is given, the calculator should assume the input is an expression and attempt to evaluate it. If there are no errors, the calculator should print a blank line and return.

The following is an example session using the calculator

[bennett@mirkwood hw7]$ Calculator
(0) % 3 6 /

(1) % print

1/2
(1) % fprint

0.5
(1) % 3 6 -

(2) % print

-3
(2) % 15 7 1 1 + - / 3 * 2 1 1 + + -

(3) % print

5
(3) % dump

1/2 -3 5
(3) % clear

(0) % off

[bennett@mirkwood hw7]$

Errors

You may encounter the following errors: In any case, your calculator should continue to operate after an error. It should not exit.
[bennett@mirkwood hw7]$ Calculator
(0) % 3 +

Error: Insufficient Stack for Add.
(1) % 1 2 3 4 5 6 7 8 9 10

Error: Stack Overflow.
(10) % bob

Error: Invalid Input bob.
(10) % 1a/b

Error: Invalid Input 1a/b.
(10) %

Required Classes

You must implement the following classes:

When implementing the rational number class, the Euclidean Algorithm might be useful. This algorithm will compute the greatest common divisor (GCD) of any two integers very efficiently. The proof of this algorithm is given in Discrete Math, but you can use it right now

GCD (a,b)
   if a < b 
      return GCD(b,a)
   if b is 0
      return a
   else
      return GCD(b, a%b)

Testing

Discussion

Required Files

A tar file containing

Submission

Send the tar file to your instructor as an attachment to an email message by the due date.