编译原理实验三(逆波兰式的产生及计算)

Posted by nop on 2020-04-21
Words 1.2k In Total

说明

实验代码采用多文件的形式,即包含三部分:

  1. Rp.h : 结构体以及类(成员函数等)的声明
  2. Rp.cpp : 类的成员函数的实现
  3. main.cpp : 主调函数, 完成用户输入的过滤以及类的的实例化

实验流程图

Alt

实验代码

  • Rp.h
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
// Rp.h
#ifndef RP_H
#define RP_H

#define LEN 20 // 定义输入字符长度

struct fake_stack
{
char value; // 存储字符
fake_stack *fd; // 前驱指针
fake_stack *bk; // 后驱指针
};

struct values
{
int v; // 记录运算量的值
char v_c; // 记录运算量符号
values *next;
};

class Rp
{
public:
Rp(char input[]); // 构造函数,接收输入的表达式
void Rpn(); // 生成逆波兰式
void Calculate(); // 计算逆波兰式
int compare(char arg1,char arg2); // 判断优先级
// arg1>arg2;return 1 arg1<=arg2;return 0
int compare1(char ch); // compare 子函数
void show(char ch, char *in, char *out); // 输出分析过程
int getNum(char ch, int value); // 获取对应运算量的值
bool isNum(char p); // 判断是否属于运算量
void stack_pop(char *ch); // 模拟弹栈操作
void stack_push(char ch); // 模拟压栈操作
private:
fake_stack *bp,*sp; // 栈指针
char input[LEN],output[LEN];
values *head; // 记录运算量的单链表头指针
};

#endif
  • Rp.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
// Rp.cpp
#include "Rp.h"
#include <iostream>
#include <cstring>
using namespace std;

void Rp::stack_pop(char *ch)
{
// 模拟弹栈操作
fake_stack *p;
p = sp;
*ch = p->value;
sp = sp->fd;
sp->bk = NULL;
p->fd = NULL;
delete p;
}

void Rp::stack_push(char ch)
{
// 模拟压栈操作
fake_stack *q;
q = new fake_stack;
q->value = ch;
q->bk = NULL;
q->fd = sp;
sp->bk = q;
sp = q;
}

bool Rp::isNum(char ch)
{
// 约定输入的表达式为小写字母和运算符组成
if(ch < 'z' && ch >= 'a')
return true;
return false;
}

int Rp::getNum(char ch, int value)
{
if(ch == 'T')
return value;
values *p;
p = head;
while(p!=NULL){
if(p->v_c == ch)
return p->v;
p = p->next;
}
cout<<"get num failed!"<<endl;
return 0;
}
void Rp::show(char ch, char *in, char *out)
{
fake_stack *p;
string stack;
p = bp;
do{
stack += p->value;
p = p->bk;
}while(p != NULL);
cout<<ch;
cout.width(20);
cout<<in;
cout.width(20);
cout<<stack;
cout.width(20);
cout<<output;
cout<<endl;
}

int Rp::compare1(char ch)
{
switch (ch)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default:
return -1; // 不合法运算符的情况
}
}
int Rp::compare(char arg1, char arg2)
{
int t1,t2; // 记录运算符优先级
if(t1 = compare1(arg1))
if(t2 = compare1(arg2))
if(t1<=t2)
return 0;
return 1;
}

void Rp::Calculate()
{
char *p = output;
char value1,value2; // 临时存储要计算的变量
char res = 'T'; // 作为存储临时计算结果的标志
int c_res; // 存储临时计算结果
while(*p != '#'){
if(isNum(*p)){
stack_push(*p);
p++;
}else{
stack_pop(&value2);
stack_pop(&value1);
switch (*p)
{
case '+':{
stack_push(res);
c_res = getNum(value1, c_res) + getNum(value2, c_res);
p++;
break;
}
case '-':{
stack_push(res);
c_res = getNum(value1, c_res) - getNum(value2, c_res);
p++;
break;
}
case '*':{
stack_push(res);
c_res = getNum(value1, c_res) * getNum(value2, c_res);
p++;
break;
}
case '/':{
stack_push(res);
c_res = getNum(value1, c_res) / getNum(value2, c_res);
p++;
break;
}
default:{
cout<<"Erro!";
break;
}
}
}
}
cout<<"\nCalculate result: "<<c_res<<endl;
}

void Rp::Rpn()
{
char *in(input),*out(output); // 输入、输出缓冲区指针
cout<<"current\t\t"<<"input\t\t"<<"sym stack\t\t"<<"output"<<endl;
char ch;
ch = *in; // 获取当前字符
in++; // 指向下一个输入字符
while (ch != '#'){
if(isNum(ch)){// 是运算量的情况
*out = ch;
out++;
ch = *in;
in++;
show(ch,in,output);
}else if(ch != '(' && ch!= ')'){// 运算符
if(sp->value == '#'){// 符号栈为空
stack_push(ch);
ch = *in;
in++;
show(ch,in,output);
}else{// 符号栈不为空
L:
int t = compare(ch,sp->value);
switch (t)
{
case 1:{
stack_push(ch);
ch = *in;
in++;
show(ch,in,output);
break;
}
default:{
stack_pop(out);
show(ch,in,output);
out++;
goto L;
}
}
}
}else if(ch == '('){
stack_push(ch);
show(ch,in,output);
ch = *in;
in++;
}else if(ch == ')'){
L1:
if(sp->value == '('){
char trash;
stack_pop(&trash);
show(ch,in,output);
ch = *in;
in++;
}else if(sp->value == '#')
cout<<"Erro!"<<endl;
else{
stack_pop(out++);
show(ch,in,output);
goto L1;
}
}else{
while(sp->value == '#'){
if(sp->value != '('){
stack_pop(out++);
show(ch,in,output);
}else
cout<<"Erro!"<<endl;
}
}
}
if(sp->value != '#'){
stack_pop(out++);
show(ch,in,output);
}
*out = '#'; // 为output添加结束标志
while(sp!=bp){
char temp;
stack_pop(&temp);
}// 清空栈
}

Rp::Rp(char inputs[])
{
strcpy(input,inputs); // 拷贝输入到模拟缓冲区
// 初始化栈空间
bp = new fake_stack;
bp->value = '#';
sp = bp;
bp->fd = NULL;
bp->bk = NULL;
head = new values;
values *p;
p = head;
char temp;
cout<<"\nPlease input sym and value(press E to end input): "<<endl;
start:
cout<<"\nsym: ";
cin>> temp;
if(temp == 'E')
goto run;
p->v_c = temp;
cout<<"value: ";
cin>> p->v;
p->next = new values;
p = p->next;
goto start;
run:
fflush(stdin);
fflush(stdout);
Rpn(); // 产生逆波兰式
Calculate(); // 计算表达式结果
}
  • main.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
// main.cpp
#include "Rp.h"
#include <iostream>
using namespace std;

int main()
{
char in[LEN];
s:
cout<<"Enter a string ending in #: ";
cin>>in;
char *q=in;
while (*q!='#')
{
if( !((*q >='a' && *q <='z')||\
(*q == '+' ||\
*q == '-' ||\
*q == '*' ||\
*q == '/' ||\
*q == '(' ||\
*q == ')'))){
cout<<"wrong input!"<<endl;
goto s;
}
q++;
}
Rp p(in);
}

You are welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them.