C语言实现自动机FA,当然是确定的又穷的咯
结果图
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 else 11 { 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 else 51 { 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 else 87 printf("\nSo this string can't be identified\n"); 88 printf("press enter to exit the program\n"); 89 getchar(); 90 91 }