Skip to content

GouruRK/rpn-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rpn-calculator

dc command-like to compute rpn - Reverse Polish notation expression.

Download and run

To download the repo, you can either download the .zip file for this project, or use git commands as bellow :

$ git clone https://github.com/GouruRK/rpn-calculator.git

This repo uses the readline library. Donwload it with

$ sudo apt-get install libreadline-dev.

To compile it, go in the rpn-calculator folder and run the make command to compile it. It creates an executable rpn.

$ cd rpn-calculator
$ make
$ ./rpn

Examples

$ ./rpn
1 3 p + 4 * p q
3
16
$ ./rpn
p c a 1 a c a c 0 r 1 r / % ^ a 3 ^ a 3 r ^ a 42 1984 a - - - - a 1947 - a ! 5 + ! a q
rpn: stack empty
[] <- HEAD
[1] <- HEAD
[] <- HEAD
rpn: stack empty
rpn: divide by 0
rpn: divide by 0
[1] <- HEAD
[1] <- HEAD
[3] <- HEAD
[3, 42, 1984] <- HEAD
rpn: stack empty
rpn: stack empty
[1945] <- HEAD
[-2] <- HEAD
rpn: factorial of negative number
[6] <- HEAD

How to use

  • Tokens must be separated by spaces or new lines
  • Relatives numbers must be written as -a
  • This implementation does not support floating numbers
  • If computing a sequence triggers an error, the stack stays untouched

Supported operation

Operation Input Calculated
+ a b + a + b
- a b - a - b
* a b * a * b
/ a b / a / b
% a b % a % b
^ a b ^ a ^ b
! a ! a!

Supported commands

  • a : Print the stack
  • p : Print the first element of the stack
  • r : Reverse the top two elements of the stack
  • c : Clear the stack
  • q : Quit

About

dc command-like to compute rpn expression

Topics

Resources

License

Stars

Watchers

Forks