用 C 语言实现有限状态机

发布于 2024-12-16 01:48:50 字数 1406 浏览 3 评论 0原文

我一直在编写一个代码,它接受或拒绝一串输入符号作为指定语言的一部分。我已经为第一种语言编写了代码,但它不接受正确的事情,我想知道是否有人可以给我一个关于我哪里出错的提示。谢谢

问题:为什么该语言不能被正确接受或拒绝?

谢谢

我的代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花海 2024-12-23 01:48:50

您将初始状态设置为字符零而不是数字零。
试试这个:

static final char initial_state = 0;

You're setting initial state as a character zero and not a numeric zero.
Try this instead:

static final char initial_state = 0;
垂暮老矣 2024-12-23 01:48:50

您仅打开 current_state 而不是输入。

You are only switching on current_state and not the input.

享受孤独 2024-12-23 01:48:50

您已经定义了两次 initial_state,并且本地状态正在赢得“范围战争”。因此,在您的代码中,每次您认为自己引用的是这个 initial_state

static final char initial_state = '0';

您实际上引用的是这个初始状态:

char current_state, next_state, initial_state;  // this last guy here

此外,您正在执行一些数字和一些字符。因为您从键盘获取输入,所以您需要所有字符。在任何将状态定义为 1 或 0 的地方,请用单引号将其引起来,使其成为 '1''0'

然后,取出对所有状态重新定义1 = '1'的代码;我确实相信您要求程序重新定义数字 0x1 以表示数字 0x41 - 这太疯狂了。

这是最终结果(格式错误):

#include <stdio.h>

static const char initial_state = '0';
static const char accepting_state = '3';
int main(int argc, char* argv[]){
  int x;
  char current_state;

  current_state = initial_state;
  printf("Enter a series of characters (0 or 1)\n");
  printf("Press <ctrl-d> to finish.\n");
  printf("> ");
  while(scanf("%d", &x)!=EOF){
     x += '0';
         switch(current_state){
      case '0': /*initial state*/
       switch(x){
       case'0':current_state='1'; break;
       case'1':current_state='0'; break;
       default: goto fail;
       }
       break;
     case '1': /*Last input was 0*/
      switch(x){
      case'0':current_state='1'; break;
      case'1':current_state='2'; break;
      default: goto fail;
      }
      break;
     case '2': /*Last input was 1*/
      switch(x){
      case'0':current_state='3'; break;
      case'1':current_state='0'; break;
      default: goto fail;
      }
      break;
     case '3': /*Last input was 0*/
      switch(x){
      case'0':current_state='3'; break;
      case'1':current_state='3'; break;
      default: goto fail;
      }
      break;
     default: goto fail;
     }
     printf("> ");
  }

  if (current_state == accepting_state) {
    printf("Language accepts!\n");
  } else {
    printf("Language rejects.\n");
  }
  return 0;

fail:
  printf("Invalid input\n");
  return 1;
}

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 this initial_state:

static final char initial_state = '0';

you're actually referring to this initial state:

char current_state, next_state, initial_state;  // this last guy here

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):

#include <stdio.h>

static const char initial_state = '0';
static const char accepting_state = '3';
int main(int argc, char* argv[]){
  int x;
  char current_state;

  current_state = initial_state;
  printf("Enter a series of characters (0 or 1)\n");
  printf("Press <ctrl-d> to finish.\n");
  printf("> ");
  while(scanf("%d", &x)!=EOF){
     x += '0';
         switch(current_state){
      case '0': /*initial state*/
       switch(x){
       case'0':current_state='1'; break;
       case'1':current_state='0'; break;
       default: goto fail;
       }
       break;
     case '1': /*Last input was 0*/
      switch(x){
      case'0':current_state='1'; break;
      case'1':current_state='2'; break;
      default: goto fail;
      }
      break;
     case '2': /*Last input was 1*/
      switch(x){
      case'0':current_state='3'; break;
      case'1':current_state='0'; break;
      default: goto fail;
      }
      break;
     case '3': /*Last input was 0*/
      switch(x){
      case'0':current_state='3'; break;
      case'1':current_state='3'; break;
      default: goto fail;
      }
      break;
     default: goto fail;
     }
     printf("> ");
  }

  if (current_state == accepting_state) {
    printf("Language accepts!\n");
  } else {
    printf("Language rejects.\n");
  }
  return 0;

fail:
  printf("Invalid input\n");
  return 1;
}
桃扇骨 2024-12-23 01:48:50

由于内部开关是一个字符,我相信您的意思是它位于 x 而不是 current_state

 switch(current_state){
  case 0: /*initial state*/
   switch(x){
   case'0':next_state=1; break;
   case'1':next_state=0; break;
   }
   break;

Since the inner switch is a char, I believe you meant it to be on x and not current_state:

 switch(current_state){
  case 0: /*initial state*/
   switch(x){
   case'0':next_state=1; break;
   case'1':next_state=0; break;
   }
   break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文