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:
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.
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.
CMD: 3 / Processing 3 / Expression error: insufficient operands for /. Processing of 3 / failed. CMD: 3 4 + 5 6 - * ^ Processing 3 4 + 5 6 - * ^ Expression error: insufficient operands for ^. Processing of 3 4 + 5 6 - * ^ failed.If there any portion of the expression remains to be processed after the error is detected, it should be reported as well.
CMD: 3 4 + 5 6 - * ^ / % Processing 3 4 + 5 6 - * ^ / % Expression error: insufficient operands for ^. Unprocessed expression "/ %". Processing of 3 4 + 5 6 - * ^ / % failed.In each case, note that the error detail is proceeded by a tab character.
CMD: 2 3 Processing 2 3 Expression error: 1 unused operands. Processing of 2 3 failed.
4 0 /
is not a legal operation.
4 0 %
is not a legal operation.
CMD: 4 0 / Processing 4 0 / Math error: attempt to divide by 0. Processing of 4 0 / failed. 4 0 % CMD: 4 0 % Processing 4 0 % Math error: attempt to divide by 0. Processing of 4 0 % failed. 4 2 2 - / CMD: 4 2 2 - / Processing 4 2 2 - / Math error: attempt to divide by 0. Processing of 4 2 2 - / failed. 4 2 2 - / 8 * CMD: 4 2 2 - / 8 * Processing 4 2 2 - / Math error: attempt to divide by 0. Unprocessed expression "8 *". Processing of 4 2 2 - / 8 * failed.
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.
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 - testOutIf 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
[bennett@mirkwood me]$ calculator < testCase | diff - testOut 28c28 < Expression error: insufficient operands for ^. Unprocessed Expression "/ %". --- > Expression error: insufficient operands for ^. Unprocessed expression "/ %". 53c53 < Math error: attempt to divide by 0. Unprocessed Expression "8 *". --- > Math error: attempt to divide by 0. Unprocessed expression "8 *".
calculator < inputFileName
calculator > outputFileName
calculator < inputFileName > outputFileName
If you wish to communicate anything with your submission, please put this in a file called ReadMe
.