一、实验目的
编写程序实现自动机对无符号常数的识别程序
二、实验重难点
自动机的程序实现及识别
三、实验内容与要求
1、FA的C++程序表示;
2、FA的识别。
四、实验学时
4课时
五、实验设备与环境
Visual C++ 6.0
示例代码
#include<iostream>
#include<stdio.h>
using namespace std;
class FA{
private:
int arr1[21][5];
char arr2[5];
public :
FA(char a,char b, char c, char d)
{
for(int j=0;j<5;j++)
for(int i=0;i<21;i++)
arr1[j][i]=-1;
arr2[1]=a;
arr2[2]=b;
arr2[3]=c;
arr2[4]=d;
}
void getdata(int state,char word,int next)
{
int x;
int i=4;
arr2[0]=word;
while(arr2[i]!=word)
i--;
x=i;
arr1[state][x]=next;
};
void judge(char *p,int &y)
{
int x;
char *s=p;
if(s=='\0')
cout<<"input is error1"<<endl;
else while(*s!='\0')
{
cout<<*s<<endl;
int i=4;
arr2[0]=*s;
while(arr2[i]!=*s)
i--;
x=i;
if(arr1[y][x]==-1)
{
cout <<"input is error2"<<endl;
break;
}
else
y=arr1[y][x];
s++;
}
}
};
int main(){
char p[20],*s;
int c=1;
FA A('d','.','E','F');
A.getdata(1,'E',5);
A.getdata(1,'d',2);
A.getdata(1,'.',3);
A.getdata(2,'E',5);
A.getdata(2,'d',2);
A.getdata(2,'.',3);
A.getdata(3,'d',4);
A.getdata(4,'d',4);
A.getdata(4,'E',5);
A.getdata(5,'F',6);
A.getdata(5,'d',7);
A.getdata(6,'d',7);
A.getdata(7,'d',7);
cout<<"please input the word:"<<endl;
cin>>p;
s=p;
while(*s!='\0')
{
if(*s>='0'&&*s<='9')
*s='d';
s++;
}
cout<<p<<endl;
A.judge(p,c);
if(c==2||c==4||c==7)
cout<<"the word is suitable for the rule"<<endl;
else
cout<<"the word is not suitable for the rule"<<endl;
getchar();
}
截图