Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ibudasov/rpn-calculator

๐Ÿงฎ This calculator uses CLI and Reverse ๐Ÿ‡ต๐Ÿ‡ฑ Notation (RPN) for operating it. Nicely extendable with new operations
https://github.com/ibudasov/rpn-calculator

memento-pattern rpn rpn-calculator stack

Last synced: 17 days ago
JSON representation

๐Ÿงฎ This calculator uses CLI and Reverse ๐Ÿ‡ต๐Ÿ‡ฑ Notation (RPN) for operating it. Nicely extendable with new operations

Awesome Lists containing this project

README

        

# Reverse Polish Notation (RPN) Calculator

The calculator has a stack that can contain real numbers.

โ€ข The calculator waits for user input and expects to receive strings containing whitespace separated lists of numbers and operators.

โ€ข Numbers are pushed on to the stack. Operators operate on numbers that are on the stack. โ€ข Available operators are +, -, *, /, sqrt, undo, clear.

โ€ข Operators pop their parameters off the stack, and push their results back onto the stack.

โ€ข The โ€˜clearโ€™ operator removes all items from the stack.

โ€ข The โ€˜undoโ€™ operator undoes the previous operation. โ€œundo undoโ€ will undo the previo us two operations.

โ€ข sqrt performs a square root on the top item from the stack.

โ€ข The โ€˜+โ€™, โ€˜-โ€™, โ€˜*โ€™, โ€˜/โ€™ operators perform addition, subtraction, multiplication and division respectively on the top two items from the stack.

โ€ข After processing an input string, the calculator displays the current contents of the stack as a space-separated list.

โ€ข Numbers should be stored on the stack to at least 15 decimal places of precision, but displayed to 10 decimal places (or less if it causes no loss of precision).

โ€ข All numbers should be formatted as plain decimal strings (ie. no engineering formatting).

โ€ข If an operator cannot find a sufficient number of parameters on the stack, a warning is displayed:
operator (position: ): insufficient parameters

โ€ข After displaying the warning, all further processing of the string terminates and the current state of the stack is displayed.

## Ideas behind the design

- I was trying to be as expressive in code as possible, by naming things according to domain terms and project
requirements like: `SorryCannotFindSufficientNumberOfParametersInTheStack`
, `Operation.performOperationAndAddResultToStack()`, etc., to make the code as clear as possible for those who
understand the domain
- I was trying to follow Hexagonal/DDD approach, by splitting the whole project to `domain`, `application`
and `infrastructure` layers
- `infrastructure` layer contains only CLI interface, but later on โ€” will be able to support also web interface
- All the calculator logic is contained in the `domain` layer, and adding a new operation shall not involve anything
from `application` or `infrastructure` layers

## Install

`git clone [email protected]:ibudasov/rpn-calculator.git`

## Use

`MainKt.main` ๐Ÿ‘ˆ run it in your IDE ๐Ÿ’โ€

## Todo

- preserve stack between inputs, so the operations can be chained