📄 duomem.cpp
字号:
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define max 10
struct princple
{
char left;
char right[4];
};
typedef struct
{
char element[max];
int top;
}stack;
stack *s;
int yxjz[8][8]={0,0,0,0,0,0,0,3,
0,0,2,1,0,1,0,3,
0,2,0,0,0,2,0,3,
0,0,1,1,2,1,0,3,
0,3,0,0,0,3,0,3,
0,3,0,0,0,3,2,3,
0,3,0,0,0,3,0,3,
1,1,1,1,1,1,1,3};
struct princple p[4]={{'S',"bAb"},{'A',"(B"},{'A',"a"},{'B',"Aa)"}};
char inputs[10],tem[5],string[10];
int i=0,j1=0,j=0;
void main()
{
void ctable();
void suanfa();
ctable();
cout<<"enter the stream(以$号结束):"<<endl;
cout<<"->";
cin>>inputs;
suanfa();
}
void ctable()
{
void suanfa();
int i,j;
cout<<"模拟优先矩阵:"<<endl;
cout<<" 0-空关系 1-小于关系"<<endl;
cout<<" 2-等于关系 3-大于关系 "<<endl;
cout<<endl;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%d ",yxjz[i][j]);
cout<<endl;
}
}
void suanfa()
{
void push(char x);
int find1(char x);
int find2(char x);
void error();
int guiyue(char ch[]);
j=0;
s=(stack *)malloc(sizeof(stack));
s->top=-1;/*置空栈*/
push('$');/*把$符与开始符入栈*/
cout<<"栈\t"<<"输入\t"<<"句柄"<<"\t输出\t"<<endl;
cout<<"$\t"<<inputs;
do
{
i=s->top;
int a=find1(s->element[i]);
int b=find2(inputs[j]);
if(yxjz[a][b]=='0')
error();
if(yxjz[a][b]=='1'||yxjz[a][b]=='2')
{
push(inputs[j]);
i++;
j++;
}
if(yxjz[a][b]=='3')
{
int k;
j1=i;
while(yxjz[s->element[j1]][i]=='2')
j1--;
for(k=j1;k<=i;k++)
string[k]=s->element[k];
int hui=guiyue(string);
s->top=j1;
i=s->top;
push(p[hui].left);
for(k=0;k<=s->top;k++)
cout<<s->element[k];
cout<<"\t";
for(k=j;k<10;k++)
cout<<inputs[k];
cout<<"\t";
cout<<string<<"\t";
cout<<p[hui].left<<"->"<<p[hui].right<<endl;
}
}while(!(s->element[s->top]=='E'&&s->element[s->top-1]=='$'&&inputs[j]=='$'));
}
void push(char x)
{
s->top++;
s->element[s->top]=x;
}
int find1(char x)
{
if(x=='S')
return 0;
else if(x=='b')
return 1;
else if(x=='A')
return 2;
else if(x=='(')
return 3;
else if(x=='B')
return 4;
else if(x=='a')
return 5;
else if(x==')')
return 6;
else if(x=='$')
return 7;
else
return NULL;
}
int find2(char x)
{
if(x=='S')
return 0;
else if(x=='b')
return 1;
else if(x=='A')
return 2;
else if(x=='(')
return 3;
else if(x=='B')
return 4;
else if(x=='a')
return 5;
else if(x==')')
return 6;
else if(x=='$')
return 7;
else
return NULL;
}
void error()
{
cout<<"error!"<<endl;
exit(0);
}
int guiyue(char ch[])
{
if(strcmp(ch,"bAb")==0)
return 0;
else if(strcmp(ch,"(B")==0)
return 1;
else if(strcmp(ch,"a")==0)
return 2;
else if(strcmp(ch,"Aa)")==0)
return 3;
else
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -