Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stokito/rpn-calc
Java RPN calculator example
https://github.com/stokito/rpn-calc
assignment job-assignment rpn-calculator
Last synced: 18 days ago
JSON representation
Java RPN calculator example
- Host: GitHub
- URL: https://github.com/stokito/rpn-calc
- Owner: stokito
- License: mit
- Created: 2020-04-02T11:09:01.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-26T19:17:32.000Z (over 1 year ago)
- Last Synced: 2024-12-13T01:33:14.300Z (23 days ago)
- Topics: assignment, job-assignment, rpn-calculator
- Language: Java
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# CLI RPN Calculator
The command-line [Reverse Polish Notation (RPN)](https://en.wikipedia.org/wiki/Reverse_Polish_notation) calculator.
Was developed as a [test task](https://gist.github.com/joedean/078a62b9ec03b38dfc519b3a5f168b07).## Specifications
* The calculator should use standard input and standard output
* It should implement the four standard arithmetic operators
* The calculator should handle errors and recover gracefully
* The calculator should exit when it receives a `q` command or an end of input indicator (`EOF` / `Ctrl+D`)
`py` op
1. A^2 + B^2 = C^2
5 12 13 = true## Build and run
You need a JDK 13 or later and Maven. To build run `mvn clean package`.
The executable jar will be built and placed into `target/rpn-calc.jar`.
To run it use:java -jar target/rpn-calc.jar
Then it will prompt for an input with a line started with `> `.
## Usage
For example to calculate an expression `5 / (9 - 1) = 0.625` write each number (operand) and press Enter then write an arithmetic command (operator):
> 5
5
> 9
9
> 1
1
> -
8
> /
0.625
>The last number is your result. You can use a single line input:
> 5 9 1 - /
0.625To exit the program you can input command `quit` or simply `q`.
You can also use other commands for example `debug` or `d` to see the values stack and `clear` or `c` to remove everything from the stack:
> 1
1
> 2
2
> 3
3
> d
3
2
1
> c
> d
>The calculator also have other functions from `java.lang.Math` and to see them use a command `help` or `h`:
> h
*
+
-
/
IEEEremainder
abs
acos
asin
atan
atan2
cbrt
ceil
copySign
cos
cosh
exp
expm1
floor
hypot
log
log10
log1p
max
min
neg
nextAfter
nextDown
nextUp
pow
rint
signum
sin
sinh
sqrt
tan
tanh
toDegrees
toRadians
ulp
>For example to calculate square root and negate it:
> 4
4
> sqrt
2
> neg
-2
>## Implementation
The core of the calculator is `Calc` class which contains the values stack and performs all the calculations.
The class designed in a JavaBeans style and have getters and setters so can be easily used with DI.
The actual parsing and interaction with a user via the command line performed by the `StreamProcessor` class.
You can extend the calc and add your custom operator by extending `CalcUnaryOp` or `CalcBinaryOp`. Then you'll need to add them into `Ops.BASIC_OPS` list or this can be done with DI.Internally the calculator uses a `double` type so all the calculations have the same issues as IEEE-754 float numbers have.