?? test.cpp
字號:
#include<stdio.h>
#include<malloc.h>
#include<iostream.h>
#define MAX 200;
typedef struct
{
char *base;
char *top;
int stacksize;
}stack;
stack s;
typedef struct
{
char pLeft;//產生式的左部符號
char pRight[20];//產生式的右部最多包括20個字符
}Rule;
Rule rule[12];//定義文法最多可以包含12個產生式
int end_num=0;/*終結符的個數*/
int no_end_num=0;/*非終結符個數*/
int rule_num=0;/*規(guī)則數*/
char *p;/*存放非終結符*/
char *q;/*存放終結符*/
char **FristVT;/*表示no_end_num行end_num列矩陣*/
char **LastVT;/*表示no_end_num行end_num列矩陣*/
char *temptChar;
void create(stack &s)
{
s.base=s.top=(char*)malloc(sizeof(char));
s.stacksize=MAX;
}
void push(stack &s,char e)
{
*s.top++=e;
}
void push(stack &s,char p,char a)/*壓棧,每次壓兩個字符,其中p是非終結符,a是終結符*/
{
*s.top++=p;
*s.top++=a;
}
void pop(stack &s,char &e)
{
if(s.top==s.base)
{
return;
}
else
{
e=*--s.top;
}
}
void pop(stack &s,char &p,char &a)/*出棧,同樣每次也得出兩個字符*/
{
if(s.top==s.base)
{
return;
}
else
{
p=*--s.top;
a=*--s.top;
}
}
bool Empty(stack &s)
{
if(s.top<=s.base)
{
return true;
}
else
{
return false;
}
}
void Copy(char *p,char*q,int n)
{
for(int i=0;i<n;i++)
{
*p++=*q++;
}
}
int InitP()/*初始化P返回字符個數*/
{
int i=0;
char ch;
char *tempt=new char[100];
while((ch=getchar())!='\n')
{
tempt[i]=ch;
++i;
}
p=new char[i];
Copy(p,tempt,i);
delete tempt;
return i;
}
int InitQ()/*初始化Q返回字符個數*/
{
int i=0;
char ch;
char *tempt=new char[100];
while((ch=getchar())!='\n')
{
tempt[i]=ch;
++i;
}
q=new char[i];
Copy(q,tempt,i);
delete tempt;
return i;
}
void Initialize()/*初始化矩陣,非終結符數組,終結符數組*/
{
printf("請輸入非終結符:");
no_end_num=InitP();
printf("請輸入終結符:");
end_num=InitQ();
FristVT=(char**)malloc(sizeof(char*)*(no_end_num+1));
for(int i=0;i<no_end_num+1;i++)
{
FristVT[i]=(char*)malloc(sizeof(char)*(end_num+1));
}
LastVT=(char**)malloc(sizeof(char*)*(no_end_num+1));
for(int j=0;j<no_end_num+1;j++)
{
LastVT[j]=(char*)malloc(sizeof(char)*(end_num+1));
}
for(int a=1;a<no_end_num+1;a++)
{
for(int b=1;b<end_num+1;b++)
{
FristVT[a][b]='0';
LastVT[a][b]='0';
}
}
for(int e=1;e<no_end_num+1;e++)
{
FristVT[0][e]=p[e-1];
LastVT[0][e]=p[e-1];
}
for(int f=1;f<end_num+1;f++)
{
FristVT[f][0]=q[f-1];
LastVT[f][0]=q[f-1];
}
}
void InitRule()/*輸入文法*/
{
printf("請輸入文法個數 ");
int i=0;
cin>>i;
rule_num=i;
for(int j=0;j<i;j++)
{
printf("請輸入文法%d:",j);
int count=0;
char ch;
char *temp=new char[20];//定義最多20個字符
while((ch=getchar())!='\n')
{
temp[count]=ch;
count++;
}
rule[j].pLeft=temp[0];
for(int a=1;a<count;a++)
{
rule[j].pRight[a]=temp[a];
}
}
}
char GetF(char p,char a)/*獲得F[p,a]的值*/
{
int row=0;
int column=0;
for(int i=0;i<no_end_num+1;i++)
{
if(FristVT[0][i]==p)
{
column=i;
}
}
for(int j=0;j<end_num+1;j++)
{
if(FristVT[j][0]==a)
{
row=j;
}
}
return FristVT[row][column];
}
char GetL(char p,char a)/*獲得F[q,a]的值*/
{
int row=0;
int column=0;
for(int i=0;i<no_end_num+1;i++)
{
if(FristVT[0][i]==p)
{
column=i;
}
}
for(int j=0;j<end_num+1;j++)
{
if(FristVT[j][0]==a)
{
row=j;
}
}
return FristVT[row][column];
}
void SetF(char q,char a)
{
int row=0;
int column=0;
for(int i=0;i<no_end_num+1;i++)
{
if(LastVT[0][i]==q)
{
column=i;
}
}
for(int j=0;j<end_num+1;j++)
{
if(LastVT[j][0]==a)
{
row=j;
}
}
FristVT[row][column]='1';
}
void SetL(char q,char a)
{
int row=0;
int column=0;
for(int i=0;i<no_end_num+1;i++)
{
if(LastVT[0][i]==q)
{
column=i;
}
}
for(int j=0;j<end_num+1;j++)
{
if(LastVT[j][0]==a)
{
row=j;
}
}
LastVT[row][column]='1';
}
int LeachChar()/*濾去"->"字符串*/
{
return 1;
}
void GetFirstVT(char c)/*獲得FirstVT*/
{
}
void GetLastVT(char c)/*獲得LastVT*/
{
}
void InsertF(char p,char a)
{
}
void InsertL(char q,char a)
{
}
void main()
{
Initialize();
printf("%d",no_end_num);
printf("%d",end_num);
//InitRule();
//printf("%s",rule[0].pRight);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -