Create a program that functions as a Reverse Polish Notation calculator.
What is Reverse Polish Notation?
RPN is a way to write mathematical expressions, where the operation occurs after its operands. For example, instead of "2 x 3", in RPN you would write "2 3 x". This has the advantage that no parentheses or precedence rules such as "Please Excuse My Dear Aunt Sally" are required; expressions are unambiguous.
RPN expressions are evaluated with a stack, roughly like so:
- If the next input token is a number, push the number onto the stack.
- If the next input token is an operation, and n is the number of operands:
- Pop (remove from the top) n numbers from the stack
- Perform the operation on these numbers
- Push the result onto the the stack.
- Repeat until you're out of input tokens
An RPN calculator works in a similar way.
- If the user inputs a number, and then presses [Enter] or any operation, push the number onto the stack
- If the user presses an operation that takes n numbers
- Pop n numbers from the stack
- Perform the operation on the numbers
- Push the result back onto the stack
So the following key presses: "2 Enter 5 Enter 6 Multiply Add" would first add 2 to the stack, then add 5 to the stack, then add 6 to the stack. Then it would pop 5 and 6 from the stack, multiply them, and push 30 back onto the stack. Then it would add 2 and 30, and you'd be left with 32 on the stack.
- Handles real numbers
- Supports the following binary operations: plus, minus, times, division, exponentiation
- Supports the following unary operations: natural log, second power, reciprocal, sin, cos, tan
- Supports stack size of at least 14
More challenging, and therefore, awesome:
- Supports some sort of menu that allows access to more operations
- More operations
- Ability to pop the stack
- Storing values and retrieving them from variables
- Backspace, in the numerical input
The main focus here is ease of use. Have fun!