-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrecalc.cpp
executable file
·365 lines (314 loc) · 11.8 KB
/
krecalc.cpp
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
364
365
#include"krecalc.h"
bool isNumber(const char c) {
return ('0' <= c && c <= '9') || (c == '.');
}
bool isText(const char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
bool isInteger(const f64 x) {
f64 integerPart;
return std::modf(x, &integerPart) == 0;
}
bool isInteger(const f64 x, f64 &integerPart) {
return std::modf(x, &integerPart) == 0;
}
f64 factorial(std::string str) {
f64 cnt = stold(str), res = 1;
if(!isInteger(cnt, cnt)) throw "Factorial cannot be computed for non-integers";
for(int i = 2; i <= (int)cnt; ++i) res *= i;
return res;
}
// Debugging tool
std::ostream& operator<<(std::ostream& o, const token& t)
{
o << "type: " << t.type << ", Value: " << t.value;
if(!t.extra_data.empty())
{
o << ", extra_data: ";
for(std::vector<token> field : t.extra_data) {
o << "{";
for(token tk : field) {
o << tk << "; ";
}
o << "}; ";
}
}
return o;
}
std::vector<token> tokenizeString(std::string str) {
std::vector<token> tokens;
std::string tempString;
token tempToken;
for(int i = 0; i < (int)str.size(); ++i) {
tempToken = {None, ""};
switch(str[i]) {
case ' ': continue; break;
case Addition: tempToken.type = Addition; break;
case Subtraction: tempToken.type = Subtraction; break;
case Multiplication: tempToken.type = Multiplication; break;
case Division: tempToken.type = Division; break;
case Power: tempToken.type = Power; break;
case Modulo: tempToken.type = Modulo; break;
case Factorial: tempToken.type = Factorial; break;
case LeftBracket: tempToken.type = LeftBracket; break;
case RightBracket: tempToken.type = RightBracket; break;
case Comma: tempToken.type = Comma; break;
default:
tempString = str[i];
//Get all characters in Text
if(isText(str[i])) {
while(i+1 < (int)str.size() && isText(str[i+1])) tempString += str[++i];
tempToken = {Text, tempString};
break;
}
//Get all digits of a number
else if(isNumber(str[i])) {
while(i+1 < (int)str.size() && isNumber(str[i+1])) tempString += str[++i];
tempToken = {Number, tempString};
break;
}
throw tempString + " is not a valid token";
break;
}
tokens.emplace_back(tempToken);
}
return tokens;
}
//Match a pattern
bool match(std::vector<token>::iterator it, std::vector<token>::iterator end, std::vector<tokenType> matchType, std::vector<std::string> matchValue = {}) {
int k = 0;
for(int i = 0; i < (int)matchType.size(); ++i) {
if(it == end) return 0;
if(it->type != matchType[i]) return 0;
if(it->type == Text && !matchValue.empty() && it->value != matchValue[k++]) return 0;
++it;
}
return 1;
}
void addValueAndRemove(std::vector<token> &t, std::vector<token>::iterator &it, int count) {
std::vector<token>::iterator temp = it; it++;
while(count--) {
temp->value += it->value;
it = t.erase(it);
}
}
void computeFunction(std::vector<token> &t) {
/* for(token& tk : t) std::cout << tk << std::endl; */
/* std::cout << "\n-----\n"; */
std::vector<token>::iterator it = t.begin(), functionIterator;
bool foundAny = 0;
if(t.empty()) return;
while(it != t.end()) {
if(match(it, t.end(), {Text, LeftBracket})) {
foundAny = 1;
functionIterator = it;
functionIterator->type = Function;
it++; it = t.erase(it);
int bracketCountInFunction = 1;
std::vector<token> functionSlot = {};
while(it != t.end() && bracketCountInFunction != 0) {
switch(it->type) {
case LeftBracket:
bracketCountInFunction++;
functionSlot.push_back(*it);
break;
case RightBracket:
bracketCountInFunction--;
if(bracketCountInFunction == 0) {
functionIterator->extra_data.push_back(functionSlot);
functionSlot.clear();
break;
}
functionSlot.push_back(*it);
break;
case Comma:
if(bracketCountInFunction > 1) {
functionSlot.push_back(*it);
break;
}
functionIterator->extra_data.push_back(functionSlot);
functionSlot.clear();
break;
default:
functionSlot.push_back(*it);
break;
}
it = t.erase(it);
}
if(it == t.end()) break;
it++;
}
else it++;
}
//Compute Functions in Function 💀
if(!foundAny) return;
for(std::vector<token> &data : functionIterator->extra_data) computeFunction(data);
}
void verifyAndFixTokens(std::vector<token> &t) {
std::vector<token>::iterator it;
int bracketDiff = 0;
for(int i = 0; i < (int)t.size(); ++i) {
if(bracketDiff < 0) throw "Not all brackets are closed";
if(t[i].type == LeftBracket) bracketDiff++;
if(t[i].type == RightBracket) bracketDiff--;
}
if(bracketDiff != 0) throw "Not all brackets are closed";
//eg. 2e4
it = t.begin();
while(it != t.end()) {
if(match(it, t.end(), {Number, Text, Number}, {"e"})) addValueAndRemove(t, it, 2);
else it++;
}
/* //eg. 2e+4 or 2e-4 */
it = t.begin();
while(it != t.end()) {
if(match(it, t.end(), {Number, Text, Addition, Number}, {"e"})
|| match(it, t.end(), {Number, Text, Subtraction, Number}, {"e"})) addValueAndRemove(t, it, 3);
else it++;
}
//eg. 2(...) -> 2*(...)
it = t.begin();
while(it != t.end()) {
if(match(it, t.end(), {Number, LeftBracket})) t.insert(++it, {Multiplication, "*"});
else it++;
}
//Factorial
for(it = t.begin(); it != t.end();) {
if(it->type == Factorial) {
std::vector<token> data;
it--;
int bracketCount = (it->type == RightBracket);
data.push_back(*it);
while(it != t.begin() && bracketCount) {
it--;
if(it->type == RightBracket) bracketCount++;
else if(it->type == LeftBracket) bracketCount--;
data.push_back(*it);
}
while(it->type != Factorial && it != t.end()) it = t.erase(it);
std::reverse(data.begin(), data.end());
it->value = "Factorial";
it->type = Function;
it->extra_data.push_back(data);
it++;
}
else it++;
}
//Negative Numbers
it = t.begin();
if(match(it, t.end(), {Subtraction, Number})) { // First Token is -x
it = t.erase(it);
it->value = "-" + it->value;
it++;
}
while(it != t.end()) {
if(it->isOperator()) {
it++;
if(match(it, t.end(), {Subtraction, Number})) {
it = t.erase(it);
it->value = "-" + it->value;
it++;
}
}
else it++;
}
//text ( data1 , data2 , datan )
//Functions
computeFunction(t);
//Two operators next to each other
for(int i = 0; i+1 < (int)t.size(); ++i) {
if(t[i].isOperator() && t[i+1].isOperator()) throw "Tokens " + t[i].value + " and " + t[i+1].value + " cannot be next to each other";
}
for(int i = 0; i < (int)t.size(); ++i) if(t[i].type == Text || t[i].type == None) throw t[i].value + " is not a valid token";
}
std::vector<orderedToken> buildOrderTable(std::vector<token> &tokens) {
std::vector<orderedToken> order;
int bracketCount = 0, orderNum = 0;
for(token t : tokens) {
if(t.type == LeftBracket) bracketCount += BRACKET_MULTIPLIER;
else if(t.type == RightBracket) bracketCount -= BRACKET_MULTIPLIER;
else if(t.type == Number || t.type == Function) order.push_back({t, 100000}); //Asign a big numbur to non-operators
else {
switch(t.type) {
case Addition:
case Subtraction:
orderNum = 0;
break;
case Multiplication: case Division:
case Modulo:
orderNum = 1;
break;
case Power:
orderNum = 2;
break;
default: break;
}
order.push_back({t, orderNum + bracketCount});
}
}
return order;
}
int makeTree(int left, int right, std::vector<node> &tree, std::vector<orderedToken> tokens) {
// New node pushed onto the structure
node tempNode;
// Either a number or a function of some kind
if(left == right) {
if(tokens[left].t.type == Function) {
tempNode.type = Function;
tempNode.value = tokens[left].t.value;
for(std::vector<token> &data : tokens[left].t.extra_data) {
tempNode.children.push_back(makeTree(0, data.size()-1, tree, buildOrderTable(data)));
}
}
else if(tokens[left].t.type == Number) {
tempNode.type = Number;
tempNode.value = tokens[left].t.value;
}
}
// The token is an operator
// The next position is going to be the first token with the minimum order
else {
int minimum = INT_MAX, nextPosition = 0;
for(int i = right; i >= left; --i) minimum = std::min(minimum, tokens[i].order);
for(int i = right; i >= left; --i) if(tokens[i].order == minimum) {
nextPosition = i;
break;
}
tempNode.type = tokens[nextPosition].t.type;
tempNode.value = tokens[nextPosition].t.value;
tempNode.children.push_back(makeTree(left, nextPosition-1, tree, tokens)); // Left Branch
tempNode.children.push_back(makeTree(nextPosition+1, right, tree, tokens)); // Right Branch
}
tree.push_back(tempNode);
return tree.size()-1;
}
f64 calculateAnswer(int idx, std::vector<node> tree) {
node tempNode = tree[idx];
if(tempNode.type == Number) return stold(tempNode.value);
else if(tempNode.type == Factorial) return factorial(tempNode.value);
f64 leftAns = calculateAnswer(tempNode.children[0], tree);
f64 rightAns = calculateAnswer(tempNode.children[1], tree);
switch(tempNode.type) {
case Addition:
return leftAns + rightAns;
break;
case Subtraction:
return leftAns - rightAns;
break;
case Multiplication:
return leftAns * rightAns;
break;
case Division:
return leftAns / rightAns;
break;
case Power:
return pow(leftAns, rightAns);
break;
case Modulo:
if(!isInteger(leftAns) || !isInteger(rightAns)) throw "Modulus operation only works for integers";
return (i64)leftAns % (i64)rightAns;
break;
default:
return 0;
}
}