@@ -25,16 +25,9 @@ public class MathParser
2525 #region Properties
2626
2727 /// <summary>
28- /// All operators should be inside this property.
29- /// The first operator is executed first, et cetera.
30- /// An operator may only be ONE character.
28+ /// All operators that you want to define should be inside this property.
3129 /// </summary>
32- public List < string > OperatorList { get ; set ; }
33-
34- /// <summary>
35- /// When adding a variable in the OperatorList property, you need to assign how that operator should work.
36- /// </summary>
37- public Dictionary < string , Func < double , double , double > > OperatorAction { get ; set ; }
30+ public Dictionary < string , Func < double , double , double > > Operators { get ; set ; }
3831
3932 /// <summary>
4033 /// All functions that you want to define should be inside this property.
@@ -66,22 +59,7 @@ public MathParser(bool loadPreDefinedFunctions = true, bool loadPreDefinedOperat
6659 {
6760 if ( loadPreDefinedOperators )
6861 {
69- OperatorList = new List < string > ( 10 )
70- {
71- "^" ,
72- "%" ,
73- ":" ,
74- "/" ,
75- "*" ,
76- "-" ,
77- "+" ,
78-
79- ">" ,
80- "<" ,
81- "="
82- } ;
83-
84- OperatorAction = new Dictionary < string , Func < double , double , double > > ( 10 )
62+ Operators = new Dictionary < string , Func < double , double , double > > ( 10 )
8563 {
8664 [ "^" ] = Math . Pow ,
8765 [ "%" ] = ( a , b ) => a % b ,
@@ -90,17 +68,14 @@ public MathParser(bool loadPreDefinedFunctions = true, bool loadPreDefinedOperat
9068 [ "*" ] = ( a , b ) => a * b ,
9169 [ "-" ] = ( a , b ) => a - b ,
9270 [ "+" ] = ( a , b ) => a + b ,
93-
71+
9472 [ ">" ] = ( a , b ) => a > b ? 1 : 0 ,
9573 [ "<" ] = ( a , b ) => a < b ? 1 : 0 ,
9674 [ "=" ] = ( a , b ) => Math . Abs ( a - b ) < 0.00000001 ? 1 : 0
9775 } ;
9876 }
9977 else
100- {
101- OperatorList = new List < string > ( ) ;
102- OperatorAction = new Dictionary < string , Func < double , double , double > > ( ) ;
103- }
78+ Operators = new Dictionary < string , Func < double , double , double > > ( ) ;
10479
10580 if ( loadPreDefinedFunctions )
10681 {
@@ -372,11 +347,11 @@ private List<string> Lexer(string expr)
372347 }
373348
374349 if ( i + 1 < expr . Length && ( ch == '-' || ch == '+' ) && char . IsDigit ( expr [ i + 1 ] ) &&
375- ( i == 0 || OperatorList . IndexOf ( expr [ i - 1 ] . ToString (
350+ ( i == 0 || Operators . ContainsKey ( expr [ i - 1 ] . ToString (
376351#if ! NETSTANDARD1_4
377352 CultureInfo
378353#endif
379- ) ) != - 1 ||
354+ ) ) ||
380355 i - 1 > 0 && expr [ i - 1 ] == '(' ) )
381356 {
382357 // if the above is true, then the token for that negative number will be "-1", not "-","1".
@@ -514,30 +489,32 @@ private double BasicArithmeticalExpression(List<string> tokens)
514489
515490 if ( op == "-" || op == "+" )
516491 {
517- return double . Parse ( ( op == "+" ? "" : ( tokens [ 1 ] . Substring ( 0 , 1 ) == "-" ? "" : "-" ) ) + tokens [ 1 ] , CultureInfo ) ;
492+ var first = op == "+" ? "" : ( tokens [ 1 ] . Substring ( 0 , 1 ) == "-" ? "" : "-" ) ;
493+
494+ return double . Parse ( first + tokens [ 1 ] , CultureInfo ) ;
518495 }
519496
520- return OperatorAction [ op ] ( 0 , double . Parse ( tokens [ 1 ] , CultureInfo ) ) ;
497+ return Operators [ op ] ( 0 , double . Parse ( tokens [ 1 ] , CultureInfo ) ) ;
521498 case 0 :
522499 return 0 ;
523500 }
524501
525- foreach ( var op in OperatorList )
502+ foreach ( var op in Operators )
526503 {
527- while ( tokens . IndexOf ( op ) != - 1 )
504+ int opPlace ;
505+
506+ while ( ( opPlace = tokens . IndexOf ( op . Key ) ) != - 1 )
528507 {
529- var opPlace = tokens . IndexOf ( op ) ;
530-
531508 var numberA = double . Parse ( tokens [ opPlace - 1 ] , CultureInfo ) ;
532509 var numberB = double . Parse ( tokens [ opPlace + 1 ] , CultureInfo ) ;
533510
534- var result = OperatorAction [ op ] ( numberA , numberB ) ;
511+ var result = op . Value ( numberA , numberB ) ;
535512
536513 tokens [ opPlace - 1 ] = result . ToString ( CultureInfo ) ;
537514 tokens . RemoveRange ( opPlace , 2 ) ;
538515 }
539516 }
540-
517+
541518 return double . Parse ( tokens [ 0 ] , CultureInfo ) ;
542519 }
543520
0 commit comments