Homework 1, Postfix Calculator and Stack Class.

Short Description:

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

Goals

When you finish this homework, you should have:

Formal Description

The Stack Class

You should start with the header file StackT.h. You may not change this header file, you must implement the class defined in this header file. Your implementation file should be called StackT.cpp or StackT.C. You must correctly implement all methods described in the header file in your implementation file.

If you need to implement any additional routines, DeleteStack or CopyStack for example, these can be placed in the implementation file. They do not need to be members of the stack class. Simply place the function prototypes at the top of the file and implement the functions anywhere in the file. There is no need to modify the header file.

When I grade, I will replace the StackT.h file in your submission folder with a copy of the given StackT.h. If your code does not compile with this header file, you will receive no credit and not be eligable for a redo.

Your stack should be implemented based on a set of dynamic nodes each holding a single data item. Each time an item is added to a stack, a new node should be created. When an item is popped from the stack, the node containing that item should be deleted.

The stack class should contain ALL of the methods required for a class containing dynamic memory. There should be no memory leaks.

There are currently two error conditions the implementor of a stack should detect, but has no control over. An attempt to Pop or Top and empty stack. If either of these conditions occur, your stack should:

This error handling mechanism will change in the future.

The type DataT has been defined to be in integer. Please use DataT and not int for references to the data type stored in the stack. We will replace this later to make a generic stack.

The structure StackNodeT has been declared but not defined in the header file. You should define this in the implementation file.

If you do not understand the operation of a stack, or of any of the methods defined in StackT.h, please ask. If you do not understand any of the abstraction methods (DataT, StackNodeT) discussed above please ask.

To determine if your stack class is working correctly, a test driver has been provided. This is in the file stackTest.cpp. Your stack class should work with this program and should produce no output. Additionally, you should be able to run your program with valgrind as a driver and no memory leaks should be detected.

To make compiling and running your program easier, you should use this Makefile. Just copy it directly, do not retype it, there are places where the tab character is required in a makefile. To use the Makefile, just type make. A tutorial on makefiles can be found here. Please use this makefile.

The Calculator

You will use your stack class to implement a postfix calculator.

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 or operator precedence. 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.

The second part of 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.

Other than the errors listed below, there will be no errors in the data. All numbers will be numbers, all opererators will be from those listed. All items will be seperated by a single space. The line will begin with either a number or an operator and will end with a newline (\n) character.

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 parsing error conditions should be handled.

There are two computational errors that the calculator should handle. 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 

Please note, numbers are not limited to a single digit, and can be proceeded by a + or -. The following are valid:

CMD: -100

Processing -100 = -100

CMD: -100 -100 +

Processing -100 -100 + = -200

CMD: +1234567 +222 /

Processing +1234567 +222 / = 5561

Your program should quit when the quit command is issued.

Testing your Calculator

You should test your calculator thoroughly. It should produce the output as described above or given in the examples. If there is any undefined or ambiguous output please ask for assistance.

Your calculator must work with this testCase input file to produce this testOut output. You can test your program by downloading these two files and typing

calculator < testCase | diff - testOut
If no output is observed, your calculator works on the test case correctly. If there is an error, you will be provided with output that shows I may apply additional test cases when grading.

Other Details

Given Files

Required Files

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

If you wish to communicate anything with your submission, please put this in a file called ReadMe.

Submission

Submit your tar or zip file to the D2L assignment folder by the due date.