-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.cpp
344 lines (272 loc) · 7.87 KB
/
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
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
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <limits>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
#define PI_VALUE "3.1415926535897932385"
#define E_VALUE "2.7182818284590452354"
/*-------------------------------------------------------------*/
string help = R"(Available operators: + addition
- subtraction
* multiplication
/ division
^ exponentiation
! factorial
% modulo
Available functions: abs(x) absolute value
sqrt(x) square root
ln(x) natural logarithm
log(x,y) logarithm in base x of y
sin(x) sine of angle in radians
cos(x) cosine of angle in radians
tan(x) tangent of angle in radians
asin(x) arcsine of angle in radians
acos(x) arccosine of angle in radians
atan(x) arctangent of angle in radians
Available parenthesis: { [ ( ) ] }
Available constants: pi ratio of a circumference to the diameter
e Euler’s constant
Other: -h to print this help)";
/*-------------------------------------------------------------*/
typedef double (*calc1)(double);
typedef double (*calc2)(double, double);
typedef vector <pair<string, string>> pairs;
/*-------------------------------------------------------------*/
double add (double x, double y){return x+y;}
double sub (double x, double y){return x-y;}
double mul (double x, double y){return x*y;}
double div (double x, double y){return x/y;}
double logb(double x, double y){return log(y)/log(x);}
double neg (double x){return -x;}
double fact(double x){return tgamma(x+1);}
/*-------------------------------------------------------------*/
void fail();
template <typename T> void push(T, vector <T> &);
template <typename T> void pop(T &, vector <T> &);
unsigned get_prec(char);
bool is_num(char);
bool is_func(char);
void replace_unary(char, char, string &);
void replace(const pairs &, string &);
string get_str(unsigned &, const string &);
double get_double(unsigned &, const string &);
string infix_to_postfix(const string &);
double evaluate_postfix(const string &);
/*-------------------------------------------------------------*/
map <char, calc1> map1 = {{'a', abs}, {'b', log}, {'c', sqrt}, {'d', asin},
{'f', acos}, {'g', atan}, {'h', sin}, {'i', cos},
{'j', tan}, {'k', neg}, {'l', fact}};
map <char, calc2> map2 = {{'+', add}, {'-', sub}, {'*', mul}, {'/', div},
{'^', pow}, {'%', fmod}, {'m', logb}};
pairs func = {{"abs", "a"}, {"ln", "b"}, {"sqrt", "c"}, {"asin", "d"},
{"acos", "f"}, {"atan", "g"}, {"sin", "h"}, {"cos", "i"},
{"tan", "j"}, {"log", "m"}};
pairs other = {{"!", "l"}, {"[", "("}, {"]", ")"}, {"{", "("}, {"}", ")"},
{"pi", PI_VALUE}, {"e", E_VALUE}};
/*-------------------------------------------------------------*/
int main(int argc, char * argv[]){
string in, rpn;
double res;
if (argc >= 2){
if (strcmp(argv[1], "-h") == 0){
cout << help << endl;
return 0;
} else {
in = argv[1];
}
} else {
cout << "Expression: ";
getline(cin, in);
}
if (in.length() == 0) fail();
replace(func, in);
replace(other, in);
replace_unary('-', 'k', in);
replace_unary('+', 0, in);
rpn = infix_to_postfix(in);
res = evaluate_postfix(rpn);
cout << "Result: " << setprecision(numeric_limits <double> ::digits10 + 1);
cout << res << endl;
}
/*-------------------------------------------------------------*/
void fail(){
cout << "Invalid expression (use -h to show the help)" << endl;
exit(1);
}
template <typename T> void push(T o, vector <T> &s){
s.push_back(o);
}
template <typename T> void pop(T &o, vector <T> &s){
o = s.back();
s.pop_back();
}
unsigned get_prec(char c){
switch(c){
case 'l': return 5;
case 'k': return 4;
case '^': return 4;
case '/': return 3;
case '*': return 3;
case '%': return 2;
case '-': return 1;
case '+': return 1;
default : return 0;
}
}
bool is_num(char c){
return ((c >= 48 && c <= 57) || c == '.') ? true : false;
}
bool is_func(char c){
string o;
for (unsigned w = 0; w < func.size(); w++){
o = func.at(w).second;
if (c == o.at(0)) return true;
}
return false;
}
void replace_unary(char from, char to, string &m){
char u;
for (unsigned i = 0; i < m.length(); i++){
if (m.at(i) == ' '){
m.erase(i--, 1);
} else if (m.at(i) == from){
if (i == 0){
m.at(i) = to;
} else {
u = m.at(i-1);
if ((get_prec(u) >= 1 && get_prec(u) <= 4) || (u == '(') || (u == ',')){
m.at(i) = to;
}
}
}
}
}
void replace(const pairs &v, string &s){
int begin;
string src, dst;
for (unsigned w = 0; w < v.size(); w++){
src = v.at(w).first;
dst = v.at(w).second;
for (unsigned i = 0; i < s.length(); i++){
begin = s.find(src, i);
if (begin == -1) break;
s.replace(begin, src.length(), dst);
}
}
}
string get_str(unsigned &end, const string &v){
string tot;
unsigned begin = end;
while ((end != v.length()-1) && is_num(v.at(end+1))) end++;
tot = v.substr(begin, end-begin+1);
return tot;
}
double get_double(unsigned &p, const string &v){
size_t i;
double tot;
try {
tot = stod(&v.at(p), &i);
} catch (invalid_argument){
fail();
}
p += i;
return tot;
}
string infix_to_postfix(const string &in){
string r1, r2, rpn;
vector <string> op_stack, rpn_stack;
for (unsigned i = 0; i < in.length(); i++){
r1 = in.at(i);
if (is_num(r1.at(0))){
r1 = get_str(i, in);
push(r1, rpn_stack);
} else {
if ((get_prec(r1.at(0)) >= 1) && (get_prec(r1.at(0)) <= 3)){
for (string i : op_stack){
r2 = op_stack.back();
if (get_prec(r1.at(0)) <= get_prec(r2.at(0))){
pop(r2, op_stack);
push(r2, rpn_stack);
}
}
push(r1, op_stack);
} else if (get_prec(r1.at(0)) >= 4){
for (string i : op_stack){
r2 = op_stack.back();
if (get_prec(r1.at(0)) < get_prec(r2.at(0))){
pop(r2, op_stack);
push(r2, rpn_stack);
}
}
push(r1, op_stack);
} else if ((r1.at(0) == '(') || is_func(r1.at(0))){
push(r1, op_stack);
} else if (r1.at(0) == ')'){
for (string i : op_stack){
r2 = op_stack.back();
if (r2.at(0) != '('){
pop(r2, op_stack);
push(r2, rpn_stack);
}
}
if (op_stack.empty() || (r2.at(0) != '(')) fail();
pop(r2, op_stack);
if (!op_stack.empty()){
r2 = op_stack.back();
if (is_func(r2.at(0))){
pop(r2, op_stack);
push(r2, rpn_stack);
}
}
} else if (r1.at(0) == ','){
for (string i : op_stack){
r2 = op_stack.back();
if (r2.at(0) != '('){
pop(r2, op_stack);
push(r2, rpn_stack);
}
}
if (op_stack.empty() || (r2.at(0) != '(')) fail();
}
}
}
for (string i : op_stack){
r2 = op_stack.back();
if ((r2.at(0) == '(') || (r2.at(0) == ')')) fail();
pop(r1, op_stack);
push(r1, rpn_stack);
}
for (string i : rpn_stack) rpn += i + " ";
return rpn;
}
double evaluate_postfix(const string &rpn){
double l1, l2;
vector <double> out_stack;
for (unsigned i = 0; i < rpn.length(); i++){
if (is_num(rpn.at(i))){
l1 = get_double(i, rpn);
push(l1, out_stack);
} else {
if (map1.find(rpn.at(i)) != map1.end()){
if (out_stack.size() < 1) fail();
pop(l1, out_stack);
calc1 op = map1.at(rpn.at(i));
l1 = op(l1);
push(l1, out_stack);
} else if (map2.find(rpn.at(i)) != map2.end()){
if (out_stack.size() < 2) fail();
pop(l1, out_stack);
pop(l2, out_stack);
calc2 op = map2.at(rpn.at(i));
l2 = op(l2, l1);
push(l2, out_stack);
}
}
}
if (out_stack.size() != 1) fail();
return out_stack.back();
}