Reverse Polish Calculator
Stack |
|
|||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||
Workings for the last operation
Reverse Polish Calcuations
Reverse Polish Notation
Infix notation is the standard way of expressing a calculation. It uses brackets and BIDMAS to define the order that each part of the expression is calculated.
RPN does not need to use brackets or define the presidence that different operations take. The position of the operators in a RPN expression define the presidence with which they are performed
Use of a Stack
A Reverse Polish calculator uses a stack.
- Numerical values are pushed onto the top of the stack
- When an operation is specific, the top two values on the stack are popped off
- The operation is performed between these two values and the result is pusshed onto the top of the stack
Example
Infix Expression: (1 + 2 × 3) × (4 + 5)
Steps used to convert an infix expression into an expression using Reverse Polish Notation
| Action | Stack | Expression |
|---|---|---|
| Push 1 onto the stack | 1 | 1 |
| Push 2 onto the stack | 1 2 | 1 2 |
| Push 3 onto the stack | 1 2 3 | 1 2 3 |
| Use the × operator to multiply 2 by 3 and give the result 6 | 1 | 1 2 3 × |
| Push 6 onto the stack | 1 5 | 1 2 3 × |
| Use the +; operator to add 1 to 6 and give the result 7 | 7 | 1 2 3 × + |
| Push 7 onto the stack | 7 | 1 2 3 × + |
| Push 4 onto the stack | 7 4 | 1 2 3 × + 4 |
| Push 5 onto the stack | 7 4 4 | 1 2 3 × + 4 5 |
| Use the + operator to add 4 to 5 and give the result 9 | 7 | 1 2 3 × + 4 5 + |
| Use the × operator to multiply 7 by 9 and give the result 63 | 1 2 3 × + 4 5 + × |
