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:
- Correctly implemented a class which employs dynamic memory.
- Implemented a stack
- Written a program to employ a stack
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
- Read in the expression.
- Print Processing expression on a line.
- Print expression = result on a line.
- Print a newline
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.
- If at any time during a computation, an operator is being processed and there are fewer than 2 operands on the stack, the program should print the error message "Expression error, insufficient operands.", abort the computation and report that processing of the expression failed.
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.
- If after processing the entire expression, the stack has more than one element, report the error "Expression error: n unused operands." where n is the number of arguments remaining.
CMD: 2 3
Processing 2 3
Expression error: 1 unused operands.
Processing of 2 3 failed.
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
- You must implement a stack. Your stack must compile, link and work with an unaltered version of StackTest.C.
- Your stack must be a linked list based implementation, not an array based implementation.
- You must use dynamic memory that you allocate with new and deallocate with free.
- Required Stack Functions
- Push - places an item on the top of the stack.
- Pop - removes and returns the top item on the stack.
- Top - returns, but does not remove the top item on the stack.
- Size - returns the number of items on the stack.
- You should also implement all required functions for a class with dynamic memory.
- Your program must not have any memory leaks and valgrind should report All heap blocks were freed -- no leaks are possible
- You must compile with the compiler flags -g -O3 -Wall -Wextra -Wuninitialized -pedantic -Wshadow -Wunused
- My Makefile. Note my calculator is called postfix.
- There is no redo on this homework.
- You may not use elements of the Standard Container Class.
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.