📄 minipentium.cpp
字号:
#include<iostream.h>
enum Type{NUM,MOV,ADD,SUB,MUL,EMPTY};
class exp
{
public:
Type kind;
int number;
exp * left;
exp * right;
exp();
};
exp :: exp()
{
kind = EMPTY;
number = 0;
left = NULL;
right = NULL;
}
void initExp(int a,exp * x)
{
exp * temp = x;
temp->kind = NUM;
temp->number = a;
temp->left = NULL;
temp->right = NULL;
}
exp MOVL(exp a,exp b)
{
exp c;
c.kind = MOV;
c.left = new exp();
*(c.left) = a;
c.right = new exp();
*(c.right) = b;
return c;
}
exp ADDL(exp a,exp b)
{
exp c;
if((a.kind == EMPTY)||(b.kind == EMPTY))
{
cout<<"ERROR!"<<endl;
return c;
}
c.kind = ADD;
c.left = new exp();
*(c.left) = a;
c.right = new exp();
*(c.right) = b;
return c;
}
exp SUBL(exp a,exp b)
{
exp c;
if((a.kind == EMPTY)||(b.kind == EMPTY))
{
cout<<"ERROR!"<<endl;
return c;
}
c.kind = SUB;
c.left = new exp();
*(c.left) = a;
c.right = new exp();
*(c.right) = b;
return c;
}
exp MULL(exp a,exp b)
{
exp c;
if((a.kind == EMPTY)||(b.kind == EMPTY))
{
cout<<"ERROR!"<<endl;
return c;
}
c.kind = MUL;
c.left = new exp();
*(c.left) = a;
c.right = new exp();
*(c.right) = b;
return c;
}
void print(exp * a)
{
if(a->kind == EMPTY)
{
return ;
}
if((a->left == NULL)&&(a->right == NULL))
{
cout<<"NUM : "<<a->number<<endl;
return;
}
if(a->left != NULL)
print(a->left);
if(a->kind == MOV)
cout<<"MOV"<<endl;
else if(a->kind == ADD)
cout<<"ADD"<<endl;
else if(a->kind == SUB)
cout<<"SUB"<<endl;
else if(a->kind == MUL)
cout<<"MUL"<<endl;
if(a->right != NULL)
print(a->right);
}
void getNum(exp * a)
{
if(a->kind == NUM)
{
cout<<"NUM : "<<a->number<<endl;
return ;
}
if(a->left != NULL)
getNum(a->left);
if(a->right != NULL)
getNum(a->right);
}
int main()
{
//令 x = 1 , y = 2 ,构造 x + y
exp x0;
exp y0;
initExp(1,&x0);
initExp(2,&y0);
exp x;
exp y;
exp z;
x = MOVL(x0,x);
y = MOVL(y0,y);
y = ADDL(x,y);
cout<<"AST中的各节点为:"<<endl;
print(&y);
cout<<endl;
cout<<"算式中的数字有:"<<endl;
getNum(&y);
cout<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -