This TypeScript Reverse Polish Notation (RPN) calculator is a command-line application that evaluates arithmetic expressions in RPN format. The calculator will prompt for input. Enter a space-separated list of numbers and operators in RPN format, and press enter to see the result.
To exit the calculator, enter q
or ctrl-d
.
Currently supported operators are + - * /
e.g.
❯ npm start
> [email protected] start
> tsc && node dist/app.js
> 5 5 5 8 + + -
-13
> 13 +
0
>
-
Install dependencies:
npm install
-
Run the calculator:
npm start
-
Run tests:
npm test
- Computation of a given string expression is handled in calculator.ts separate from the input processing logic. This should allow for alternative interfaces e.g. WebSocket, file, or TCP socket to reuse the same functionality.
- The calculator is fixing the result of each operand to 6 decimal places, in certain expressions this could result in a loss of precision. If the results of the calculator needed to be highly accurate e.g. for handling money calculations it would worth incorporating a library to perform the calculations e.g. decimal.js