-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcurate.h
198 lines (159 loc) · 4.92 KB
/
calcurate.h
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
#ifndef _CALCURATE_H_
#define _CALCURATE_H_
#define DIV 1
#define MUL 2
#define PLS 3
#define MNS 4
#define NO_EXP 0
// Process expressions
int expression_processor(
string it,
bool *exists_mul_and_div,
bool *exists_pls_and_mns,
bool go_through )
{
int exptype = NO_EXP;
if (it == "*") {
exptype = MUL;
*exists_mul_and_div = true;
} else if (it == "/") {
exptype = DIV;
*exists_mul_and_div = true;
}
if (!*exists_mul_and_div && go_through) {
if (it == "+") {
exptype = PLS;
*exists_pls_and_mns = true;
} else if (it == "-") {
exptype = MNS;
*exists_pls_and_mns = true;
}
}
return exptype;
}
void exp_divider(
list<string> *exp_array,
list<string>::iterator *it,
struct EXP_DIVIDER_RESULT *exp_result )
{
string buf;
*it = exp_array->erase(*it);
buf = *(*it);
exp_result->right_value = _stoi(buf);
*it = exp_array->erase(*it);
*(*it)--;
buf = *(*it);
exp_result->left_value = _stoi(buf);
}
int exec_calcurate(int exptype, struct EXP_DIVIDER_RESULT divider_result)
{
int let = 0;
int right = divider_result.right_value;
int left = divider_result.left_value;
switch (exptype) {
case MUL: let = right * left; break;
case DIV: let = left / right; break;
case PLS: let = left + right; break;
case MNS: let = left - right; break;
default: let = (int)NULL; break;
}
return let;
}
int sub_calcurate(struct PROGRESSION_FLAGS *pflags)
{
struct EXP_DIVIDER_RESULT edr;
int right, left, let;
int exptype;
exptype = expression_processor(
*pflags->it, &pflags->exists_mul_and_div,
&pflags->exists_pls_and_mns, pflags->go_through
);
if (exptype != NO_EXP) {
exp_divider(&pflags->expressions, &pflags->it, &edr);
right = edr.right_value;
left = edr.left_value;
if (right == -1 || left == -1) {
return -1;
}
let = exec_calcurate(exptype, edr);
if (let == (int)NULL) {
return -2;
}
pflags->expressions.insert(pflags->it, _itos(let));
pflags->it = pflags->expressions.erase(pflags->it);
return 1;
}
return 0;
}
int calcurate(list<string>::iterator begin, list<string>::iterator end)
{
struct PROGRESSION_FLAGS pflags;
int result;
pflags.it = begin;
for (;pflags.it != end;pflags.it++) {
pflags.expressions.push_back(*pflags.it);
}
pflags.go_through = false;
cout << "(" << stringify_list(pflags.expressions) << ")" << endl;
while (1) {
pflags.exists_mul_and_div = false;
pflags.exists_pls_and_mns = false;
pflags.it = pflags.expressions.begin();
while (pflags.it != pflags.expressions.end()) {
result = sub_calcurate(&pflags);
if (result == 1) {
cout << "= (" << stringify_list(pflags.expressions) << ")" << endl;
} else if (result < 0) {
cout << "Error(" << result << ")" << endl;
}
pflags.it++;
}
if (pflags.go_through &&
!pflags.exists_mul_and_div &&
!pflags.exists_pls_and_mns) {
break;
}
// processing for subtraction and addition is not going
// to be triggerd off before going-through expression
// processing has not completed once.
pflags.go_through = true;
}
return _stoi(*pflags.expressions.begin());
}
void build_expressions(string expressions, struct PROGRESSION_FLAGS *pflags)
{
string::size_type current, prev, pos_plus, pos_minus, pos_multi, pos_div;
string::size_type pos_bracket_begin, pos_bracket_end;
string substr_buffer;
bool noexp;
current = 0;
noexp = false;
while (1) {
pos_plus = expressions.find("+", current);
pos_minus = expressions.find("-", current);
pos_multi = expressions.find("*", current);
pos_div = expressions.find("/", current);
pos_bracket_begin = expressions.find("(", current);
pos_bracket_end = expressions.find(")", current);
if (pos_plus == string::npos &&
pos_minus == string::npos &&
pos_multi == string::npos &&
pos_div == string::npos &&
pos_bracket_begin == string::npos &&
pos_bracket_end == string::npos) {
noexp = true;
}
// number
prev = current;
current = min(pos_plus, min(pos_minus, min(pos_multi,
min(pos_div, min(pos_bracket_begin, pos_bracket_end)))));
pflags->expressions.push_back(expressions.substr(prev, current - prev));
// expression
if (noexp) break;
pflags->expressions.push_back(expressions.substr(current, 1));
current++;
}
cout << "> " << stringify_list(pflags->expressions) << endl;
return;
}
#endif