Skip to content

Commit 2e11b80

Browse files
committed
Remove OperatorList and rename OperatorAction to Operators
1 parent b8a4e98 commit 2e11b80

File tree

2 files changed

+24
-49
lines changed

2 files changed

+24
-49
lines changed

MathParser/MathParser.cs

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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

MathParserTest/Test.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics;
3-
using System.Collections.Generic;
43

54
using Microsoft.VisualStudio.TestTools.UnitTesting;
65

@@ -120,9 +119,8 @@ public void Trigonometry()
120119
public void CustomizeOperators()
121120
{
122121
var parser = new MathParser();
123-
124-
parser.OperatorList.Add("$");
125-
parser.OperatorAction.Add("$", (a, b) => a * 2 + b * 3);
122+
123+
parser.Operators.Add("$", (a, b) => a * 2 + b * 3);
126124

127125
Assert.AreEqual(3 * 2 + 3 * 2, parser.Parse("3 $ 2"));
128126
}
@@ -191,12 +189,12 @@ public void ExceptionCatching()
191189
[TestMethod]
192190
public void StrangeStuff()
193191
{
194-
var parser = new MathParser {OperatorList = new List<string>() {"times", "*", "dividedby", "/", "plus", "+", "minus", "-"}};
192+
var parser = new MathParser();
195193

196-
parser.OperatorAction.Add("times", (x, y) => x * y);
197-
parser.OperatorAction.Add("dividedby", (x, y) => x / y);
198-
parser.OperatorAction.Add("plus", (x, y) => x + y);
199-
parser.OperatorAction.Add("minus", (x, y) => x - y);
194+
parser.Operators.Add("times", (x, y) => x * y);
195+
parser.Operators.Add("dividedby", (x, y) => x / y);
196+
parser.Operators.Add("plus", (x, y) => x + y);
197+
parser.Operators.Add("minus", (x, y) => x - y);
200198

201199
Debug.WriteLine(parser.Parse("5 plus 3 dividedby 2 times 3").ToString(parser.CultureInfo));
202200
}

0 commit comments

Comments
 (0)