File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using System ;
2+
3+ namespace Common . BasicHelper . Math ;
4+
5+ public static class Calculator
6+ {
7+ public static double Calculate ( Expression ? a , Expression ? b , CalculationType type )
8+ {
9+ a = a ?? throw new ArgumentNullException ( nameof ( a ) ) ;
10+ b = b ?? throw new ArgumentNullException ( nameof ( b ) ) ;
11+
12+ switch ( type )
13+ {
14+ case CalculationType . Unknown :
15+ throw new NotImplementedException (
16+ $ "Unknown type can't calculate for `{ a ? . Result } { type } { b ? . Result } `."
17+ ) ;
18+
19+ case CalculationType . Add :
20+ return a ! . Result + b ! . Result ;
21+
22+ case CalculationType . Substraction :
23+ return a ! . Result - b ! . Result ;
24+
25+ case CalculationType . Multiply :
26+ return a ! . Result * b ! . Result ;
27+
28+ case CalculationType . Division :
29+ return a ! . Result / b ! . Result ; // Here couldn't throw divide by zero exception.
30+
31+ case CalculationType . Power :
32+ return System . Math . Pow ( a ! . Result , b ! . Result ) ;
33+
34+ default :
35+ throw new NotImplementedException (
36+ $ "Please provide argument { nameof ( type ) } ({ nameof ( CalculationType ) } )"
37+ ) ;
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ namespace Common . BasicHelper . Math ;
2+
3+ public enum CalculationType
4+ {
5+ Unknown = 0 ,
6+ Add = 1 ,
7+ Substraction = 2 ,
8+ Multiply = 3 ,
9+ Division = 4 ,
10+ Power = 5 ,
11+ }
12+
13+ public class Expression
14+ {
15+ private CalculationType type = CalculationType . Unknown ;
16+
17+ public CalculationType Type { get => type ; set => type = value ; }
18+
19+ public Expression ? Left { get ; set ; }
20+
21+ public Expression ? Right { get ; set ; }
22+
23+ private double ? givenValue = null ;
24+
25+ public double Result
26+ {
27+ get => givenValue ?? Calculator . Calculate ( Left , Right , type ) ;
28+ set
29+ {
30+ givenValue = value ;
31+ }
32+ }
33+
34+ public static Expression FromValue ( double value )
35+ {
36+ return new ( )
37+ {
38+ Result = value
39+ } ;
40+ }
41+ }
42+
43+
Original file line number Diff line number Diff line change @@ -66,7 +66,7 @@ public static int GetPosition(int number, int bit)
6666 var pow = 10 ;
6767 for ( var i = 0 ; i < bit - 2 ; ++ i )
6868 pow *= pow ;
69- return ( number % pow ) / ( pow / 10 ) ;
69+ return number % pow / ( pow / 10 ) ;
7070 }
7171}
7272
You can’t perform that action at this time.
0 commit comments