• 有穷自动机


    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        char p[30][30];
        char q[30][30];
        int line=0;
        int n;
        int i,j;
        int count=0;
        int k,t=0;
        int flag=0;
        int l,m=0;
        char VN[30]={''};
        char VT[30]={''};
        printf("请输入规则个数");
        scanf("%d",&n);
        line=n;
        for(i=0;i<30;i++)
            for(j=0;j<30;j++)
            {
                p[i][j]='';
                q[i][j]='';
            }
            printf("请输入文法: ");
            for(i=0;i<line;i++)
            {
                scanf("%s",p[i]);
            }
            l=0;
            m=0;
            for(i=0;i<line;i++)
            {
                for(j=0;j<30&&(p[i][j]!='');j++)
                {
                    if(p[i][j]<='z'&&p[i][j]>='a'||(p[i][j]<='9'&&p[i][j]>='0'))
                    {
                        flag=0;
                        for(t=0;VN[t]!='';t++)
                        {
                            if(VN[t]==p[i][j])
                            {
                                flag=1;break;
                            }
                        }
                        if(flag==0)
                        {
                            VN[l]=p[i][j];
                            l++;
                        }
                    }
                    if(p[i][j]<='Z'&&p[i][j]>='A')
                    {
                        flag=0;
                        for(t=0;t<30&&(VT[t]!='');t++)
                        {
                            if(VT[t]==p[i][j])
                            {
                                flag=1;
                                break;
                            }
                        }
                        if(flag==0)
                        {
                            VT[m]=p[i][j];
                            m++;
                        }
                    }
                }
            }
            count=0;
            k=0;
            for(i=0;i<line;i++)
            {
                for(j=4;j<30&&(p[i][j]!='');j++)
                {
                    if((p[i][j]<='z'&&p[i][j]>='a')||(p[i][j]<='Z'&&p[i][j]>='A')||(p[i][j]<='9'&&p[i][j]>='0'))
                    {
                        q[count][k]=p[i][j];
                        k++;
                    }
                    else
                    {
                        count++;
                        k=0;
                    }
                }
                count++;
                k=0;
            }
            flag=0;
            for(i=0;i<count;i++)
            {
                for(j=i+1;j<count;j++)
                {
                    if(strcmp(q[i],q[j])==0)
                    {
                        flag=1;
                        break;
                    }
                }
            }
            if(flag==1)
            {
                printf("是非确定的有穷状态自动机,即NFA ");
                printf("构造的有穷状态自动机为: ");
                printf("NFA   N=(K,E(总和的意思),M,{S},{Z}) ");
            }
            else
            {
                printf("是确定的有穷状态自动机,即DFA ");
                printf("构造的有穷状态自动机为: ");
                printf("DFA   N=(K,E(总和的意思),M,{S},{Z}) ");
            }
            printf("其中, K={S");
            for(i=0;i<30&&(VT!='');i++)
            {
                printf(",%c",VT[i]);
            }
            printf("} ");
            printf("E={");
            for(i=0;i<30&&(VN[i]!='');i++)
            {
                printf("%c   ",VN[i]);
            }
            printf("} ");
            //分离文法
            k=0;
            count=0;
            for(i=0;i<line;i++)
            {
                j=4;
                while(p[i][j]!='')
                {
                    if(k<4)
                    {
                        q[count][k]=p[i][k];
                        k++;
                    }
                    else
                    {
                        if((p[i][j]<='z'&&p[i][j]>='a')||(p[i][j]<='Z'&&p[i][j]>='A')||(p[i][j]<='9'&&p[i][j]>='0'))
                        {
                            q[count][k]=p[i][j];
                            k++;
                            j++;
                        }
                        if(p[i][j]=='l')
                        {
                            count++;
                            k=0;
                            j++;
                        }
                    }
                }
                count++;
                k=0;
            }
            printf(" ");
            //打印
            printf("M: ");
            l=0;
            while(VN[l]!='')
            {
                printf("M(S,%c)={",VN[l]);
                for(i=0;i<30;i++)
                {
                    for(j=4;j<30&&(q[i][j]!='');j++)
                    {
                        if(VN[l]==q[i][j]&&(q[i][j+1]=='')&&(q[i][j-1]=='='))
                            printf("%c",q[i][0]);
                    }
                }
                printf("} ");
                l++;
            }
            printf(" ");
            l=0;k=0;
            while(VT[k]!='')
            {
                l=0;
                while(VN[l]!='')
                {
                    printf("M(%c,%c)={",VT[k],VN[l]);
                    for(i=0;i<30;i++)
                    {
                        for(j=4;j<30&&(q[i][j]!='');j++)
                        {
                            if(VT[k]==q[i][j]&&VN[l]==q[i][j+1])
                                printf("%c",q[i][0]);
                        }
                    }
                    printf("} ");
                    l++;
                }
                k++;
                printf(" ");
            }
            system("pause");
    }

  • 相关阅读:
    eclipse中修改工程的Android版本
    解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:
    JavaScript 消息框
    JavaScript HTML DOM 元素(节点)
    JavaScript HTML DOM 事件
    JavaScript HTML DOM
    JavaScript 表单验证
    JavaScript 错误
    SQL利用Case When Then多条件判断
    SQL学习一
  • 原文地址:https://www.cnblogs.com/wangdahua/p/5017335.html
Copyright © 2020-2023  润新知