首页 > 代码库 > 实验三 有限自动机的构造与识别

实验三 有限自动机的构造与识别

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 101
int n=1;
void tackBack(char ch[],int start,int end,int begain,int final);
int main(){
    char ch[MAX];
    int size;
    scanf("%s",ch);
    size=strlen(ch);
    tackBack(ch,0,size,0,1);
    return 0;
}

void tackBack(char ch[],int start,int end,int begain,int final)
{
    
    int i=start;    
    while(!(ch[i] == |) && i<end)
        i++;
    if(ch[i]==|){
        tackBack(ch,start,i-1,begain,final);
        tackBack(ch,i+1,end,begain,final);    
    }
    i=start;
    //printf("start=%d\tend=%d\n",start,end);
    
    if(ch[i]>=a && ch[i]<=z)
    {
        if(ch[i+1]==.)
        {
            n++;
            printf("f<%d,%c>=%d\n",begain,ch[i],n);                
        }
        else if(ch[i+1]==*)
        {
            n++;
            printf("f(%d,~)=%d\n",begain,n);
            printf("f(%d,~)=%d\n",n,final);
            printf("f(%d,%c)=%d\n",n,ch[i],n);
        }
    }    
    i+=2;
    while(i<end)
    {
        if(ch[i]>=a && ch[i]<=z)
        {
            if(ch[i+1]==.)
            {
                printf("f<%d,%c>=%d\n",n,ch[i],n+1);        
                n++;
            }
            else if(ch[i+1]==*)
            {
                n++;
                printf("f(%d,~)=%d\n",begain,n);
                printf("f(%d,~)=%d\n",n,final);
                printf("f(%d,%c)=%d\n",n,ch[i],n);
            }
        }    
        i++;
    }
    if(ch[i]>=a && ch[i]<=z)
    {
        
        if(ch[i+1]==*)
        {
            printf("f(%d,%c)=%d\n",n,ch[i],final);
        }
        else
        {
            printf("f<%d,%c>=%d\n",n,ch[i],final);
        }
    }

}

实验结果

技术分享

实验三 有限自动机的构造与识别