-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.cs
More file actions
96 lines (79 loc) · 2.94 KB
/
Decoder.cs
File metadata and controls
96 lines (79 loc) · 2.94 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
using Decoder.Internal;
namespace Decoder.External
{
public class equationDecoder : IDisposable
{
// Used for the Dispose methods
private bool disposed = false;
private object Lock = new object();
private verify verify = new verify();
private calculate calculate = new calculate();
private bracketHandler bracketHandler = new bracketHandler();
public double calculateResult(string originalEquation)
{
var (operators, numbers) = calculate.splitEquation(originalEquation); // Generates two lists
int opCount = 2 + ((operators.Count - 1) * 2);
// Ensures there is enough numebrs and operators to complete an equation
if (opCount != numbers.Count)
{
throw new ArgumentException("Not enough numbers or operators");
}
int numberCounter = 2;
bool firstRun = true;
double result = 0;
double num1 = 0;
double num2 = 0;
for (int i = 0; i < operators.Count; i++)
{
if (firstRun) // Since num1 and num2 will be 0 at the start
{
num1 = numbers[0];
num2 = numbers[1];
result = calculate.getResult(operators[i], num1, num2);
firstRun = false;
}
else // Since result will be used as num1 as it will have been calculated
{
num1 = result;
num2 = numbers[numberCounter];
numberCounter++;
result = calculate.getResult(operators[i], num1, num2);
}
}
return result;
}
/*
* Frees unmanaged resources and indicates the finalizer will not need to be run
*/
public void Dispose()
{
// Synchronizes the disposal methods should threads be used in the program
lock (Lock)
{
// Call's the cleaner
Dispose(true);
// Indicates the finalizer is not needed
GC.SuppressFinalize(this);
}
}
protected virtual void Dispose(bool disposing)
{
// Only disposes if it is called to do so and has not already been done so
if ((!disposed) && (disposing))
{
// Calls the dispose methods of the managed resources
verify?.Dispose();
calculate?.Dispose();
bracketHandler?.Dispose();
// Indicates that it has been disposed
disposed = true;
}
}
// The finalizer cleans up unmanaged resources
// There are none but it is good practice to implement incase some are added in future
~equationDecoder()
{
Dispose(false);
}
}
}