-
Notifications
You must be signed in to change notification settings - Fork 248
/
ExpressionCalc.cpp
169 lines (147 loc) · 3.29 KB
/
ExpressionCalc.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
/*****************************************************************
Calculation of the value of the expression.
Uses recursive descent parser. Supports doubles.
Available operations:
+ - / * Usual meaning
- Unary minus
# Root (3 # 8 = 2)
^ Power (2 ^ 3 = 8)
# Square root (unary)
TODO: find a good problem on it
*****************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <utility>
#include <iomanip>
using namespace std;
string s;
int len;
double ans;
int p;
double exp();
double term();
double interm();
double factor();
double pr1Sign(char c) {
return (c == '-' || c == '+');
}
double pr2Sign(char c) {
return (c == '*' || c == '/');
}
double pr3Sign(char c) {
return (c == '#' || c == '^');
}
double factor() {
double res = 0.0, dpow = 1.0;
bool dot = false;
if (p >= len) {
return res;
}
if (s[p] == '(') {
p++;
res = exp();
p++;
return res;
}
if (s[p] == '-') {
p++;
res = interm();
return -res;
}
if (s[p] == '#') {
p++;
res = interm();
return pow(res, 0.5);
}
while (p < len && ((s[p] >= '0' && s[p] <= '9') || s[p] == '.')) {
if (s[p] == '.')
dot = true;
else {
res = res * 10 + s[p] - '0';
if (dot)
dpow *= 10.0;
}
p++;
}
return res / dpow;
}
double interm() {
double res, t;
char c;
res = factor();
while (p < len && pr3Sign(s[p])) {
c = s[p];
p++;
t = factor();
if (c == '^') {
if (t < 1.0 && t > -1.0 && t != 0 && res < 0)
res = -pow(-res, t);
else
res = pow(res, t);
}
if (c == '#') {
if (t < 0)
res = -pow(-t, (1.0 / res));
else
res = pow(t, (1.0 / res));
}
}
return res;
}
double term() {
double res, t;
char c;
res = interm();
while (p < len && pr2Sign(s[p])) {
c = s[p];
p++;
t = interm();
if (c == '*')
res *= t;
if (c == '/')
res /= t;
}
return res;
}
double exp() {
double res, t;
char c;
res = term();
while (p < len && pr1Sign(s[p])) {
c = s[p];
p++;
t = term();
if (c == '-')
res -= t;
if (c == '+')
res += t;
}
return res;
}
void remove_whitespaces(string &s) {
string temp = s;
s = "";
for (int i = 0; i < (int) temp.length(); i++)
if (temp[i] != ' ')
s.append(1, temp[i]);
}
int main() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
getline(cin,s);
remove_whitespaces(s);
len = s.length();
ans = exp();
printf("%lg",ans);
return 0;
}