RPN Calculator
Introduction
In reverse Polish notation, the operators follow their operands; for instance, to add 3 and 4 together, one would write 3 4 + rather than 3 + 4. If there are multiple operations, operators are given immediately after their second operands; so the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in reverse Polish notation: 4 is first subtracted from 3, then 5 is added to it. An advantage of reverse Polish notation is that it removes the need for parentheses that are required by infix notation. While 3 − 4 × 5 can also be written 3 − (4 × 5), that means something quite different from (3 − 4) × 5. In reverse Polish notation, the former could be written 3 4 5 × −, which unambiguously means 3 (4 5 ×) − which reduces to 3 20 − (which can further be reduced to -17); the latter could be written 3 4 − 5 × (or 5 3 4 − ×, if keeping similar formatting), which unambiguously means (3 4 −) 5 ×.
Table of contents
Prerequisites
Running, building and testing
This package contains a Makefile
which has three commands build
, clean
and test
.
Running the calculator
The calculator can be run in multiple ways, when running it from a produced binary use the provided rpn-calculator
binary. For development you can run go run main.go
with additional arguments.
Interactive
Run rpn-calculator -i
or go run main.go -i
to run in interactive shell mode
Run rpn-calculator 1 2 "*" 4 \
or go run main.go 1 2 "*" 4 \
to calculate the given input, beware to quote shell arguments like *
Releasing
To create a new release just create a new tag
and push this to Github. The Github actions will prepare a new release.
Requirements
- The calculator has a stack that can contain real numbers.
- The calculator waits for user input and expects to receive strings containing whitespace separated lists of numbers and operators.
- Numbers are pushed on to the stack. Operators operate on numbers that are on the stack. • Available operators are +, -, *, /, sqrt, undo, clear.
- Operators pop their parameters off the stack, and push their results back onto the stack.
- The
clear
operator removes all items from the stack.
- The
undo
operator undoes the previous operation. undo undo
will undo the previo us two operations.
- sqrt performs a square root on the top item from the stack.
- The
+
, -
, *
, /
operators perform addition, subtraction, multiplication and division respectively on the top two items from the stack.
- After processing an input string, the calculator displays the current contents of the stack as a space-separated list.
- Numbers should be stored on the stack to at least 15 decimal places of precision, but displayed to 10 decimal places (or less if it causes no loss of precision).
- All numbers should be formatted as plain decimal strings (ie. no engineering formatting).
- If an operator cannot find a sufficient number of parameters on the stack, a warning is displayed: operator (position: ): insufficient parameters
- After displaying the warning, all further processing of the string terminates and the current state of the stack is displayed.
Addendum
- The interactive console has been adapted with an improved interface allowing to easily calculate with speed.