用 C 语言实现有限状态机
我一直在编写一个代码,它接受或拒绝一串输入符号作为指定语言的一部分。我已经为第一种语言编写了代码,但它不接受正确的事情,我想知道是否有人可以给我一个关于我哪里出错的提示。谢谢
问题:为什么该语言不能被正确接受或拒绝?
谢谢
我的代码:
#include <stdio.h>
static final char initial_state = '0';
static final char 0 = '0';
static final char 1 = '1';
static final char 2 = '2';
static final char 3 = '3';
int main(int argc, char* argv[]){
int x;
char current_state, next_state, initial_state;
current_state = initial_state;
printf("Enter a string of characters: ");
while(scanf("%d", &x)!=EOF){
switch(current_state){
case 0: /*initial state*/
switch(current_state){
case'0':next_state=1; break;
case'1':next_state=0; break;
}
break;
case 1: /*Last input was 0*/
switch(current_state){
case'0':next_state=1; break;
case'1':next_state=2; break;
}
break;
case 2: /*Last input was 1*/
switch(current_state){
case'0':next_state=3; break;
case'1':next_state=0; break;
}
break;
case 3: /*Last input was 0*/
switch(current_state){
case'0':next_state=3; break;
case'1':next_state=3; break;
}
break;
}
current_state=next_state;
}
if((current_state==next_state)){
printf("Language 1 accepts");
}else if((current_state!=next_state)){
printf("Language 1 rejects");
}
return 0;
}
I have been writing a code that either accepts or rejects a string of input symbols as part of a specified language. And I have written code for the first language but it doesn't accept the right thing and i was wondering if any one could give me a hint as to where i went wrong. thanks
Question: Why wont the language be accepted or rejected correctly?
Thanks
My code:
#include <stdio.h>
static final char initial_state = '0';
static final char 0 = '0';
static final char 1 = '1';
static final char 2 = '2';
static final char 3 = '3';
int main(int argc, char* argv[]){
int x;
char current_state, next_state, initial_state;
current_state = initial_state;
printf("Enter a string of characters: ");
while(scanf("%d", &x)!=EOF){
switch(current_state){
case 0: /*initial state*/
switch(current_state){
case'0':next_state=1; break;
case'1':next_state=0; break;
}
break;
case 1: /*Last input was 0*/
switch(current_state){
case'0':next_state=1; break;
case'1':next_state=2; break;
}
break;
case 2: /*Last input was 1*/
switch(current_state){
case'0':next_state=3; break;
case'1':next_state=0; break;
}
break;
case 3: /*Last input was 0*/
switch(current_state){
case'0':next_state=3; break;
case'1':next_state=3; break;
}
break;
}
current_state=next_state;
}
if((current_state==next_state)){
printf("Language 1 accepts");
}else if((current_state!=next_state)){
printf("Language 1 rejects");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将初始状态设置为字符零而不是数字零。
试试这个:
You're setting initial state as a character zero and not a numeric zero.
Try this instead:
您仅打开
current_state
而不是输入。You are only switching on
current_state
and not the input.您已经定义了两次
initial_state
,并且本地状态正在赢得“范围战争”。因此,在您的代码中,每次您认为自己引用的是这个initial_state
:您实际上引用的是这个初始状态:
此外,您正在执行一些数字和一些字符。因为您从键盘获取输入,所以您需要所有字符。在任何将状态定义为 1 或 0 的地方,请用单引号将其引起来,使其成为
'1'
或'0'
。然后,取出对所有状态重新定义
1 = '1'
的代码;我确实相信您要求程序重新定义数字 0x1 以表示数字 0x41 - 这太疯狂了。这是最终结果(格式错误):
You have defined
initial_state
twice, and the local one is winning the "scope war." So in your code, every time you think you're referring to thisinitial_state
:you're actually referring to this initial state:
Additionally, you're doing some numerics and some characters. You want all characters since you're taking input from the keyboard. Any place that you define a state as a 1 or a 0, put single quotes around it so it's a
'1'
or a'0'
.Then, take out the code that redefines
1 = '1'
for all the states; I do believe you're asking the program to redefine the number 0x1 to mean the number 0x41 -- that's crazy.Here's the final result (badly formatted):
由于内部开关是一个字符,我相信您的意思是它位于
x
而不是current_state
:Since the inner switch is a char, I believe you meant it to be on
x
and notcurrent_state
: