Repository navigation
Expand file tree
/
Copy pathExprEval.cpp
More file actions
305 lines (288 loc) · 5.63 KB
/
Copy pathExprEval.cpp
File metadata and controls
305 lines (288 loc) · 5.63 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define F false
#define T true
string expression;
vector<string> next_p;
//const double PI = 3.14159265358979323846;
//const double E = 2.71828182845904523536;
int op_doing(char op)//优先级
{
switch (op)
{
case '+':return 1;
case '-':return 1;
case '*':return 2;
case '/':return 2;
case '%':return 2;
case '^':return 3;
case '(':return 0;
case ')':return 0;
default:return -1;
}
}
bool op_is_left(char op)//是左结合运算符吗?
{
return op != '^';
}
bool is_other_op(const char& tep)
{
if (tep == 'e')
return T;
if (tep == 'p')
return T;
else
return F;
}
bool op_is_using(char op)
{
return op == '+' || op == '-' || op == '*' || op == '/' || op == '%' || op == '^' || op == '(' || op == ')';
}
double count(double a, double b, char op)//运算
{
switch (op)
{
case '+':return a + b;
case '-':return a - b;
case '/':
{
if (b == 0)
{
cout << "ERROR : " << a << " / 0!";
return 0;
}
else
return a / b;
}
case '*':return a * b;
case '%':
{
if (b == 0)
{
cout << "ERROR : " << a << " % 0!";
return 0;
}
else
return fmod(a, b);
}
case '^':return pow(a, b);
default:return -1;
}
}
bool is_minus(const string& expression, int len)//是减号还是负数?
{
if (expression[len] != '-')
return F;
if (len == 0)
return T;
char B_len = expression[len - 1];
if (B_len == '(')
return T;
if (B_len == '+' || B_len == '-' || B_len == '*' || B_len == '/' || B_len == '%' || B_len == '^')
return T;
return F;
}
string read_other_op(const string& expression, ll& i)//<- will using f()
{
string re = "";
if (expression[i] == 'e')//e
{
re = "2.71828182845904523536";
i++;
}
else if (expression[i] == 'p' && expression.size() > i + 1 && expression[i + 1] == 'i')//pi
{
re = "3.14159265358979323846";
i += 2;
}
//sin,cos,tan,lg,log...
//will using f()
i--;
return re;
}
string read_string(const string& expression, ll& i)//表达式处理
{
string num = "";
if (expression[i] == '-' && is_minus(expression, i))//负数
{
num += '-';
i++;
}
if (i < expression.size() && is_other_op(expression[i]))
{
//bool is_f = F;
string other = read_other_op(expression, i);//<-
//if()
if (num == "-")
return "-" + other;
else
return other;
}
while (i < expression.size() && isdigit(expression[i]))//整数
{
num += expression[i];
i++;
}
if (i < expression.size() && expression[i] == '.')//小数
{
num += '.';
i++;
while(i < expression.size() && isdigit(expression[i]))
{
num += expression[i];
i++;
}
}
i--;
return num;
}
vector<string> sol_string(const string& expression)
{
vector<string> p;
stack<char> ops;
for (ll i = 0; i < expression.size(); i++)
{
char tep = expression[i];
if (tep == ' ')
continue;
if (isdigit(tep) || (tep == '-' && is_minus(expression, i)) || is_other_op(tep))
{
string num = read_string(expression, i);
p.push_back(num);
}
else if (tep == '(')
ops.push(tep);
else if (tep == ')')
{
while (!ops.empty() && ops.top() != '(')
{
p.push_back(string(1, ops.top()));
ops.pop();
}
if (!ops.empty())
ops.pop();
}
else if (op_is_using(tep))
{
if (tep == '-' && is_minus(expression, i))
continue;
while (!ops.empty() && ops.top() != '(')
{
int top_p = op_doing(ops.top());
int next_p = op_doing(tep);
if (tep == '^' && ops.top() == '^')
break;
if (top_p > next_p || (top_p == next_p && op_is_left(ops.top())))
{
p.push_back(string(1, ops.top()));
ops.pop();
}
else
break;
}
ops.push(tep);
}
}
while (!ops.empty())
{
p.push_back(string(1, ops.top()));
ops.pop();
}
return p;
}
double count_p(const vector<string>& p)
{
stack<double> nums;
for (const string &t : p)
{
bool is_number = F;
if (t[0] == '-' && t.size() > 1)
{
is_number = T;
for (ll i = 1; i < t.size(); i++)
if (!isdigit(t[i]) && t[i] != '.')
{
is_number = F;
break;
}
}
else if (isdigit(t[0]))
is_number = T;
if (is_number)
nums.push(stod(t));
else if (t.size() == 1 && op_is_using(t[0]))
{
if (nums.size() < 2)
{
cout << "ERROR : CONT HAVE ENOUGHT NUM(S)" << endl;
return -1;
}
double a = nums.top();
nums.pop();
double b = nums.top();
nums.pop();
double result = count(b, a, t[0]);
nums.push(result);
}
}
if (nums.size() != 1)
{
cout << "ERROR : U EXPRESSION IS WORNG." << endl;
return -1;
}
return nums.top();
}
int reading_form_file()
{
ifstream fin("input.txt");
ofstream fout("output.txt");
if(!fin.is_open())
{
cerr << "ERROR: CAN'T OPEN \"input.txt\"!" << endl;
return -1;
}
if(!fout.is_open())
{
cerr << "ERROR: CAN'T WRITE TO \"output.txt\"!" << endl;
return -2;
}
while(getline(fin,expression))
{
if(expression.empty())
continue;
next_p = sol_string(expression);
double result = count_p(next_p);
fout << expression << "=" << result << endl;
cout << expression << "=" << result << endl;
}
fin.close();
fout.close();
cout << "Calculation completed, results have been written to output.txt.";
return 0;
}
int reading_form_cin()
{
cout << "ENTER U EXPRESSION:" << endl;
getline(cin, expression);
next_p = sol_string(expression);
double result = count_p(next_p);
cout << expression << " = " << result;
return 0;
}
int main()
{
int chose;
cout <<"U HAVE TWO CHOSE:"<<endl;
cout <<"CIN 1:USE FILE"<<endl;
cout <<"CIN 2:USE CIN"<<endl;
cin >> chose;
cin.ignore();
if(chose == 1)
reading_form_file();
else if(chose == 2)
reading_form_cin();
else
cout <<"ERROR: U ENTER AN ERROR OP ABOUT CIN OR FILE!";
return 0;
}