CS111 Project 3: Postfix
Due Wednesday, October 26, at 9:00 AM
Postfix, or Reverse Polish Notation (RPN) is a notation where the operator comes after the operands, not before as it does using infix notation. This is best explained by example:
- Prefix: + 4 5
- Infix: 4 + 5
- Postfix: 4 5 +
Postfix notation does not require parenthesis, but rather uses a stack-based calculation system. Consider the following expressions:
- Infox: (4 + 5) * 3
- Postfix: 3 4 5 + *
In the postfix expression, evaluation happens like this, with the top of the stack drawn at the right:
- 3 is pushed on the stack. Stack: 3
- 4 is pushed on the stack. Stack: 3 4
- 5 is pushed on the stack. Stack: 3 4 5
- + is evaluated. The top two operands are popped off the stack, added, and the result is placed back on the stack. Stack: 3 9
- * is evaluated. The top two operands are popped off the stack, multipled, and the result is placed back on the stack. Stack: 27
For this assignment, you will create a postfix calculator. It must support the basic operators +, -, *, and /, plus pop, which removes the top item from the stack. You can use a list for the stack. There are three types of operation:
- When the user enters a number, this number should be added to the stack, and the stack displayed
- When the user enters an operator, the operation should be performed on the top two operands on the stack.
- When the user enters "pop", the top number should be removed from the stack.
Regarding user input, the user should be presented with a prompt, at which they may enter as many operations as they like, separated by spaces. So the following are all valid input:
- 17.43
- 3 4 + 5 *
- 1 2 3 pop + pop
- 1 2 3 4 5 6 7 8 9 + + + + + + + +
Some method of exiting the program should be provided. How this works is up to you.
When you are finished, upload the program in the usual manner.