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
|
namespace DiscoBot.Auxiliary.Calculator
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// The StringSolver divides the calculation string into operations and SubStringSolvers if the string contains parentheses
/// </summary>
public class StringSolver : ISolvable
{
public readonly string input;
public readonly List<object> arguments = new List<object>();
public StringSolver(string input)
{
this.input = input;
}
public override string ToString()
{
return "(0+" + this.input.Replace(" ", string.Empty).ToLower() + ")";
}
public int Solve()
{
string workInput = "0+" + this.input.Replace(" ", string.Empty).ToLower();
workInput = ExpandParentheses(workInput);
// Create a List of the different parts of the calculation, e.g.:{"0", "+", "(5+6)", "d", "3"}.
this.AtomizeOperations(workInput);
// traverse the List in order of Operation to Create the binary operation tree .
this.NestOperations();
// the List now contains only the top operation node, witch can be solved recursively,
return ((ISolvable)this.arguments.First()).Solve();
}
public static string GetInner(ref string input) // extract the inner bracket an remove the section from the input string
{
int depth = 0;
for (var index = 1; index < input.Length; index++)
{
char c = input[index];
switch (c)
{
case '(':
depth++;
break;
case ')':
if (depth == 0)
{
var split = input.Substring(1, index - 1);
input = input.Substring(index + 1);
return split.Equals(string.Empty) ? "0" : split;
}
else
{
depth--;
}
break;
}
}
return string.Empty;
}
public static Ops GetOps(char c)
{
switch (c)
{
case 'd':
case 'w':
return Ops.Dice;
case '+':
return Ops.Add;
case '-':
return Ops.Subtract;
case '*':
return Ops.Multiply;
default:
return Ops.Multiply;
}
}
public static string ExpandParentheses(string input) // insert * between Parentheses and digits
{
for (int i = 0; i < input.Length - 1; i++)
{
if (input[i + 1] == '(' && char.IsNumber(input[i]))
{
input = input.Insert(i + 1, "*");
}
}
for (int i = 1; i < input.Length; i++)
{
if (input[i - 1] == ')' && char.IsNumber(input[i]))
{
input = input.Insert(i, "*");
}
}
return input;
}
public void AtomizeOperations(string workInput)
{
for (var index = 0; index < workInput.Length; index++)
{
char c = workInput[index];
if (char.IsNumber(c))
{
// if char number, check if at end of string, else continue looping
if (index == workInput.Length - 1)
{
// if at end of string; add remaining number to arguments
this.arguments.Add(new Argument(workInput.Substring(0, index + 1)));
}
continue;
}
switch (c)
{
case ')':
throw new ArgumentException($"Unmögliche Anordnung von Klammern");
case '(':
this.arguments.Add(new StringSolver(GetInner(ref workInput)));
index = -1;
break;
default:
if (index > 0)
{
this.arguments.Add(new Argument(workInput.Substring(0, index)));
}
this.arguments.Add(GetOps(c));
workInput = workInput.Remove(0, index + 1);
index = -1;
break;
}
}
}
public void NestOperations()
{
foreach (Ops currentOp in Enum.GetValues(typeof(Ops)))
{
// cycle through operators in operational order
for (var index = 0; index < this.arguments.Count; index++)
{
var arg = this.arguments[index];
if (arg.GetType() != typeof(Ops))
{
continue;
}
// arg is of type Ops
var op = (Ops)arg;
if (op != currentOp)
{
continue;
}
// arg describes the current operation
this.HandleSpecialFormatting(ref index, op); // Deal with special needs...
// replace the previous current and next Element in the List with one Operation object
var temp = new Operator((ISolvable)this.arguments[index - 1], (ISolvable)this.arguments[index + 1], op);
this.arguments[index - 1] = temp;
this.arguments.RemoveRange(index, 2);
index--;
}
}
}
public void HandleSpecialFormatting(ref int index, Ops op)
{
var arg1 = this.arguments[index - 1];
if (arg1.GetType() == typeof(Ops))
{
if (op == Ops.Dice)
{
this.arguments.Insert(index++, new Argument("1")); // w6 -> 1w6
}
if (op == Ops.Subtract)
{
this.arguments.Insert(index++, new Argument("0")); // +-3 -> +0-3
}
}
var arg2 = this.arguments[index + 1]; // 3+-5 -> 3+(0-5)
if (arg2.GetType() == typeof(Ops))
{
this.arguments[index + 1] = new Operator(new Argument("0"), (ISolvable)this.arguments[index + 2], (Ops)arg2);
this.arguments.RemoveAt(index + 2);
}
}
}
}
|