编程模拟自动机的实现方法(用状态转换图)

来源:互联网 发布:dell风扇控制软件 编辑:程序博客网 时间:2024/06/07 13:10

算法如下:

State=i;
Nextchar(ch);
While(state!=error&&ch!=EOF)
 switch (state)     {
Case i: switch(ch)  {
       case a: state=j;  break;
       case b: state=k;  break;
       }
Case j: switch(ch)  {…..}
}
If (state是终止状态 and ch=EOF) print(OK);
   else  print(error);

 

 

(a|b)*ab  实验

 

C语言实现代码如下:

 

#include<stdio.h>

#define N 1000

 

void main()

{

   char state='A';

   char ch[N];

   int n,m;

   printf("请输入由 ab字母组成的字符串:/n");

   gets(ch);

  

 

  for(n=0;ch[n];n++)

   {

    if(ch[n]=='a'||ch[n]=='b')

    {

 

s:     switch(state)

      {

         case 'A':switch(ch[n]){

            case'a': state='B'; break;

            case'b': state='A'; continue;

                 } break;

 

         case 'B':switch(ch[n]){

            case'a': state='B'; continue;

            case'b': state='D'; break;

                 } break;

 

         case 'C':switch(ch[n]){

            case'a': state='B'; break;

            case'b': state='A'; break;

                 } break;

 

            case'D':goto h;

      }

    }

   

    else if(ch[n]=='')

    {

       for(m=n+1;ch[m]!='/0';m++)

       {

           ch[m-1]=ch[m];   

       }

       for(n=0;ch[n];n++)

           ch[n]=ch[m];

       goto s;

    }

 

    else

       printf("please input a||b/n");

   }

 

 

   h:   if( (state=='D')&&(ch[n--]=='/0') )

           printf("可被接受/n");

        else

           printf("不可被接受/n");

 

}

 

 

原创粉丝点击