1118有限自动机的识别与构造

 
  
  1 #include<stdio.h>
  2 #define Max 100
  3 int flag=0;
  4 struct data
  5 {
  6     int a;
  7     char r;
  8     int b;
  9 };
 10 
 11 typedef struct data Data;
 12 void First(int k,int Mark1[],int arry[]);
 13 void Second(int k,int Mark2[],int arry[]);
 14 void Fird(int k,int Mark3[],int arry[]);
 15 void Fouth(int k,int Mark4[],int arry[]);
 16 void Fifth(int k,int Mark5[],int arry[]);
 17 
 18 main()
 19 {
 20     Data fa[Max];
 21     char ch;
 22     char string[Max];
 23     int Mark1[Max];
 24     int Mark2[Max];
 25     int Mark3[Max];
 26     int Mark4[Max];
 27     int Mark5[Max];
 28     int  arry[Max];
 29     
 30     //int flag=0;
 31     int i=-1,j=0,k=0,m=0,n=0;
 32     int count1=0;
 33     int count2=0;
 34     int count3=0;
 35     int count4=0;
 36     int count5=0;
 37 
 38     printf("请输入正规式:");
 39     do{
 40         i++;
 41         scanf("%c",&ch);
 42         string[i]=ch;
 43     }while(ch!='#');
 44     string[i]='\0';
 45     for(j=0;j<i;j++)
 46     {
 47         if(string[j]=='|')
 48         {
 49             Mark1[count1]=j;
 50             count1++;
 51         }
 52         else if(string[j]=='.')
 53         {
 54             Mark2[count2]=j;
 55             count2++;
 56         }
 57         else if(string[j]=='*')
 58         {
 59             Mark3[count3]=j;
 60             count3++;
 61         }
 62         else if(string[j]=='(')
 63         {
 64             Mark4[count4]=j;
 65             count4++;
 66         }
 67         else if(string[j]==')')
 68         {
 69             Mark5[count5]=j;
 70             count5++;
 71         }
 72 
 73         else
 74             continue;
 75     }
 76 
 77     while(m!=i)
 78     {
 79         for(k=0;k<count1;k++)
 80             First(k,Mark1,arry);
 81         for(k=0;k<count2;k++)
 82             Second(k,Mark2,arry);
 83         for(k=0;k<count3;k++)
 84             Fird(k,Mark3,arry);
 85         for(k=0;k<count4;k++)
 86             Fouth(k,Mark4,arry);
 87         for(k=0;k<count5;k++)
 88             Fifth(k,Mark5,arry);
 89         m++;
 90     }
 91 
 92 
 93     printf("f(%d,%c)=%d\n",fa[i].a,fa[i].b,fa[i].r);
 94 
 95 
 96 
 97     //printf("%s\n",string);
 98     //for(k=0;k<count1;k++)
 99     //printf("%d\n",Mark1[k]);
100 }
101 void First(int k,int Mark1[],int arry[])
102 {
103 
104 
105 }
106 void Second(int k,int Mark2[],int arry[])
107 {
108 
109 
110 }
111 void Fird(int k,int Mark3[],int arry[])
112 {
113 
114 
115 }
116 void Fouth(int k,int Mark4[],int arry[])
117 {
118 
119 
120 }
121 void Fifth(int k,int Mark5[],int arry[])
122 {
123 
124 }


1
#include <string.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main() 5 { 6 char p[30][30]; //存放文法 7 char q[30][30]; 8 int line = 0; 9 int n; 10 int i, j; 11 int count =0; 12 int k,t = 0; 13 int flag = 0; 14 int l,m = 0; 15 char VN[30] = {'\0'}; //存放非终结符号 16 char VT[30] = {'\0'}; //存放终结符号 17 printf("请输入规则个数:"); 18 scanf("%d",&n); 19 line = n; 20 for(i = 0; i < 30; i++) //给字符串数组p、q全部赋值为'\0' 21 for(j=0;j<30;j++) 22 { 23 p[i][j]='\0'; 24 q[i][j]='\0'; 25 } 26 printf("请输入文法:\n"); 27 for(i = 0; i < line; i++) 28 { 29 scanf("%s",p[i]); 30 } //把字符分为终结符号合非终结符号 31 l=0; 32 m=0; 33 for(i = 0;i < line; i++) 34 { 35 for(j = 0;j < 30&&(p[i][j] != '\0');j++) 36 { // 非终结符号放入数组VN中 37 if((p[i][j]<='z' && p[i][j]>='a')||(p[i][j]<='9' && p[i][j]>='0')) 38 { 39 flag = 0; 40 for(t=0; VN[t] != '\0';t++) 41 { 42 if(VN[t] == p[i][j]) 43 { 44 flag = 1; 45 break; 46 } 47 } 48 if(flag == 0) 49 { 50 VN[l] = p[i][j]; 51 l++; 52 } 53 } 54 // 终结符号放入数组VT中 55 if(p[i][j]<='Z' && p[i][j]>='A') 56 { 57 flag = 0; 58 for(t = 0; t<30&&(VT[t] != '\0'); t++) 59 { 60 if(VT[t] == p[i][j]) 61 { 62 flag = 1; 63 break; 64 } 65 } 66 if(flag==0) 67 { 68 VT[m] = p[i][j]; 69 m++; 70 } 71 } 72 } 73 } //把规则右部分分离放入数组q中 74 count = 0; 75 k =0; 76 for(i = 0;i < line;i++) 77 { 78 for(j = 4;j < 30 && (p[i][j] != '\0');j++) 79 { 80 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')) 81 { 82 q[count][k] = p[i][j]; 83 k++; 84 } 85 else 86 { 87 count++; 88 k =0; 89 } 90 } 91 count++; 92 k=0; 93 } //判断是确定的还是非确定的有穷状态自动机并进行前半部分打印 94 //判断依据:q数组中每一行字符串是否相同 95 flag = 0; 96 for(i = 0;i < count i++) 97 { 98 for(j = i+1; j < count j++) 99 { 100 if(strcmp(q[i],q[j]) == 0) 101 { 102 flag = 1; 103 break; 104 } 105 } 106 } 107 if(flag == 1) 108 { 109 printf("是非确定的有穷状态自动机,即NFA\n\n"); 110 printf("构造的有穷状态自动机为:\n"); 111 printf("NFA N =(K,∑,M,{S},{Z})\n"); 112 } 113 else 114 { 115 printf("是确定的有穷状态自动机,即DFA\n\n\n"); 116 printf("构造的有穷状态自动机为:\n"); 117 printf("DFA D=(K,∑,M,{S},{Z})\n"); 118 } 119 printf("其中,\nK = {S"); 120 for(i = 0;i < 30 && (VT[i]!='\0');i++) 121 { 122 printf(",%c",VT[i]); 123 } 124 printf("}\n"); 125 printf("∑={"); 126 for(i = 0;i < 30 && (VN[i]!='\0');i++) 127 { 128 printf("%c",VN[i]); 129 } 130 printf("}\n"); //分离文法; 131 k = 0; 132 count = 0; 133 for(i = 0; i < line; i++) 134 { 135 j = 4; 136 while(p[i][j] != '\0') 137 { 138 if(k < 4) 139 { 140 q[count][k] = p[i][k]; 141 k++; 142 } 143 else 144 { 145 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')) 146 { 147 q[count][k] = p[i][j]; 148 k++; 149 j++; 150 } 151 if(p[i][j] == '|') 152 { 153 count++; 154 k = 0; 155 j++; 156 } 157 } 158 } 159 count++; 160 k=0; 161 } 162 printf("\n"); 163 //打印M后半部分 164 printf("M:\n"); 165 l =0; 166 while(VN[l] != '\0') 167 { 168 printf("M(S,%c)={",VN[l]); 169 for(i = 0; i < 30 i++) 170 { 171 for(j = 4; j<30 && (q[i][j] != '\0');j++) 172 { 173 if(VN[l]==q[i][j]&&(q[i][j+1] == '\0')&&(q[i][j-1] == '=')) 174 printf("%c ",q[i][0]); 175 } 176 } 177 printf("}\t"); 178 l++; 179 } 180 printf("\n"); 181 l = 0; 182 k = 0; 183 while(VT[k] != '\0') 184 { 185 l =0; 186 while(VN[l] != '\0') 187 { 188 printf("M(%c,%c)={",VT[k],VN[l]); 189 for(i = 0; i < 30 i++) 190 { 191 for(j = 4; j<30 && (q[i][j] != '\0');j++) 192 { 193 if(VT[k] == q[i][j] && VN[l] == q[i][j+1]) 194 printf("%c ",q[i][0]); 195 } 196 } 197 printf("}\t"); 198 l++; 199 } 200 k++; 201 printf("\n"); 202 } 203 system("pause"); 204 }


总结:

第一个是自己思考的,接下来不知道怎么弄,虽然做得不好,可是也是经过思考的。第二个是借鉴同学的,用来学习 的。知道利用递归来完成自动机,可是不会算法,只能依稀地写下大概的结构框架。

 

你可能感兴趣的:(1118有限自动机的识别与构造)