用C语言实现设计一个分支结构程序,分析所给输入字符串是否可为该自动机接受,s-a,-u,u-b-v,s-b-v,u-a-q,q-a-q,q-b-q,s-b-v,v-a-u,v-b-q
时间: 2023-06-24 12:08:16 浏览: 45
以下是一个用 C 语言实现的程序,可以判断输入字符串是否能被该自动机接受:
```c
#include <stdio.h>
#include <string.h>
int main() {
char input[100];
printf("请输入待判断的字符串:");
scanf("%s", input);
int len = strlen(input);
int state = 0; // 自动机的初始状态为 0
for (int i = 0; i < len; i++) {
switch (state) {
case 0:
if (input[i] == 's') {
state = 1;
} else {
printf("该字符串不可被接受!\n");
return 0;
}
break;
case 1:
if (input[i] == 'a') {
state = 2;
} else if (input[i] == 'u') {
state = 3;
} else {
printf("该字符串不可被接受!\n");
return 0;
}
break;
case 2:
if (input[i] == 'u') {
state = 3;
} else {
printf("该字符串不可被接受!\n");
return 0;
}
break;
case 3:
if (input[i] == 'b') {
state = 4;
} else if (input[i] == 'a') {
state = 6;
} else {
printf("该字符串不可被接受!\n");
return 0;
}
break;
case 4:
if (input[i] == 'v') {
state = 5;
} else {
printf("该字符串不可被接受!\n");
return 0;
}
break;
case 5:
printf("该字符串被接受!\n");
return 0;
case 6:
if (input[i] == 'q') {
state = 7;
} else {
printf("该字符串不可被接受!\n");
return 0;
}
break;
case 7:
if (input[i] == 'a' || input[i] == 'b') {
state = 7;
} else {
printf("该字符串不可被接受!\n");
return 0;
}
break;
}
}
printf("该字符串不可被接受!\n");
return 0;
}
```
注:该程序中的状态转移图可以参考提问中所给的自动机。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)