-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
371 lines (327 loc) · 11.1 KB
/
main.cpp
File metadata and controls
371 lines (327 loc) · 11.1 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
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
366
367
368
369
370
371
#include <iostream>
#include <vector>
#include <string_view>
#include <variant>
#include <deque>
#include <algorithm>
#include <cmath>
#include <unordered_map>
enum class OPenum{
NOOP,
PLUS,
MIN,
MULT,
DIV,
EXP,
};
enum class NUMenum{
NONUM,
NUM,
DOT
};
NUMenum is_char_numeric(char const & c){
int num = int(c);
if (num>=48 && num <= 57){
return NUMenum::NUM;
}
else if (num == 46){
return NUMenum::DOT;
}
return NUMenum::NONUM;
};
OPenum is_char_operator(char const& c){
switch(c){
case '+': return OPenum::PLUS; break;
case '-': return OPenum::MIN; break;
case '*': return OPenum::MULT; break;
case '/': return OPenum::PLUS; break;
case '^': return OPenum::EXP; break;
}
return OPenum::NOOP;
}
struct OP{
OPenum op = OPenum::NOOP;
OP(char const & c) {
switch(c){
case '+': op = OPenum::PLUS; break;
case '-': op = OPenum::MIN; break;
case '*': op = OPenum::MULT; break;
case '/': op = OPenum::DIV; break;
case '^': op = OPenum::EXP; break;
default : op = OPenum::NOOP; break;
}
}
friend std::ostream& operator<<(std::ostream& o, OP const & opp){
char out;
switch(opp.op){
case OPenum::PLUS: out = '+'; break;
case OPenum::MIN: out = '-'; break;
case OPenum::MULT: out = '*'; break;
case OPenum::DIV: out = '/'; break;
case OPenum::EXP: out = '^'; break;
default : out = 'n'; break;
}
return o << out;
}
};
struct VAL{
float value{0};
VAL(std::string const& valuestring){
value = atof(valuestring.c_str());
}
VAL() = default;
friend std::ostream& operator<<(std::ostream& o, VAL const & v){
return o << v.value;
}
};
void evaluate(VAL* left, OP * op, VAL * right){
float value{};
switch(op->op){
case OPenum::EXP : value = std::pow(left->value,right->value); break;
case OPenum::MULT : value = left->value * right->value; break;
case OPenum::DIV : value = left->value / right->value; break;
case OPenum::PLUS : value = left->value + right->value; break;
case OPenum::MIN : value = left->value - right->value; break;
default : value = left->value; break;
}
left->value = value;
//after evaluation call delete for op and right elements
}
struct Expression{
using expTree = std::vector<std::variant<std::string,Expression*>>;
expTree data{};
using parsedTree = std::vector<std::variant<OP*,VAL*,Expression*>>;
parsedTree p_data{};
static std::unordered_map<OPenum,int> op_order;
void parse();
void fill_implicit_mult();
void calculate();
};
// order of operations
std::unordered_map<OPenum,int> Expression::op_order{{OPenum::NOOP, -1},
{OPenum::PLUS, 0},
{OPenum::MIN, 0},
{OPenum::MULT, 1},
{OPenum::DIV,1},
{OPenum::EXP,2}, };
void print_exp_simple(Expression * exp){
for(auto const & el : exp->p_data){
int i = el.index();
if (i == 0){
std::cout << "[OP " << *(std::get<OP*>(el)) << "]";
}
else if (i == 1){
std::cout << "[VAL " << *(std::get<VAL*>(el)) << "]";
} else if (i == 2){
std::cout<< "[EXP]";
}
std::cout << ' ';
}
std::cout << "\n";
}
void Expression::fill_implicit_mult(){
std::deque<Expression::parsedTree::iterator> insert_pos;
int offset{0};
for(auto i = p_data.begin(); i != p_data.end(); i++){
auto cur_idx = (*i).index();
auto nxt_idx = (*(i+1)).index();
if( (cur_idx == 1 && nxt_idx == 2) || (cur_idx == 1 && nxt_idx == 2) || (cur_idx == 2 && nxt_idx == 2) ){
std::cout << "inserting at " << i-p_data.begin() << "\n";
insert_pos.push_back(i+1);
}
}
while(!insert_pos.empty()){
OP * mult = new OP{'*'};
p_data.insert(insert_pos.back()+offset,mult);
offset++;
insert_pos.pop_back();
}
}
void parse_substring(Expression::parsedTree& data, std::string_view const & parse_string){
std::string catcher{};
bool handling_number = false;
bool num_hasdot = false;
int length = parse_string.length();
int pos = 0;
bool negate = false;
int last_pushed = -1;
for (char const & c : parse_string){
++pos;
if (is_char_numeric(c)==NUMenum::NUM || (is_char_numeric(c)==NUMenum::DOT && !num_hasdot)){
catcher += c;
if (!handling_number)
handling_number = true;
if (!num_hasdot && is_char_numeric(c)==NUMenum::DOT)
num_hasdot = true;
if (pos < length && is_char_numeric(parse_string[pos])!=NUMenum::NONUM){
continue;}
}
if (is_char_operator(c)!=OPenum::NOOP){
catcher += c;
}
if(pos == length || (handling_number && is_char_numeric(parse_string[pos])==NUMenum::NONUM) || (!handling_number && is_char_operator(c)!=OPenum::NOOP) ){
if (handling_number){
auto symbol = new VAL{catcher};
if (negate){
symbol->value *= -1;
negate = false;
}
data.push_back(symbol);
last_pushed = 0;
} else if (is_char_operator(c)!=OPenum::NOOP && ((last_pushed == -1 || last_pushed == 1)&& is_char_operator(c)==OPenum::MIN)){
negate = (negate) ? false : true;
} else if (is_char_operator(c)!=OPenum::NOOP){
auto symbol = new OP{c};
data.push_back(symbol);
last_pushed = 1;
}
handling_number = false;
num_hasdot = false;
catcher = "";
}
}
}
void Expression::parse(){
for(auto i = data.begin(); i !=data.end() ; i++){
//check type
auto el = *i;
if (el.index() == 0){
//parse contents and add to list
std::string_view new_parse_string = std::get<std::string>(el);
parse_substring(p_data,new_parse_string);
}
else if (el.index() == 1){
Expression* exp = std::get<Expression*>(el);
p_data.push_back(exp);
exp->parse();
};
}
fill_implicit_mult();
}
void Expression::calculate(){
using eltype = std::variant<OP*,VAL*,Expression*>;
auto hasOps = [](eltype& el){return el.index()==0;};
while(std::any_of(p_data.begin(),p_data.end(),hasOps)){
int max_p{-1};
int eval_idx{-1};
int idx{};
for(auto const& el : p_data){
if (el.index()==0){
OPenum cur = std::get<OP*>(el)->op;
int prescedence = op_order.at(cur);
if (prescedence > max_p){
eval_idx = idx;
max_p = prescedence;
}
}
idx++;
}
if (max_p != -1 && eval_idx != -1){
VAL* left = std::get<VAL*>(p_data[eval_idx-1]);
VAL* right = std::get<VAL*>(p_data[eval_idx+1]);
OP* operand = std::get<OP*>(p_data[eval_idx]);
std::cout << *left << *operand << *right << "\n";
evaluate(left,operand,right);
std::cout << " result: " << *left << "\n";
delete right;
delete operand;
p_data.erase(p_data.begin()+(eval_idx));
p_data.erase(p_data.begin()+(eval_idx));
}
}
}
void calculate_expression(Expression* head){
using eltype = std::variant<OP*,VAL*,Expression*>;
auto hasExp = [](eltype& el){return el.index()==2;};
std::deque<Expression *> stack {};
std::deque<Expression::parsedTree::iterator> it_stack;
bool br = false;
stack.push_back(head);
while(!stack.empty()){
bool exprLeft = std::any_of(stack.back()->p_data.begin(),stack.back()->p_data.end(),hasExp);
if (exprLeft) {
auto it_pt = std::find_if(stack.back()->p_data.begin(),stack.back()->p_data.end(),hasExp);
auto exp = std::get<Expression*>(*it_pt);
stack.push_back(exp);
it_stack.push_back(it_pt);
}
else {
stack.back()->calculate();
auto result = stack.back()->p_data[0];
if (it_stack.size()>0) {
*(it_stack.back()) = result;
it_stack.pop_back();
} else {
stack.front()->p_data[0] = result;
}
std::cout << "resolved: "<< std::get<VAL*>(result)->value <<"\n";
if (stack.size()>0) {
stack.pop_back();
}
}
if (stack.size()>0)
print_exp_simple(stack.back()); // useless printing but it is fun
}
}
Expression * parse_parens(std::string_view const & str){
auto head = new Expression();
std::deque<Expression *> stack {};
stack.push_back(head);
int open{0};
std::string collect{""};
for(auto i = str.begin();i!=str.end();i++){
char c = *i;
auto curExpr = stack.back();
if (c == '('){
auto newExpr = new Expression();
if (collect != ""){
curExpr->data.emplace_back(collect);
collect = "";
}
open++;
curExpr->data.push_back(newExpr);
stack.push_back(newExpr);
continue;
}
if (c == ')'){
if (collect != ""){
curExpr->data.push_back(collect);
collect = "";
}
open--;
if(open < 0){
std::cout << "too many closing parentheses.\n";
return nullptr;
}
stack.pop_back();
continue;
}
collect += c;
}
if (collect != ""){
stack.back()->data.push_back(collect);
}
if (open>0){
std::cout << "too many opening parentheses.\n";
return nullptr;
}
return head;
}
void parse_expression(std::string_view const & math_str){
std::cout << "parsing: " <<math_str << "\n";
auto x = parse_parens(math_str);
x->parse();
std::cout << "starting calculation...\n-----\n";
calculate_expression(x);
auto idx = x->p_data.front().index();
if (idx==2){
auto unpack = std::get<Expression*>(x->p_data.front());
std::cout << "FINAL RESULT: " << std::get<VAL*>(unpack->p_data.front())->value << "\n";
} else if (idx==1){
std::cout << "FINAL RESULT: " << std::get<VAL*>(x->p_data.front())->value << "\n";
}
};
int main(){
std::string_view str = "(5 + (4 + 2) + (5 + 10)(20) * (1 * (4+3)) / (2^3))";
parse_expression(str); // expected result = 273.5
}