編譯原理:實驗四 無符號常數的自動機程序的設計與實現(C++)

一、實驗目的
編寫程序實現自動機對無符號常數的識別程序
二、實驗重難點
自動機的程序實現及識別
三、實驗內容與要求
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();
}

截圖
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章