forked from psemiletov/tea-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libretta_calc.cpp
247 lines (177 loc) · 4.86 KB
/
libretta_calc.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
#include <string>
#include <iostream>
#include <sstream>
#include <list>
#include <algorithm>
#include <iterator>
#include <ctype.h>
#include <locale>
#include <math.h>
#include "utils.h"
using namespace std;
class CItem
{
public:
char op;
double val;
CItem (char a_op, double a_val);
};
CItem::CItem (char a_op, double a_val)
{
op = a_op;
val = a_val;
}
double calculate (string expression)
{
list <CItem> items;
lconv *l = localeconv();
string sep_need = l->decimal_point;
string sep_find;
if (sep_need == ".")
sep_find = ",";
else
sep_find = ".";
size_t position = expression.find (sep_find);
while (position != string::npos)
{
expression.replace (position, 1, sep_need);
position = expression.find (sep_find, position + 1);
}
//open braces
size_t end_pos = expression.find (')');
// cout << " end pos = " << end_pos << endl;
size_t start_pos = 0;
if (end_pos != string::npos)
do
{
for (size_t i = end_pos; i-- > 0 ;)
{
if (expression[i] == '(')
{
start_pos = i;
break;
}
}
string s_temp_value = expression.substr (start_pos + 1, end_pos - start_pos - 1);
//cout << "start_pos = " << start_pos << " end pos = " << end_pos << endl;
//cout << "s_temp_value = " << s_temp_value << endl;
double f_temp_value = calculate (s_temp_value);
std::ostringstream float_stream;
float_stream << f_temp_value;
string temp_s (float_stream.str());
//cout << "temp_s = " << temp_s << endl;
expression = expression.replace (start_pos + 1, end_pos - start_pos - 1, temp_s);
}
while (end_pos == string::npos);
//parse expression to list:
bool new_operator = false;
string t_operand;
char t_operator = '0';
size_t stop_size = expression.length() - 1;
for (size_t i = 0; i < expression.length(); i++)
{
char t = expression[i];
if (isdigit (t) || t == '.' || t == ',')
t_operand += t;
if (t == '+' || t == '-' ||
t == '/' || t == '*' ||
t == '^' || t == '%' ||
i == stop_size)
{
new_operator = true;
t_operator = t;
if (i == stop_size)
t_operator = '0';
}
if (new_operator)
{
//добавляем в items
double f = atof (t_operand.c_str());
items.push_back (CItem (t_operator, f));
t_operand = "";
t_operator = '0';
new_operator = false;
}
}
list<CItem>::iterator p = items.begin();
//степень и процент
do {
CItem current = *p;
list<CItem>::iterator t = p;
++t;
CItem next = *t;
if (current.op == '^' || current.op == '%')
{
if (current.op == '^')
{
next.val = pow (current.val, next.val);
*t = next;
}
else
if (current.op == '%')
{
next.val = (float) get_value (current.val, next.val);
*t = next;
}
p = items.erase (p);
continue;
}
++p;
}
while (p != items.end());
//умножаем и делим
p = items.begin();
do {
CItem current = *p;
list<CItem>::iterator t = p;
++t;
CItem next = *t;
if (current.op == '*' || current.op == '/')
{
if (current.op == '*')
{
next.val = current.val * next.val;
*t = next;
}
else
if (current.op == '/')
{
next.val = current.val / next.val;
*t = next;
}
p = items.erase (p);
continue;
}
++p;
}
while (p != items.end());
//складываем и вычитаем
p = items.begin();
do {
CItem current = *p;
list<CItem>::iterator t = p;
++t;
CItem next = *t;
if (current.op == '+' || current.op == '-')
{
if (current.op == '+')
{
next.val = current.val + next.val;
*t = next;
}
else
if (current.op == '-')
{
next.val = current.val - next.val;
*t = next;
}
p = items.erase (p);
continue;
}
++p;
}
while (p != items.end());
list<CItem>::iterator start = items.begin();
CItem item = *start;
return item.val;
}