有穷自动机转换

#include<stdio.h>
#define M 100
char str[M][M];
void Go(char ch);
main()
{
    char ch;
    printf("\n输入需要识别的字符(以#结束):");
    ch=getchar();
    Go(ch);
}
void Go(char ch)
{
    int i=0,j=0,k,s,n,sum;
    while(ch!='#')
    {
        while(ch=='|')
        {
            str[i][j]='\0';
            j=0;
            i++;
            ch=getchar();
        }
        str[i][j]=ch;
        j++;
        ch=getchar();
    }
    str[i][j]='\0';
    sum=2;
    for(k=0;k<=i;k++)
    {
        s=0;
        n=1;
        while(str[k][s]!='\0')
        {
            if(str[k][s]=='.'||str[k][s]=='*')
            {
                if(str[k][s]=='.'&&n==1)
                {
                    n++;
                    printf("\nf(0,%c)=%d\n",str[k][s-1],sum);
                    if(str[k][s+2]=='\0')
                        printf("\nf(%d,%c)=1\n",sum,str[k][s+1]);
                    sum=sum+1;
                }
                else if(str[k][s]=='.'&&n!=1)
                {
                    n++;
                    printf("\nf(%d,%c)=%d\n",sum-1,str[k][s-1],sum);
                    if(str[k][s+2]=='\0')
                        printf("\nf(%d,%c)=1\n",sum,str[k][s+1]);
                    sum=sum+1;
                }
                else if(str[k][s]=='*'&&n==1)
                {
                    n++;
                    printf("\nf(0,~)=%d\n",sum);
                    printf("\nf(%d,%c)=%d\n",sum,str[k][s-1],sum);
                    if(str[k][s+1]=='\0')
                        printf("\nf(%d,~)=1\n",sum);
                    else
                        printf("\nf(%d,~)=%d\n",sum,sum+1);
                    sum=sum+1;
                }
                else if(str[k][s]=='*'&&n!=1)
                {
                    n++;
                    printf("\nf(%d,%c)=%d\n",sum,str[k][s-1],sum);
                    if(str[k][s+1]=='\0')
                        printf("\nf(%d,~)=1\n",sum);
                    else
                        printf("\nf(%d,~)=%d\n",sum,sum+1);
                    sum=sum+1;
                }
            }
            s++;
        }
    }
}

 

posted on 2016-12-16 23:04  204罗德广  阅读(172)  评论(0编辑  收藏  举报