Skip to content

neuodev/calc-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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, // '/'
}

About

Calculator to perform basic and nested arithmetic operations on raw expressions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages