编译原理:实验四 无符号常数的自动机程序的设计与实现(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();
}

截图
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值