-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMathParserOverdrive.cs
More file actions
99 lines (75 loc) · 2.73 KB
/
MathParserOverdrive.cs
File metadata and controls
99 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using MathParser.DataTypes;
using MathParser.DataTypes.DynamicDataTypes;
namespace MathParser
{
public class MathParserOverdrive
{
public delegate void OnMathParserOverDriveSyncDelegate(ref MathParser.Solver sol);
public static OnMathParserOverDriveSyncDelegate OnMathParserOverDriveSolverCreate;
void mathParserOverdrive_keywords_sync(ref List<string> keyWordsList){
keyWordsList.Add("root");
keyWordsList.Add("eval");
}
// variables
MathParserExpression solution;
bool Processed = true;
List<string> mathParserOverdrive_keywords;
string givenExpression;
public List<string> oldSolver_KeyWords;
public string oldSolver_OperatorList;
public Dictionary<string, MathParserExpression> oldSolver_Constants;
public Dictionary<string, MathParserExpression> oldSolver_History;
public List<string> oldSolver_LeftRightFunctionKeywords = new List<string>();
// variables
public MathParserOverdrive(){}
public MathParserOverdrive(string expression) {
mathParserOverdrive_keywords = new List<string>();
mathParserOverdrive_keywords_sync (ref mathParserOverdrive_keywords);
this.givenExpression = expression;
}
public bool isProcessed() { return Processed;}
public MathParserExpression getSolution() { return solution;}
public void Solve(){
// validate the given string.
if (this.givenExpression.Length - this.givenExpression.Replace("->", "").Length > 2) {
Processed = false;
throw new MathParser.MathParserException($"Invalid expression '{this.givenExpression}' " +
". Only write '->' after the overdrive function " +
"keyword");
}
string[] e = this.givenExpression.Split(new string[] {"->"},StringSplitOptions.None);
e[0] = e[0].Trim();
e[1] = e[1].Trim();
if(e[1][0] == '<' && e[1][1] == '>'){
e[1] = e[1].Substring(2);
}
string[] args = e[1].Trim(new char[] { '<', '>' }).Split(new string[] {"<>"}, StringSplitOptions.None);
if(mathParserOverdrive_keywords.Contains(e[0])){
Process(e[0], args, ref solution);
}else{
throw new MathParserException($"Given keyword '{e[0]}' does not exit.");
}
}
MathParser.Solver makeMathParserSolverObject(){
MathParser.Solver sol = new MathParser.Solver();
OnMathParserOverDriveSolverCreate?.Invoke(ref sol);
return sol;
}
void Process(string command, string[] args, ref MathParserExpression theSolution) {
MathParser.Solver sol = makeMathParserSolverObject ();
sol.SaveHistory();
if(command == "eval"){
foreach (var x in args)
{
sol.setMathExpression(x);
sol.Solve();
}
theSolution = sol.getSolution();
return;
}
throw new MathParserException("Keyword does not exist");
}
}
}