A calculator for evaluating nested mathematical expressions written in English
This is a simple calculator through which mathematical expressions could be evaluated and results sent back to the console. Main mathematical functions which are catered are as under
- Addition
- Subtraction
- Multiplication
- Division
Apart from these, there is another expression called let
which is used for assigning data
to a variable and then using it for evaluating further mathematical functions.
Some of the example inputs and respective output is given below:
Input | Output |
---|---|
add(1, 2) | 3 |
add(1, mult(2, 3)) | 7 |
mult(add(2, 2), div(9, 3)) | 12 |
let(a, 5, add(a, a)) | 10 |
let(a, 5, let(b, mult(a, 10), add(b, a))) | 55 |
let(a, let(b, 10, add(b, b)), let(b, 20, add(a, b)) | 40 |
- The general expression is in the format =>
expr(subExpr ...)
whereexpr
is an expression andsubExpr...
are multiple sub-expressions within an expression. - A sub-expression can be either a number of an expression
add(1, 3)
=>expr == add(
andsubExpr.. == [1, 2]
mult(sub(1, 3), 3)
=>expr == mult(
andsubExpr.. == [sub(1, 3), 3]
- A
let
operator for assigning values to variableslet(variable, value, expression(variable))
let(a, 5, add(a, a))
=>variable == a
andvalue == 5
andexpression(variable) == add(a, a)
- Validate the expression and trim any spaces
- Extract an isolated block from an expression and evaluation that block recursively.
- Base cases are the direct mathematical evaluations.
add(5, 3) is a base case since it would return mathematical result rather than expression
. - Starting point is to identify the mathematical expression so which would be the first evaluation in recursion.
evaluation of add(expression1, expression2)
is equvivalant toevaluation of expression1(subExpr...) + expression1(subExpr2...)
java -jar simple-calculator-0.0.1-SNAPSHOT.jar arg1 arg2
where:
- arg1 = Mathematical Expression
(let(a, 5, add(a, a)))
- arg2 = Log Level
(ERROR, WARN, INFO, DEBUG, TRACE, ALL)
- IntelliJ - IDE used for Development
- Java - Project development language
- Travis CI - CI for the project
- Muhammad Umer
This project is licensed under the MIT License
- see the LICENSE file for details