This is a simple RPN (Reverse Polish Notation) calculator implemented in TypeScript. It uses Node.js's readline
module to read input from the console and evaluate expressions in RPN. I chose readline
for its ability to read data from a Readable
stream one line at a time. readline
can also be included into a web socket interface such as socket.io or a TCP client (if applicable). For now it is architectured to be used locally with a single user.
- Clone or download the repository
- Install Node.js and NPM if not already installed on your system
- Navigate to the project directory in your terminal
- Run
npm install
to install the project dependencies
To start the RPN calculator, run npm start
in your terminal. You will be prompted to enter an RPN expression to evaluate.
Valid operators include +
, -
, *
, and /
. The calculator supports decimal numbers.
Example expressions:
2 3 +
evaluates to5
10 5 /
evaluates to2
3 4 2 * 1 5 - 2 3 2 5 4 * /
evaluates to0.1
Expressions can also be added from multiple prompts:
> 5
5
> 9
9
> 1
1
> -
8
> /
0.625
If an invalid operator is encountered, the calculator will log an error message and stop evaluating the expression.
If the expression is invalid or incomplete, the calculator will log an error message and stop evaluating the expression.
To exit the calculator, press Ctrl+c
, Ctrl+d
, or q
in your terminal.
The evaluateRPN
function takes an RPN expression as a string and evaluates it using a stack data structure. The function splits the input string into an array of tokens, and iterates over the tokens, pushing numbers onto the stack and performing arithmetic operations on the top two elements of the stack when an operator is encountered.
I chose this algorithm as it is simple, efficient, and can evaluate RPN expressions of arbitrary length and complexity. Functions such as isOperator
and performOperation
are broken down and passed into evaluateRPN
to better understand the underlying process. Operators
are their own seperate const
for ease of adding future operations if applicable.
If the operation is invalid, the function logs an error message. If the expression is valid, the function returns the result of the evaluation.
The RPN calculator logs the following error messages when an error occurs:
"Invalid operator: ${token}"
: when an invalid operator is encountered in the input expression"Cannot divide by 0"
: when expression is trying to divide by 0"Invalid inputs"
: when too many operators are presented
- Support for more operators, such as
^
(exponentiation) and%
(modulus) - Currently extra spaces (' ') entered will return an invalid operator. Future improvements could be added to prevent error messages and continue to evaluate the operation
- An introductory title could be added when started
- Further tests in
app.ts
by mockingreadline
commands, as well as create individual tests for non-exported functions inutil.ts
. For now this tests multiple executions with expected results.