This repository was archived by the owner on Sep 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlibstringmath.hpp
More file actions
298 lines (214 loc) · 10 KB
/
Copy pathlibstringmath.hpp
File metadata and controls
298 lines (214 loc) · 10 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
// version 1.1-c2
#pragma once
#include <string>
#include <cmath>
#include <map>
#include <vector>
#include <algorithm>
#include "libstrmanip.hpp"
namespace string_math {
enum TokenType {
EXPRESSION,
POWER,
UNARY_MINUS,
MUL,
DIV,
PLUS,
MINUS,
NUMBER,
UNDEFINED
};
std::map<TokenType, int> tokenPriority = {{EXPRESSION, 0}, {POWER, 1}, {UNARY_MINUS, 1}, {MUL, 3}, {DIV, 3}, {PLUS, 4}, {MINUS, 4}, {NUMBER, 5}};
std::map<char, TokenType> mathOps = {{'+', PLUS}, {'-', MINUS}, {'*', MUL}, {'/', DIV}, {'^', POWER}};
struct MathToken {
TokenType type;
std::string value;
double num_value;
bool pow_seq;
bool good;
MathToken() {};
MathToken(std::string str, TokenType type) : value(str), type(type) {}
MathToken(double num) : num_value(num), type(NUMBER) {}
MathToken(bool good) : good(good) {}
};
using math_vars = std::map<std::string, double>;
using math_tokens = std::vector<MathToken>;
// void printTokens(math_tokens tokens) {
// for (auto i : tokens) {
// std::cout << "token type: " << i.type << " value: " << ((i.type == NUMBER) ? std::to_string(i.num_value) : i.value) << std::endl;
// }
// }
bool is_index(math_tokens tokens, int64_t index) {
return index >= 0 && index < tokens.size();
}
bool is_operator(MathToken token) {
return token.type == UNARY_MINUS || token.type == POWER || token.type == MUL || token.type == DIV || token.type == PLUS || token.type == MINUS;
}
bool is_pow_exp(math_tokens& tokens, int64_t i, bool invert) {
if (i + 2 >= tokens.size()) return false;
else if (invert && i - 2 < 0) return false;
if (invert) return tokens[i].type == NUMBER && tokens[i - 1].type == POWER;
return tokens[i].type == NUMBER && tokens[i + 1].type == POWER;
}
bool is_pow_seq(math_tokens& tokens, int64_t i) {
int64_t power_count = 0;
for (; i + 1 < tokens.size(); i++) {
if (tokens[i].type != UNARY_MINUS && tokens[i].type != NUMBER && tokens[i].type != POWER && tokens[i].type != EXPRESSION) break;
else if ((tokens[i].type == NUMBER || tokens[i].type == EXPRESSION) && tokens[i + 1].type == POWER) power_count++;
}
return power_count > 1;
}
int64_t get_pow_seq_end(math_tokens& tokens, int64_t i) {
for (; i < tokens.size(); i++)
if (tokens[i].type != UNARY_MINUS && tokens[i].type != NUMBER && tokens[i].type != POWER && tokens[i].type != EXPRESSION) break;
return i;
}
void __math_set_vars(math_tokens& tokens, math_vars& vars) {
for (auto& i : tokens) if (vars.find(i.value) != vars.end()) i = vars.at(i.value);
}
std::string __parse_number(std::string& string, int64_t& pos) {
std::string ret;
for (; pos < string.size(); pos++) {
if (!isdigit(string[pos]) && string[pos] != '.') break;
ret += string[pos];
}
pos--;
return ret;
}
std::string __parse_expression(std::string& string, int64_t& pos) {
std::string ret;
int64_t brackets = 0;
for (; pos < string.size(); pos++) {
char ch = string[pos];
if (ch == '(') brackets++;
else if (ch == ')') brackets--;
if (ch == '(' && brackets == 1) continue;
if (!brackets) break;
ret += ch;
}
return ret;
}
std::string __parse_undefined(std::string& string, int64_t& pos) {
std::string ret;
for (; pos < string.size() && isalpha(string[pos]); pos++) ret += string[pos];
pos--;
return ret;
}
math_tokens __math_parse(std::string& string) {
replaceAll(string, " ", "");
replaceAll(string, "\r", "");
replaceAll(string, "\n", "");
replaceAll(string, "\t", "");
math_tokens tokens;
for (int64_t i = 0; i < string.size(); i++) {
char ch = string[i];
if (isdigit(ch)) tokens.push_back(std::stod(__parse_number(string, i)));
else if (mathOps.find(ch) != mathOps.end()) tokens.push_back({std::string(1, ch), mathOps.at(ch)});
else if (ch == '(') tokens.push_back({__parse_expression(string, i), EXPRESSION});
else tokens.push_back({__parse_undefined(string, i), UNDEFINED});
}
return tokens;
}
void __math_preprocess(math_tokens& tokens) {
for (int64_t i = 0; is_index(tokens, i); i++)
if (tokens[i].type == MINUS && (!i || is_index(tokens, i - 1) && is_operator(tokens[i - 1]))) tokens[i].type = UNARY_MINUS;
for (int64_t i = 0; is_index(tokens, i); i++) {
if (tokens[i].type == POWER) tokens[i].pow_seq = false;
else if (is_pow_seq(tokens, i)) {
auto i_end = get_pow_seq_end(tokens, i);
std::reverse(tokens.begin() + i, tokens.begin() + i_end);
for (; i != i_end; i++) if (tokens[i].type == POWER) tokens[i].pow_seq = true;
}
}
}
double solve(std::string, std::map<std::string, double> = { {"p", M_PI}, {"e", M_E} });
double __math_calculate(math_tokens& tokens, int priority, math_vars vars) {
if (!tokens.size()) return 0;
if (priority == tokenPriority[NUMBER]) return tokens.front().num_value;
int64_t curPriorOps = 0;
for (int64_t i = 0; is_index(tokens, i); i++)
if (tokenPriority.at(tokens[i].type) == priority) curPriorOps++;
for (int64_t i = 0; i < tokens.size(); i++) {
if (tokenPriority.at(tokens[i].type) == priority) {
if (tokens[i].type == EXPRESSION) {
tokens[i] = solve(tokens[i].value, vars);
curPriorOps--;
}
else if (tokens[i].type == UNARY_MINUS && is_index(tokens, i + 1) && tokens[i + 1].type == NUMBER && !is_pow_exp(tokens, i + 1, false)) {
tokens[i] = -tokens[i + 1].num_value;
tokens.erase(tokens.begin() + i + 1);
curPriorOps--;
}
else if (tokens[i].type == UNARY_MINUS && is_index(tokens, i - 1) && tokens[i - 1].type == NUMBER && !is_pow_exp(tokens, i - 1, true)) {
tokens[i] = -tokens[i - 1].num_value;
tokens.erase(tokens.begin() + i - 1);
i--;
curPriorOps--;
}
else if (tokens[i].type == UNARY_MINUS && is_index(tokens, i + 1) && tokens[i + 1].type == UNARY_MINUS) {
tokens.erase(tokens.begin() + i + 1);
tokens.erase(tokens.begin() + i);
i--;
curPriorOps -= 2;
}
else if (tokens[i].type == POWER && is_index(tokens, i - 1) && tokens[i - 1].type == NUMBER && is_index(tokens, i + 1) && tokens[i + 1].type == NUMBER) {
double op1 = tokens[i - 1].num_value;
double op2 = tokens[i + 1].num_value;
if (tokens[i].pow_seq) {
op1 = tokens[i + 1].num_value;
op2 = tokens[i - 1].num_value;
}
tokens.erase(tokens.begin() + i + 1);
tokens.erase(tokens.begin() + i - 1);
i--;
curPriorOps--;
tokens[i] = pow(op1, op2);
}
else if (tokens[i].type == MUL && is_index(tokens, i - 1) && tokens[i - 1].type == NUMBER && is_index(tokens, i + 1) && tokens[i + 1].type == NUMBER) {
double op1 = tokens[i - 1].num_value;
double op2 = tokens[i + 1].num_value;
tokens.erase(tokens.begin() + i + 1);
tokens.erase(tokens.begin() + i - 1);
i--;
curPriorOps--;
tokens[i] = op1 * op2;
}
else if (tokens[i].type == DIV && is_index(tokens, i - 1) && tokens[i - 1].type == NUMBER && is_index(tokens, i + 1) && tokens[i + 1].type == NUMBER) {
double op1 = tokens[i - 1].num_value;
double op2 = tokens[i + 1].num_value;
tokens.erase(tokens.begin() + i + 1);
tokens.erase(tokens.begin() + i - 1);
i--;
curPriorOps--;
tokens[i] = op1 / op2;
}
else if (tokens[i].type == PLUS && is_index(tokens, i - 1) && tokens[i - 1].type == NUMBER && is_index(tokens, i + 1) && tokens[i + 1].type == NUMBER) {
double op1 = tokens[i - 1].num_value;
double op2 = tokens[i + 1].num_value;
tokens.erase(tokens.begin() + i + 1);
tokens.erase(tokens.begin() + i - 1);
i--;
curPriorOps--;
tokens[i] = op1 + op2;
}
else if (tokens[i].type == MINUS && is_index(tokens, i - 1) && tokens[i - 1].type == NUMBER && is_index(tokens, i + 1) && tokens[i + 1].type == NUMBER) {
double op1 = tokens[i - 1].num_value;
double op2 = tokens[i + 1].num_value;
tokens.erase(tokens.begin() + i + 1);
tokens.erase(tokens.begin() + i - 1);
i--;
curPriorOps--;
tokens[i] = op1 - op2;
}
}
if (i + 1 >= tokens.size() && curPriorOps) i = -1;
}
return __math_calculate(tokens, priority + 1, vars);
}
double solve(std::string string, math_vars vars) {
math_tokens tokens = __math_parse(string);
__math_set_vars(tokens, vars);
__math_preprocess(tokens);
return __math_calculate(tokens, 0, vars);
}
}