-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountRPN.h
More file actions
449 lines (404 loc) · 6.5 KB
/
CountRPN.h
File metadata and controls
449 lines (404 loc) · 6.5 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#define _CRT_SECURE_NO_DEPRECATE
#pragma once
#include<iostream>
#include<assert.h>
#include<stack>
#include<queue>
using namespace std;
//栈的应用,逆波兰表达式(RPN)的求解;
//enum Type //枚举
//{
// OP_SYMNOL, //操作符
// OP_NUM, //操作数
// ADD, //加
// SUB, //减
// MUL, //乘
// DIV, //除
//};
//
//struct Cell
//{
// Type _type; //所取数的类型
// int _value; //所取值
//};
//int CountRPN(Cell rpn[],size_t n)
//{
// stack<int> s; //数的压栈
//
// for (size_t i = 0; i < n; i++)
// {
// if (rpn[i]._type == OP_NUM) //先判断是否为操作数,是压栈保存;
// {
// s.push(rpn[i]._value);
// }
// else if (rpn[i]._type == OP_SYMNOL)
// {
// int right = s.top(); //注意第一个出来的数是运算符右侧的值;
// s.pop();
// int left = s.top(); // 第二个出来的是运算符左侧的运算数;
// s.pop();
//
// //求解
// switch (rpn[i]._value)
// {
// case ADD:
// s.push(left + right);
// break;
// case SUB:
// s.push(left - right);
// break;
//
// case MUL:
// s.push(left * right);
// break;
//
// case DIV:
// s.push(left / right);
// break;
//
// default:
// cout << "无效值" << endl;
// }
//
//
// }
// }
//
// return s.top();
//}
//中缀 表达式转后缀 表达式
/*
左括号“(”的栈外优先级最高,他一来到立即进栈,,但当他进栈后栈内优先级数变得最低,便于括号内其他操作符进栈,其他操作符进栈后优先级数都升1;这样
可以体现在中缀表达式中相同优先级的操作符自左向右计算的要求;
当前操作符优先级数大于等于栈顶操作符优先级数,刚扫面的进栈
小于的时候,栈顶操作符出栈;一直与栈顶比较,知道遇到大于栈顶的操作符,将当前操作符压栈;
*/
int Isp(char ch) //获取操作符进栈后优先数
{
switch (ch)
{
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 3;
case '*':
case '/':
return 5;
case ')':
return 8;
default:
{
cout << "没有找到此操作符" << endl;
return -1;
}
}
}
int Icp(char ch) //获取当前扫描到操作符的优先级数
{
switch (ch)
{
case '#':
return 0;
case '(':
return 8;
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case ')':
return 1;
default:
{
cout << "没有找到此操作符" << endl;
return -1;
}
}
}
//void TestCountRPN() //测试CountRPN(Cell rpn[],size_t n);
//{
// Cell array[] =
// {
// { OP_NUM, 12 },
// { OP_NUM, 3 },
// { OP_NUM, 4 },
// { OP_SYMNOL, ADD },
// { OP_SYMNOL, MUL },
// { OP_NUM, 6 },
// { OP_SYMNOL, SUB },
// { OP_NUM, 8 },
// { OP_NUM, 2 },
// { OP_SYMNOL, DIV },
// { OP_SYMNOL, ADD },
// };
// size_t len = sizeof(array) / sizeof(array[0]);
// cout << CountRPN (array,len)<< endl;
//}
//void postfix() //中缀 表达式转后缀 输出到标准输出;
//{
// stack<char> s;
// char ch, y;
// s.push('#');
// cin.get(ch);
// while (ch != '\n')
// {
// if (isdigit(ch))
// cout << ch;
// else if (ch == ')')
// {
//
// for (y = s.top(); y != '('; y = s.top())
// {
// s.pop();
// cout << y;
//
// }
// s.pop(); //pop出左括号;
//
// /*for (y = s.top(); Isp(y) > Icp(ch); y = s.top())
// {
// s.pop();
// cout << y;
// }*/
//
// }
// else
// {
// y = s.top();
// if (Isp(y) <= Icp(ch))
// {
// s.push(ch);
// }
// else
// {
// for (; Isp(y) > Icp(ch);y = s.top())
// {
// cout << y;
// s.pop();
// }
//
// s.push(ch);
//
// }
// }
// cin.get(ch);
// }
//
// while (!s.empty())
// {
// y = s.top();
// cout << y;
// s.pop();
//
// }
//
// cout << endl;
//}
//
////计算从标准输入获得的后缀表达式;
//void DoOperator(stack<int> s, char ch)
//{
// int right = s.top();
// s.pop();
//
// int left = s.top();
// s.pop();
//
// switch (ch)
// {
// case '+':
// s.push(left + right);
// break;
// case '-':
// s.push(left - right);
// break;
// case '*':
// s.push(left * right);
// break;
// case '/':
// s.push(left / right);
// break;
// default:
// break;
//
// }
//
//}
//
//int CountRPN()
//{
// char ch;
// int num;
// stack<int> s;
//
// for (cin.get(ch); ch != '#'&& ch != '\n'; cin.get(ch))
// {
// switch (ch)
// {
// case '+':
// case '-':
// case '*':
// case '/':
// DoOperator(s,ch);
// break;
//
// default: //操作数
// cin.putback(ch);
// cin >> num;
// s.push(num);
//
// }
// }
//
// return s.top();
//}
//
//void TestCountRPN() //测试:计算从标准输入获得的后缀表达式;
//{
// postfix();
// //cout << "表达式值为:" << CountRPN() << endl;
//}
//从标准输入得到表达式,转后缀并存入 队列
//void Testq(queue<int> &q) //测试队列中的值是否是真确的后缀表达式(打印队列)
//{
// while (!q.empty())
// {
// int ch = q.front();
// switch (ch)
// {
// case '+':
// cout << '+';
// q.pop();
// break;
// case '-':
// cout << '-';
// q.pop();
// break;
// case '*':
// cout << '*';
// q.pop();
// break;
// case '/':
// cout << '/';
// q.pop();
// break;
// case '#':
// cout << '#';
// q.pop();
// break;
// default:
// cout << q.front() << " ";
// q.pop();
// break;
//
// }
// }
//}
//计算队列中的后缀表达式;
void PostFix(queue<int> &q) //中缀 表达式转后缀 输出到队列;
{
stack<char> s;
char ch, y;
int num;
s.push('#');
cin.get(ch);
while (ch != '\n')
{
if (isdigit(ch))
{
cin.putback(ch); //还给标准输入
cin >> num; //取出数字
q.push(num);
}
else if (ch == ')')
{
for (y = s.top(); y != '('; y = s.top()) //若为右括号直接将栈中内容弹出知道遇到左括号为止;
{
s.pop();
q.push(y);
}
s.pop(); //pop出左括号;
}
else
{
y = s.top();
if (Isp(y) <= Icp(ch)) //栈内优先级小于当前优先级,入栈
{
s.push(ch);
}
else
{
for (; Isp(y) > Icp(ch);y = s.top()) //栈内大于当前出栈,近队列
{
q.push(y);
s.pop();
}
s.push(ch); //遇到栈内优先级小于当前ch优先级,将ch入栈;
}
}
cin.get(ch);
}
while (!s.empty()) //将栈中内容出栈依次放到队列;
{
y = s.top();
q.push(y);
s.pop();
}
}
void DoOperator(stack<int> &s, char ch) //
{
int right = s.top();
s.pop();
int left = s.top();
s.pop();
switch (ch)
{
case '+':
s.push(left + right);
break;
case '-':
s.push(left - right);
break;
case '*':
s.push(left * right);
break;
case '/':
s.push(left / right);
break;
default:
break;
}
}
int CountRPN(queue<int> &q) //计算队列中存储的后缀表达式
{
stack<int> s; //保存操作数,计算结果;
int ch = q.front();
while (!q.empty() && ch != '#')
{
switch (ch)
{
case '+':
case '-':
case '*':
case '/':
DoOperator(s, ch); //做运算
break;
default:
s.push(ch);
break;
}
q.pop();
ch = q.front();
}
return s.top();
}
void TestCountRPN()
{
queue<int> q;
PostFix(q);
cout << CountRPN(q) << endl;
}