Code Monkey home page Code Monkey logo

simple-calculator's Introduction

simple-calculator

Build Status

A calculator for evaluating nested mathematical expressions written in English

Overview

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

  1. Addition
  2. Subtraction
  3. Multiplication
  4. 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.

Examples

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

Explanation of Example

  • The general expression is in the format => expr(subExpr ...) where expr is an expression and subExpr... are multiple sub-expressions within an expression.
  • A sub-expression can be either a number of an expression
    1. add(1, 3) => expr == add( and subExpr.. == [1, 2]
    2. mult(sub(1, 3), 3) => expr == mult( and subExpr.. == [sub(1, 3), 3]
  • A let operator for assigning values to variables
    1. let(variable, value, expression(variable))
    2. let(a, 5, add(a, a)) => variable == a and value == 5 and expression(variable) == add(a, a)

Solution Principle

  1. Validate the expression and trim any spaces
  2. Extract an isolated block from an expression and evaluation that block recursively.
  3. Base cases are the direct mathematical evaluations. add(5, 3) is a base case since it would return mathematical result rather than expression.
  4. Starting point is to identify the mathematical expression so which would be the first evaluation in recursion.
  5. evaluation of add(expression1, expression2) is equvivalant to evaluation of expression1(subExpr...) + expression1(subExpr2...)

How to Run

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)

Built With

Authors

  • Muhammad Umer

License

This project is licensed under the MIT License - see the LICENSE file for details

simple-calculator's People

Contributors

muhammad-umer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.