23.3 (cont.) - stack Example
Reverse Polish Notation (RPN, or postfix) calculator
- Arguments written before operators
- 3 + 4 * 7 would be 3 4 7 * +
- Calculation is easy:
- Each argument read is pushed onto a stack
- If a (binary) operator is read:
- Pop top two values
- Perform appropriate operation
- Push result back onto stack
- Add 2 commands:
- p - print the top of the stack
- q - quit the program
prev
|top
|next