Skip to content

Reverse Polish Notation calculator, using Vorpal (for an exercise)

Notifications You must be signed in to change notification settings

TravelingTechGuy/RPN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Reverse Polish Notation calculator

This app uses the Vorpal REPL environment to simulate an RPN calculator. It was developed for an interview exercise.

Installation

  1. Clone the repo
  2. Run npm install in the app's folder

Running the app

  1. Run npm start (or node index) in the app's folder
  2. You'll be put into the ap, with a $ prompt
  3. You can type help to see available commands
  4. Type rpn to start using the calculator
  5. Type q or exit to exit the app
  6. Type any number to add it to the queue
  7. Type one of the 4 operands (+, - ,/ , *) to carry out the operations on the last 2 operands

Sample run

> npm start
$ help

  Commands:

    help [command]   Provides help for a given command.
    exit [options]   Exit program
    rpn              Start RPN functionality
$ rpn
Welcome to RPN. Enter numbers or operators. To exit, type `exit`.
$ rpn: 3
3
$ rpn: 7
7
$ rpn: +
10
$ rpn: 2
2
$ rpn: -
-8
$ rpn: 2
2
$ rpn: /
-0.25
$ rpn: q
KBye!

Error handling

Error handling is rudimentary.

  1. Only numbers that can be parsed, operators, and end commands are checked for.
  2. The execution code always checks to see that there are 2 available operands on the stack before calculating: If there are none, an error is issued. If there's only one, an error is issued, and it is placed back on stack.
  3. It may be possible to break this REPL, or send option parameters. But this was built as quick exercise, and not intended for production use (although who uses an RPN calculator in production?).

Future improvements/extensions

  1. Support more operators - can be done by removing initial check in handleInput and adding a case to the calculate function
  2. Support direct handling of tuples ("+ 7 2" => 9) by parsing the args string in handleInput
  3. Support full RPN notation in one line ("+ 7 - 2 3" => 6)
  4. Support more than 2 operands per operation (if needed)
  5. Print out entire stack as another command
  6. Allow clearing stack as another command
  7. Allow poping a number as another command

About

Reverse Polish Notation calculator, using Vorpal (for an exercise)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages