C語言實現自動機FA,當然是確定的又窮的咯

來源:互聯網
上載者:User

結果圖

 

 

 1 #include <stdio.h> 2 //s為初態,z為終態 3 int in(int s,int z) 4 { 5     if(s == z) 6     { 7         printf("3\nlook!the last status belongs to Z"); 8         return 1; 9     }10     else11     {12         return 0;13     }14 }15 //s為狀態,t為輸入的字元16 int step(int s,char t)17 {18     if(t == 'a')19         switch(s)20         {21             case 0:return 1;22             case 1:return 3;23             case 2:return 1;24             case 3:return 3;25         }26     else if(t == 'b')27         switch(s)28         {29             case 0:return 2;30             case 1:return 2;31             case 2:return 3;32             case 3:return 3;33         }34 }35 36 int realize(char *input)37 {38     int z = 3;39     int s,i;40     s = 0;41     for(i=0;input[i]!='\n';i++)42     {43         printf("%2d",s);44         s = step(s,input[i]);45     }46         if(in(s,z))47         {48             return 1;49         }50         else51         {52             return 0;53         }54 }55 56 main()57 {58     int i;59     int a;60     char input[40];61     printf("FA=({0,1,2,3},{a,b},M,0,{3})\n");62     printf("M:\n");63     printf("    M(0,a)=1    M(0,b)=2\n");64     printf("    M(1,a)=3    M(1,b)=2\n");65     printf("    M(2,a)=1    M(2,b)=3\n");66     printf("    M(3,a)=3    M(3,b)=3\n");67     printf("請輸入你要檢查的串");68 69 lop:    for(i=0;input[i-1] != '\n';i++)70         {71             scanf("%c",&input[i]);72         }73         for(i=0;input[i-1]!='\n';i++)74         {75             if(input[i] != 'a'&&input[i] != 'b'&&input[i] != '\n')76             {77                 printf("input error,enter again please:\n");78                 goto lop;79             }80                 81         }82         printf("the status sequence is :\n");83         a = realize(input);84         if(a == 1)85             printf("\nSo this string can be identified\n");86         else87             printf("\nSo this string can't be identified\n");88         printf("press enter to exit the program\n");89         getchar();90 91 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.