-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.cpp
403 lines (352 loc) · 8.55 KB
/
Calculator.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include"Calculator.h"
bool CisNumber(const string& str) {
istringstream sin(str);
double test;
return sin >> test && sin.eof();
}
bool judgecin(const string& kksk) {
//全区域判断(特殊字符)
for (int i = 0; i < kksk.size(); i++) {
if (!(kksk[i] == '+' || kksk[i] == '-' || kksk[i] == '*' || kksk[i] == '/' || kksk[i] == '.' || \
kksk[i] == ' ' || kksk[i] == '(' || kksk[i] == ')' || isdigit(kksk[i]))) {
cout << "含有特殊字符!!" << endl;
return false;
}
}
//括号判断
int lefpa = 0, rigpa = 0;
int p = 0;
while (p < kksk.length()) {
if (kksk[p] == '(') lefpa++;
if (kksk[p] == ')')rigpa++;
p++;
}
p = 0;
if (lefpa != rigpa) {
cout << "括号数量错误!!" << endl;
return false;
}
//小数点前后数字判断
if (kksk[0] == '.') {
cout << "小数点使用错误!" << endl;
return false;
}
if (kksk[kksk.length() - 1] == '.') {
cout << "小数点使用错误!" << endl;
return false;
}
//两位小数输入错误判断
for (int k = 0; k <= kksk.length() - 1; k++) {
if (kksk[k] == '.' && k + 2 <= kksk.length() - 1) {
if (isdigit(kksk[k + 1]) && isdigit(kksk[k + 2])) {
cout << "只能为一位小数!" << endl;
return false;
}
if (isdigit(kksk[k + 1]) && kksk[k + 2] == '.') {
cout << "小数点误用!" << endl;
return false;
}
}
}
for (int k = 0; k <= kksk.length() - 1; k++) {
//防止下标溢出
if (k + 1 <= kksk.length() - 2) {
if (kksk[k + 1] == '.') {
if (!isdigit(kksk[k])) {
cout << "小数点使用错误!" << endl;
return false;
}
if (!isdigit(kksk[k + 2])) {
cout << "小数点使用错误!" << endl;
return false;
}
}
}
}
////使用字符串流判断操作符与操作数数量是否相等
//stringstream jud_bySpace(kksk);
//vector<string> jbS;
//string s;
//while (getline(jud_bySpace,s,' ')) {
// jbS.push_back(s);
//}
//int numNumb = 0, opeRand = 0;
//for (int i = 0; i < jbS.size(); i++) {
// if (isNumber(jbS[i])) numNumb++;
// if (jbS[i] == "+");
//}
//运算符使用判断
if (kksk[0] == '+' || kksk[0] == '*' || kksk[0] == '/') {
cout << "操作符使用错误!首项不能为除减号外的操作符!" << endl;
return false;
}
if (kksk[kksk.length() - 1] == '+' || kksk[kksk.length() - 1] == '*' || kksk[kksk.length() - 1] == '/' || kksk[kksk.length() - 1] == '-') {
cout << "操作符使用错误!尾项不能为操作符!" << endl;
return false;
}
for (int s = 0; s <= kksk.length() - 1; s++) {
//当前s为数字
if (isdigit(kksk[s])) {
bool spaceFlag = false;
for (int i = s + 1; i <= kksk.length() - 1; i++) {
if (kksk[i] == '(') {
cout << "数字不能和括号贴合!" << endl;
return false;
}
else if (isdigit(kksk[i]) && spaceFlag) {
cout << "数字不能和数字隔着空格贴合!" << endl;
return false;
}
else {
if (kksk[i] == ' ') {
spaceFlag = true;
continue;
}
else if (kksk[i] == '+' || kksk[i] == '-' || kksk[i] == '*' || kksk[i] == '/' || kksk[i] == ')')
break;
}
}
}
//当前s为(
else if (kksk[s] == '(') {
bool spaceFlag = false;
for (int i = s + 1; i <= kksk.length() - 1; i++) {
if (kksk[i] == '+' || kksk[i] == '*' || kksk[i] == '/') {
cout << "左括号不能直接跟操作符!" << endl;
return false;
}
else if (kksk[i] == ')') {
cout << "左括号不能直接跟右括号!" << endl;
return false;
}
else {
if (kksk[i] == ' ') {
spaceFlag = true;
continue;
}
else if (kksk[i] == '-' || isdigit(kksk[i]) || kksk[s] == '(')
break;
}
}
}
//当前s为)
else if (kksk[s] == ')') {
bool spaceFlag = false;
for (int i = s + 1; i <= kksk.length() - 1; i++) {
if (isdigit(kksk[i])) {
cout << "空格使用错误!右括号不能直接跟数字!" << endl;
return false;
}
else {
if (kksk[i] == ' ') {
spaceFlag = true;
continue;
}
else if (kksk[i] == '-' || kksk[i] == '+' || kksk[i] == '*' || kksk[i] == '/' || kksk[i] == ')')
break;
}
}
}
//当前s为 . || ' '
else if (kksk[s] == '.' || kksk[s] == ' ') {
continue;
}
//当前s为操作符
else if (kksk[s] == '+' || kksk[s] == '-' || kksk[s] == '*' || kksk[s] == '/') {
bool spaceFlag = false;
for (int i = s + 1; i <= kksk.length() - 1; i++) {
if (kksk[i] == '+' || kksk[i] == '*' || kksk[i] == '/' || kksk[i] == '-') {
cout << "操作符不能直接跟操作符!" << endl;
return false;
}
else if (kksk[i] == ')') {
cout << "操作符不能直接跟右括号!" << endl;
return false;
}
else {
if (kksk[i] == ' ') {
spaceFlag = true;
continue;
}
else if (isdigit(kksk[i]) || kksk[i] == '(')
break;
}
}
}
}
return true;
}
void transform(vector<string>& stk, string str) {
str += " ";
stack<char> MMstk;
//用以读取整个数字的开始下标
int flag = 0;
string temp;
int i = 0;
while (i < str.length()) {
//为空格
if (str[i] == ' ') {
if (i != flag) {
temp = str.substr(flag, i - flag);
stk.push_back(temp);
i++;
flag = i;
continue;
}
i++;
flag = i;
continue;
}
//为左括号,入栈
else if (str[i] == '(') {
MMstk.push(str[i]);
}
//如果ch是操作符
else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
//减号前一位为左括号
if (i - 1 >= 0 && (str[i - 1] == '(') && str[i] == '-') {
stk.push_back("0");
}
if (i != flag) {
temp = str.substr(flag, i - flag);
stk.push_back(temp);
flag = i;
}
while (true) {
// (a) 栈为空或栈顶元素为左圆括号
if (MMstk.empty() || MMstk.top() == '(') {
MMstk.push(str[i]);
break;
}
// 按优先级判断入栈
else if (MMstk.top() == '+' || MMstk.top() == '-' || MMstk.top() == '*' || MMstk.top() == '/') {
//(b) 如果ch优先级比栈顶元素高,那么把ch入栈
//(c) ch优先级低于或者等于栈顶元素,把栈顶元素出栈并输出之,转(a)
if (MMstk.top() == '*' || MMstk.top() == '/') {
char theTop = MMstk.top();
MMstk.pop();
string s(1, theTop);
stk.push_back(s);
continue;
}
else if (MMstk.top() == '+' || MMstk.top() == '-') {
if (str[i] == '-' || str[i] == '+') {
char theTop = MMstk.top();
MMstk.pop();
string s(1, theTop);
stk.push_back(s);
continue;
}
else if (str[i] == '*' || str[i] == '/' || str[i] == '+') {
MMstk.push(str[i]);
break;
}
}
}
else
break;
}
}
//为右圆括号
else if (str[i] == ')') {
if (i != flag) {
temp = str.substr(flag, i - flag);
stk.push_back(temp);
flag = i;
}
while (!MMstk.empty())
{
if (MMstk.top() == '(') {
//'('出栈
MMstk.pop();
break;
}
char theTop = MMstk.top();
MMstk.pop();
string s(1, theTop);
stk.push_back(s);
}
if (i != flag) {
temp = str.substr(flag, i - flag);
stk.push_back(temp);
i++;
flag = i;
continue;
}
}
//将浮点数存好(遇到数字和.)
else {
i++;
continue;
}
i++;
flag = i;
}
while (!MMstk.empty())
{
char theTop = MMstk.top();
MMstk.pop();
string s(1, theTop);
stk.push_back(s);
}
}
double calcalcal(vector<string> vcstr) {
vector<string> cpyvc = vcstr;
//储存操作数
stack<double> ssk;
int i = 0;
while (i != cpyvc.size()) {
if (cpyvc[i] == "+" || cpyvc[i] == "-" || cpyvc[i] == "*" || cpyvc[i] == "/") {
//是操作符, 从栈里取出两个数进行运算,结果再进行入栈
double n1 = ssk.top();
ssk.pop();
//双目操作
if (!ssk.empty()) {
double n2 = ssk.top();
ssk.pop();
if (cpyvc[i] == "+") {
double rel = n1 + n2;
ssk.push(rel);
}
else if (cpyvc[i] == "-") {
double rel = n2 - n1;
ssk.push(rel);
}
else if (cpyvc[i] == "*") {
double rel = n1 * n2;
ssk.push(rel);
}
else if (cpyvc[i] == "/") {
double rel = n2 / n1;
ssk.push(rel);
}
}
//单目操作
else {
double n2 = 0.0;
if (cpyvc[i] == "-") {
double rel = n2 - n1;
ssk.push(rel);
}
else
cout << "wrong, its not '-' ";
}
}
//是数字的话就入栈(浮点数)
else {
ssk.push(stod(cpyvc[i]));
}
i++;
}
return ssk.top();
}
double useCal(string str) {
double rel = 0.0;
vector<string> stk;
if (judgecin(str)) {
transform(stk, str);
rel = calcalcal(stk);
return rel;
}
return 0.0;
}