Homework 5, Postfix Calculator.

Short Description:

Write a program that will perform calculations given in Postfix or Reverse Polish notation.

This assignment is worth 50 points.

Goals

When you finish this homework, you should have:

Formal Description

Expressions in postfix notation are relatively easy to evaluate. They are in a form where operands (the numbers) are given before the operators (the operations). This differs from infix notation, the notation we normally use, where expressions are normally in the form operand operator operand. Some examples are:
    infix               postfix
    3 + 2               3 2 +
    3 - 2               3 2 -
    3 / 2               3 2 /
A nice feature of postfix notation is that there is no need for parenthesis. The infix expression 4 + 2 * 5 is normally interpreted as 4 + (2*5). This means that the entire expression must parsed and operator precedence must be applied before the expression can be evaluated. In postfix notation the expression would be 4 2 5 * + which can be evaluated immediately.

Evaluating a postfix expression requires a stack. An expression is processed from left to right. If a number is encountered, it is pushed on the stack. If an operator is encountered, the top two elements are popped from the stack, the operator is applied and the result is pushed back onto the stack. When a correct expression is completely processed, the answer should be the only value remaining on the stack.

Consider processing 4 2 5 * +


Expression    Current Element    Stack    Action
4 2 5 * +           4                     Push 4 onto the stack

2 5 * +             2            4        Push 2 onto the stack

5 * +               5            2:4      Push 5 onto the stack

* +                 *            5:2:4    Pop 5 and 2 from the stack
                                          Compute 2*5
                                          Push 10 onto the stack

+                   +            10:4     Pop 10 and 4 from the stack
                                          Compute 4+10
                                          Push 14 onto the stack

empty string        none         14       Pop 14 from the stack,  this is the answer.

Your assignment is to create a calculator capable of parsing and evaluating expressions given in postfix format.

Expressions will contain only integers, operators from the set {+,-,*,/,%,^} or the word quit. All portions of the expression will be separated by a single space and there may be a space after the last item in an expression. The operations are all binary (two argument) and should be evaluated as integer expressions. All results will fit into an integer.

Under normal operations, your program should prompt with CMD: (with a space at the end). There should be no new line following the prompt. The program should

For example, if the expression 3 5 + is to be evaluated, the interaction should appear as follows:
CMD: 3 5 +
Processing 3 5 +
3 5 +  = 8

Two error conditions should be handled.

Please note, I can't think of any way an empty stack can occur after processing an expression unless you process the empty string or a string of all white spaces. You should not do this.

A string with a singe number is valid.

CMD: 1
Processing 1
1 = 1 

Your program should quit when the quit command is issued.

Other Details

Required Files

A single tar file containing the source code and makefile for this program.

Submission

Email your tar file as an attachment to dbennett@edinboro.edu. Make sure that the title indicates that this is homework5 and that your name and section are included in the body of the message.