-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNonEquation.cs
More file actions
364 lines (319 loc) · 9.51 KB
/
Copy pathNonEquation.cs
File metadata and controls
364 lines (319 loc) · 9.51 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using MathParser.DataTypes;
using System.Collections.Generic;
using System.Linq;
namespace MathParser
{
/// <summary>
/// The class that is concerned with the solving those mathematical expressions
/// which donot have an equality symbol in them.
/// The solution is autonamed here.
/// </summary>
///
/// This class communicates with the DMAS Solver class, the BODMAS Solver class and
/// Checker class and OperatorHandler class.
///
/// Gives its solution to String Observer.
///
class NonEquation : ISolver
{
string givenExpression;
bool Processed = true;
MathParserExpression Solution;
List<string> ExpressionElements = new List<string>();
Dictionary<string, MathParserExpression> theData = new Dictionary<string, MathParserExpression>();
char[] theBasicOperatorList;
public string theBasicOperatorsString;
public delegate void OperatorSyncDelegate(ref char[] theBasicOperatorList);
public delegate void ConstantSyncDelegate(ref Dictionary<string, MathParserExpression> ConstantList);
public Dictionary<string, MathParserExpression> History;
int matrixAutoCounter = 0, numberAutoCounter = 0;
public OperatorSyncDelegate OnOperatorSync = null;
public static OperatorSyncDelegate staticOnOperatorSync = null;
public static ConstantSyncDelegate staticOnConstantSync = null;
public List<string> ConstantName = new List<string>();
void ConstantSync()
{
Checker.Constants = new Dictionary<string, MathParserExpression>();
staticOnConstantSync?.Invoke(ref Checker.Constants);
string[] arr = new string[Checker.Constants.Count + 2];
Checker.Constants.Keys.CopyTo(arr, Checker.Constants.Count);
//Checker.Constants.get
ConstantName = new List<string>(arr);
}
public MathParserExpression getSolution()
{
ExpressionElements.Clear();
theData.Clear();
return Solution;
}
public bool isProcessed() { return Processed; }
public NonEquation() { }
public NonEquation(string givenString)
{
givenExpression = givenString.Trim();
}
void OperatorSync() // Syncs operators.
{
if (OnOperatorSync != null)
{
OnOperatorSync(ref theBasicOperatorList);
}
else if (staticOnOperatorSync != null)
{
staticOnOperatorSync(ref theBasicOperatorList);
}
else
{
theBasicOperatorList = ("+-÷/*^" + new string(((char)215), 1)).ToCharArray();
}
theBasicOperatorsString = new string(theBasicOperatorList);
theBasicOperatorsString += "`()#";
Checker.OperatorList = theBasicOperatorsString;
} // end Operator syncor functions.
public void Solve() // the method deals with the prelimanry making of the Expression Understading.
{
OperatorSync(); // Syncs the operators to be considered.
SyncKeyWord();
ConstantSync();
if (givenExpression.Contains("(") || givenExpression.Contains(")"))
{
Only_Number_And_Operator_List_Maker();
if (!(ExpressionElements[ExpressionElements.Count - 1] == ")") && theBasicOperatorsString.Contains(ExpressionElements[ExpressionElements.Count - 1]))
{
Processed = false;
throw new MathParserException($"" +
"Invalid operator entry. No basic operator ({theBasicOperatorList.ToString()}) can be at the end of the expression.");
}
the_Data_List_Maker();
OperatorHandler
oh = new OperatorHandler(ref ExpressionElements);
oh.BasicOperatorsString = theBasicOperatorsString;
oh.Process();
if (!oh.isCorrectOperatorSequence())
{
Processed = false;
throw new MathParserException("Bad operator sequence.");
}
// Balancing brakkets.
if (ExpressionElements.Contains("(") || ExpressionElements.Contains("-(") || ExpressionElements.Contains(")"))
{
int nsb = 0;
int neb = 0;
foreach (string s in ExpressionElements)
{
if (s.Contains("("))
nsb++;
if (s.Contains(")"))
neb++;
}
if (nsb > neb)
{
for (int c = 0; c < (nsb - neb); c++)
{
ExpressionElements.Add(")");
}
}
if (neb > nsb)
{
List<string> dumy = new List<string>();
for (int c = 0; c < (-nsb + neb); c++)
{
dumy.Add("(");
}
foreach (string x in ExpressionElements)
{
dumy.Add(x);
}
ExpressionElements = dumy;
}
}
// end balancing brakkets
MathParser.BraketSolver sol = new BraketSolver(ExpressionElements, theData);
if (sol.isProcessed())
{
Solution = sol.getSolution();
}
else
{
Processed = false;
throw new MathParserException("Error");
}
}
else if ((givenExpression.Contains("[") && !givenExpression.Contains("]")) || (!givenExpression.Contains("[") && givenExpression.Contains(("]")))) // if the expession string contans an invalid matrix formate.
{
throw (new MathParserException("Invalid Matrix Formate. Entered."));
}
else // if the expression string contain only operators and numbers.
{
Only_Number_And_Operator_List_Maker();
if (theBasicOperatorsString.Contains(ExpressionElements[ExpressionElements.Count - 1]))
{
Processed = false;
throw new MathParserException($"Invalid operator entry. No basic operator ({theBasicOperatorList.ToString()}) can be at the end of the expression.");
}
else
{
the_Data_List_Maker();
OperatorHandler oh = new OperatorHandler(ref ExpressionElements);
oh.BasicOperatorsString = theBasicOperatorsString;
oh.Process();
if (!oh.isCorrectOperatorSequence())
{
Processed = false;
throw new MathParserException("Bad operator sequence.");
}
else
{
DMASSolver DS = new DMASSolver(ExpressionElements, theData);
DS.Solve();
if (DS.isProcessed())
{
Solution = new MathParserExpression(DS.getSolution());
}
else
{
Processed = false;
}
}
} // end
} // end
} // end solve
void Only_Number_And_Operator_List_Maker()
{
string dumy = "";
bool matrixBatch = false;
foreach (char c in givenExpression)
{
if (c == '[' && !matrixBatch)
{
matrixBatch = true;
}
if (theBasicOperatorsString.Contains(c.ToString()) && !matrixBatch)
{
if (string.IsNullOrEmpty(dumy) || string.IsNullOrWhiteSpace(dumy))
{
dumy = "";
}
else
{
ExpressionElements.Add(dumy.Trim());
}
ExpressionElements.Add(c.ToString());
dumy = "";
}
else
{
dumy += c.ToString();
}
if (c == ']' && matrixBatch)
{
matrixBatch = false;
ExpressionElements.Add(dumy);
dumy = "";
}
}
if (!string.IsNullOrEmpty(dumy) && !string.IsNullOrWhiteSpace(dumy))
{
ExpressionElements.Add(dumy);
}
dumy = "";
}
public List<string> theKeyWordsList = new List<string>();
public delegate void KeyWordSyncDelegate(ref List<string> theKeyWordList);
public static KeyWordSyncDelegate staticOnKeyWordSync;
Dictionary<string, string> dumyKeyWordsList = new Dictionary<string, string>();
void SyncKeyWord()
{
theKeyWordsList = new List<string>();
staticOnKeyWordSync?.Invoke(ref theKeyWordsList);
theKeyWordsList = theKeyWordsList.OrderByDescending(x => x.Length).ToList();
if (theKeyWordsList != null && !givenExpression.Contains("#"))
{
int the_counter = 0;
foreach (string keyword in theKeyWordsList)
{
string key = "¿?" + "internal_matrix_sAADIww___" + the_counter + "¿?";
givenExpression = givenExpression.Replace(keyword, "#" + key + "`");
dumyKeyWordsList.Add(key, keyword);
the_counter++;
}
foreach (var item in dumyKeyWordsList)
{
givenExpression = givenExpression.Replace(item.Key, item.Value);
}
}
Checker.KeyWords = theKeyWordsList;
}
void the_Data_List_Maker()
{
for (int c = 0; c < ExpressionElements.Count; c++)
{
string Element = ExpressionElements[c].Trim(' ', '-');
if (!theBasicOperatorsString.Contains(Element) && !theKeyWordsList.Contains(Element))
{
string name = "";
if (Checker.MatrixSyntax(Element, "[", "]", ",", ";"))
{
name = autoMatrixNamer();
theData.Add(name, new MathParserExpression(MathParser.DataTypes.DynamicDataTypes.Matrix.Parse(Element.Trim())));
}
else if (History != null &&
History.ContainsKey(Element))
{
name = autoNumberNamer();
theData.Add(name, new MathParserExpression(History[Element]));
}
else if (Element.Contains("@"))
{
}
else if (ConstantName.Contains(Element))
{
name = autoNumberNamer();
theData.Add(name, Checker.Constants[Element]);
}
else
{
try
{
name = autoNumberNamer();
theData.Add(name, new MathParserExpression(MathParser.DataTypes.DynamicDataTypes.Number.Parse(Element.Trim())));
}
catch
{
Processed = false;
throw new MathParserException($"Invalid entry '{Element}'.");
}
}
ExpressionElements[c] = name;
} // if there is an expression element other than a basic operator.
} // end foreach loop for making the data list entries.
} // end data list maker.
string autoMatrixNamer()
{
matrixAutoCounter++;
string name = "internal_matrix_sAADIww___" + matrixAutoCounter.ToString();
if (theData.ContainsKey(name))
{
return autoMatrixNamer();
}
else
{
return name;
}
}
string autoNumberNamer()
{
numberAutoCounter++;
string name = "internal_Number_sAADIww___" + numberAutoCounter.ToString();
if (theData.ContainsKey(name))
{
return autoNumberNamer();
}
else
{
return name;
}
}
}
}