在C中实现有限状态机

时间:2011-11-15 18:27:17

标签: c project state-machine

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

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

由于

我的代码:

#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;
}

4 个答案:

答案 0 :(得分:2)

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

static final char initial_state = 0;

答案 1 :(得分:2)

您只是打开current_state而不是输入。

答案 2 :(得分:1)

由于内部开关是char,我相信你的意思是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;

答案 3 :(得分:1)

你已经定义了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;
}