Giter Site home page Giter Site logo

calc-rs's Introduction

calc-rs

Calculator to perform basic arithmetic operations on raw expressions.

Numbers Adventure

Preview

preview

Examples

? Expression: (2 * 2) + (4 * 3)
#> 16
? Expression: (12 / 2) * [30 * 3]
#> 540
? Expression: 1.0 * 0.1
#> 0.1
? Expression: 0.1 * (100 * 14)
#> 140
? Expression: 0.1 / 20 + 14
#> 14.005
? Expression: (1 * (2 + 1)) + 1
#> 4

How it works

we call the whole input an experssion (expr for short). Every expr might be made of combination of terms, operators and other expr. For example take this input 1 + 2 [14 * (22 + 4)] + 7.

  • 1 + 2 [14 * (22 + 4)] + 7 this is as whole called expr
  • 1 is a term
  • + is an operator
  • [14 * (22 + 4)] another expr
  • (22 + 4) another expr

Using this recursive relationship we can evaluate the expr in this manner

Result (+)
├── 1
├── 2
├── [14 * (22 + 4)] (*)
│   ├── 14 
│   └── (22 + 4) (+)
│       ├── 22 
│       └── 4   
└── 7

I am using a combination of recursion and regular expressions to be able to perform nested operations.

at the core you will find this single regexp

let re = Regex::new(r"(\(|\[)(?P<expr>[^\)\(\]\[]+)(\)|\])").unwrap();

This regexp should match any nested experssion that is inside () or [] or both. It will match from inside out for example consider this experssion [()()] it will match () then () then the whole [() ()] experssion

I am sure if i tried to read this regexp on week from now I will not be able to do so! for this reason I am using interactive tool called regexp101.com that make regexp easy to develop

if you follow this link you should see this

Numbers Adventure

Supported operations

enum Op {
    Plus, // '+'
    Mins, // '-'
    Multi, // '*' | 'x'
    Div, // '/'
}

calc-rs's People

Contributors

neuodev avatar getify avatar

Stargazers

 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.