-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringObserver.cs
More file actions
103 lines (88 loc) · 2.48 KB
/
StringObserver.cs
File metadata and controls
103 lines (88 loc) · 2.48 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
using System;
using MathParser.DataTypes;
using System.Collections.Generic;
namespace MathParser
{
/// <summary>
/// The entry point of the data.
/// Stores all the history and send the string for further processing to different.
/// Processing classes.
/// </summary>
public class StringObserver : ISolver
{
string givenString;
bool Processed = true;
public StringObserver() { }
MathParserExpression Solution;
public MathParserExpression getSolution() => Solution;
public bool isProcessed() => Processed;
Dictionary<string, MathParserExpression> History;
Dictionary<string, MathParserExpression> theExpressionListHistory = new Dictionary<string, MathParserExpression> ();
public StringObserver(string String_To_Interpret, Dictionary<string, MathParserExpression> History) {
this.History = History;
givenString = String_To_Interpret.Trim();
Interpret();
if (Processed)
{
theExpressionListHistory.Add(givenString, Solution);
}
}
bool isOverdrive(string exp)
{
if (!exp.Contains ("@")) {
return false;
}
string nExp = exp.TrimStart(new char[] { '@' });
if (exp.Length - nExp.Length == 1)
{
if (nExp.Contains("@"))
{
throw new MathParser.MathParserException($"Cannot Parse the expression. Make sure to add just one '@' on the " +
"start of overdrive expression and donot add '@' in any other place in the" +
"expression. If you don't want to use overdrive do not write '@' in the expression");
}else{return true;}
}else{
throw new MathParser.MathParserException($"Unable to understand expression '{exp}'. For MathParser overdrive " +
"only add one '@' at the start of the 'overdrive expression'");
}
}
private void Interpret()
{
if(isOverdrive(givenString)){
MathParserOverdrive mpo = new MathParserOverdrive(givenString.TrimStart(new char[] { '@' }));
mpo.Solve();
if(isProcessed()){
Solution = mpo.getSolution();
Processed = true;
return;
}
}
else if (!givenString.Contains("="))
{
NonEquation solve = new NonEquation (givenString);
solve.History = History;
solve.Solve ();
if (solve.isProcessed())
{
Solution = solve.getSolution();
}
else
{
Processed = false;
}
}
else
{
Equation sol = new Equation (givenString);
sol.theHistory = History;
sol.Solve ();
if (sol.isProcessed ()) {
Solution = sol.getSolution ();
}
else {
Processed = false;
}
}
}
}
}