Git Product home page Git Product logo

calculator's Introduction

Calculator

Code challenge

An incorrect implementation of a simple calculator, where the following operators are supported and have ordered precedence:

*
/
+
-

(Although not BODMAS, this was more challenging to solve!)

**************************************
Expression involving bracket example:
**************************************

Given:
1 + 2 * (3 + 4) - 5 * (6 * (7 + 8)) =
1 + 2 * 7 - 5 * 90 = (15 - 450) = -435

1) Tokenize brackets
1 + 2 * a - 5 * b
a = 3 + 4
b = 6 * c
c = 7 + 8

2) Build binary expression groups 
[{1+2} {2*a} {a-5} {5*b}]
          a = [{3+4}]
                      b = [{6*c}]
                              c = [{7+8}]

3) Evalutate 
[{1+2} {2*7} {a-5} {5*b}]
                      b = [{6*c}]
                              c = [{7+8}]

[{1+2} {2*7} {a-5} {5*b}]
                      b = [{6*15}]

[{1+2} {2*7} {a-5} {5*90}]

*
[{1+2} {2*7} {a-5}  {5*90}]
[{1+14}      {14-450}]

+
[{1+14} {14-450}]
[{15-450}]

-
[{15-450}]

**************************************
Expressions without bracket examples:
**************************************

Given:
1 + 2 * 3 + 4 - 5 * 6 + 7 = (11 - 37) = -26

1) Build binary expression groups 
[{1+2} {2*3} {3+4} {4-5} {5*6} {6+7}]

2) Evalutate
*
[{1+2} {2*3} {3+4} {4-5} {5*6} {6+7}]
[{1+6}       {6+4} {4-5} {5*6} {6+7}]
[{1+6}       {6+4} {4-30}      {30+7}]

+
[{1+6} {6+4} {4-30} {30+7}]
[      {7+4} {4-30} {30+7}]
[            {11-30}{30+7}]
[            {11-37}      ]

-
[{11-37}]

**************************************
Given: 
1 + 2 - 3 * 4 * 5 - 6 + 7 = (1 + 2 - 60 - 6 + 7) = (3 - 60 - 13) = -70

1) Build binary expression groups 
[{1+2} {2-3} {3*4} {4*5} {5-6} {6+7}]

2) Evaluate
*
[{1+2} {2-3} {3*4} {4*5} {5-6} {6+7}]
[{1+2} {2-12}      {12*5} {5-6} {6+7}]
[{1+2} {2-60}            {60-6} {6+7}]

+
[{1+2} {2-60} {60-6} {6+7}]
[      {3-60} {60-6} {6+7}]
[      {3-60} {60-13}     ]

-
[{3-60} {60-13}]
[       {-57-13}]

**************************************
Given:
1 * 2 + 3 + 4 - 5 - 6 * 7 = (9 - 5 - 42) = -38

1) Build binary expression groups 
[{1*2} {2+3} {3+4} {4-5} {5-6} {6*7}]

2) Evaluate
*
[{1*2} {2+3} {3+4} {4-5} {5-6} {6*7}]
[      {2+3} {3+4} {4-5} {5-6} {6*7}]
[      {2+3} {3+4} {4-5} {5-42}     ]

+
[{2+3} {3+4} {4-5} {5-42}]
[      {5+4} {4-5} {5-42}]
[            {9-5} {5-42}]

-
[{9-5} {5-42}]
[      {4-42}]
[{4-42}]

calculator's People

Contributors

danielglennross avatar danielross-form3 avatar

Watchers

 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.