-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReverseDMASSolver.cs
More file actions
283 lines (251 loc) · 8.06 KB
/
ReverseDMASSolver.cs
File metadata and controls
283 lines (251 loc) · 8.06 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.Collections.Generic;
using MathParser.DataTypes;
using MathParser.DataTypes.DynamicDataTypes;
namespace MathParser
{
/// <summary>
/// DMAS solver.
/// This class deals with the solving of the mathematical expression.
/// it follows the CBODMAS Rule of the mathmatics.
/// Command.
/// Brakket.
/// Order.
/// Division.
/// Multiplication.
/// Addition.
/// Subtraction.
/// </summary>
public class ReverseDMASSolver : ISolver
{
List<string> Expressions;
Dictionary<string, MathParserExpression> theData;
MathParserExpression solution;
bool Processed = true;
public MathParserExpression getSolution ()
{
return solution;
}
public bool isProcessed() => Processed;
public ReverseDMASSolver() { }
public ReverseDMASSolver(List<string> theExpression, Dictionary<string, MathParserExpression> TheData)
{
Expressions = theExpression;
theData = TheData;
}
public void Solve()
{
if (Expressions.Count == 1)
{
if (Expressions[0].Contains("-"))
{
MathParserExpression hold = theData[Expressions[0].Trim("-".ToCharArray())];
if (hold.Type.Contains("Number"))
{
Number ans = new Number((double)-1) * hold.Data;
solution = new MathParserExpression(ans);
}
else if (hold.Type.Contains("Matrix"))
{
Matrix ans = -1 * hold.Data;
solution = new MathParserExpression(ans);
}
}
else {
solution = theData[Expressions[0].Trim("-".ToCharArray())];
}
}
else
{
if(Division ()){
if (Multiplication ()) {
if (Addition ()) {
solution = new MathParserExpression (theData [Expressions [0].Trim ("-".ToCharArray ())]);
}
}
}
}
}
bool Addition()
{
bool done = true;
if (Expressions.Contains ("+"))
{
for (int c = Expressions.Count-1; c >= 0; c--)
{
if (Expressions[c] == "+")
{
MathParserExpression rhs = theData[Expressions[c - 1].Trim("-".ToCharArray())];
MathParserExpression lhs = theData[Expressions[c + 1].Trim("-".ToCharArray())];
if (rhs.Type == lhs.Type) {
if (rhs.Type.Contains ("Number")) {
if (Expressions [c - 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number (-1));
Expressions [c - 1].Replace ("-", "");
}
if (Expressions [c + 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
Number ans = rhs.Data + lhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else if (rhs.Type.Contains ("Matrix")) {
if (Expressions [c - 1].Contains ("-")) {
rhs = new MathParserExpression ((Matrix)rhs.Data * -1);
Expressions [c - 1].Replace ("-", "");
}
if (Expressions [c + 1].Contains ("-")) {
lhs = new MathParserExpression ((Matrix)lhs.Data * -1);
Expressions [c + 1].Replace ("-", "");
}
Matrix mrhs = rhs.Data;
Matrix mlhs = lhs.Data;
if (mrhs.Rows == mlhs.Rows && mrhs.Columns == mlhs.Columns) {
Matrix ans = rhs.Data + lhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else {
Processed = false;
done = false;
throw new Exception ("Invalid row and column matrix addition.");
}
}
} else {
Processed = false;
done = false;
throw new Exception ("Invalid data types addition.");
}
Expressions.RemoveRange(c,2);
c = Expressions.Count-1;
} // end if
} // end for loop.
}
return done;
} // The function deals with the addition operations.
bool Multiplication()
{
bool done = true;
string multiply_cross = new string (((char)215), 1);
if (Expressions.Contains ("*") || Expressions.Contains (multiply_cross)){
for (int c = Expressions.Count-1; c >= 0; c--)
{
if (Expressions[c] == "*" || Expressions[c] == multiply_cross)
{
MathParserExpression rhs = theData[Expressions[c - 1]];
MathParserExpression lhs = theData[Expressions[c + 1]];
if (rhs.Type == lhs.Type) {
if (rhs.Type.Contains ("Number")) {
if (Expressions [c - 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number (-1));
Expressions [c - 1].Replace ("-", "");
}
if (Expressions [c + 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
Number ans = rhs.Data * lhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else if (rhs.Type.Contains ("Matrix")) {
if (Expressions [c - 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * -1);
Expressions [c - 1].Replace ("-", "");
}
if (Expressions [c + 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * -1);
Expressions [c + 1].Replace ("-", "");
}
Matrix mrhs = rhs.Data;
Matrix mlhs = lhs.Data;
if (mrhs.Rows == mlhs.Columns) {
Matrix ans = rhs.Data * lhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else {
Processed = false;
done = false;
throw new Exception ("Invalid matrix multiplication.");
}
}
} else if (lhs.Type.Contains ("Number") && rhs.Type.Contains ("Matrix")) {
Number l = lhs.Data;
Matrix r = rhs.Data;
Matrix ans = (double)l.Data * r;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else if (rhs.Type.Contains ("Number") && lhs.Type.Contains ("Matrix")) {
Number r = rhs.Data;
Matrix l = lhs.Data;
Matrix ans = l * (double)r.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else {
Processed = false;
done = false;
throw new Exception ("Not a valid multiplication.");
}
Expressions.RemoveRange(c, 2);
c = Expressions.Count-1;
} // end if
} // end for loop.
}
return done;
} // The function deals with the multiplication operations.
bool Division()
{
bool done = true;
if (Expressions.Contains ("÷") || Expressions.Contains ("/")) {
for (int c = Expressions.Count-1; c >=0; c--) {
string Element = Expressions [c];
if (Element == "/" || Element == "÷") {
MathParserExpression lhs = theData [Expressions [c - 1].Trim ("-".ToCharArray ())];
MathParserExpression rhs = theData [Expressions [c + 1].Trim ("-".ToCharArray ())];
if (lhs.Type.Contains ("Number") && rhs.Type.Contains ("Number")) {
Number l = lhs.Data;
Number r = rhs.Data;
Number ans = new Number ((double)l.Data /
(double)r.Data);
string name = autoNamer ();
theData.Add (name, new MathParserExpression (ans));
Expressions [c - 1] = name;
} else if (lhs.Type.Contains ("Matrix") && rhs.Type.Contains ("Number")) {
Matrix l = lhs.Data;
Number r = rhs.Data;
Matrix ans = l / ((double)r.Data);
string name = autoNamer ();
theData.Add (name, new MathParserExpression (ans));
Expressions [c - 1] = name;
} else {
Processed = false;
done = false;
throw new Exception ("Invalid Division.");
}
Expressions.RemoveRange (c,2);
c = Expressions.Count-1;
}
}
}
return done;
}
bool Order()
{
bool done = true;
if (Expressions.Contains ("^")) {
}
return done;
}
int NamingInteger = 0;
string autoNamer()
{
NamingInteger++;
return ("DMASINTERLAN_Solutionsss__" + NamingInteger);
}
} // end class DMASSolver.
}