user-0 提问: 用C语言实现此状态机的代码

回复:
user: user-0
content: 引用 2 楼 Ruby0819的回复:没怎么测试，你看看吧，希望有帮助。   #include <stdio.h> #include <stdlib.h>  typedef enum {     KEY_ACT_1L = 0,     KEY_ACT_1R,     KEY_ACT_2L,     KEY_ACT_2R,     KEY_ACT_3L,     KEY_ACT_3R,      KEY_ACT_MAX }eKEY_ACT;  typedef enum {     LOCK_STS_LOCKED = 0,     LOCK_STS_KEY_A_IN,     LOCK_STS_KEY_B_IN,     LOCK_STS_ALARM,     LOCK_STS_UNLOCK,      LOCK_STS_MAX }eLOCK_STATUS;  #define eGET_ACT_STR( idx )     scpKEY_ACT_str[ ( idx ) ]  #define eGET_LOCK_STR( idx )    scpLOCK_STS_str[ ( idx ) ]  #define eGET_NEXT_MODE( currMode, act )   stMODE_TBL[ act ][ currMode ]  #define PTR                     printf_s   static const char *scpLOCK_STS_str [LOCK_STS_MAX] = {     "LOCK_STS_LOCKED",     "LOCK_STS_KEY_A_IN",     "LOCK_STS_KEY_B_IN",     "LOCK_STS_ALARM",     "LOCK_STS_UNLOCK",  };  static const char* scpKEY_ACT_str[KEY_ACT_MAX] = {     "KEY_ACT_1L",     "KEY_ACT_1R",     "KEY_ACT_2L",     "KEY_ACT_2R",     "KEY_ACT_3L",     "KEY_ACT_3R", };  static const eLOCK_STATUS  stMODE_TBL[6][3] = {     { LOCK_STS_KEY_A_IN,        LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_UNLOCK     },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_KEY_B_IN,     LOCK_STS_ALARM      } };   int main( int argc, char* argv[] ) {     eLOCK_STATUS    curr_mode = LOCK_STS_LOCKED;     eKEY_ACT        e_act;     eLOCK_STATUS    next_mode;     int cnt = 0, input_num;       while ( 1 )     {         do         {             PTR( "\nPlease input No. of the password as the following: " );             for ( cnt = 0; cnt < KEY_ACT_MAX; cnt++ )             {                 PTR( "\n No.%01d %s ", cnt + 1, eGET_ACT_STR( cnt ) );             }             PTR("\n" );             scanf_s("%d", &input_num );             if ( (input_num <= 0) || (input_num > KEY_ACT_MAX) )             {                 e_act = KEY_ACT_MAX;                 PTR( "\n Your input is wrong!" );              }             else             {                 e_act = (eKEY_ACT)(input_num - 1);                 PTR( "\n Your input is right!" );             }         } while ( e_act == KEY_ACT_MAX );          next_mode =  eGET_NEXT_MODE ( curr_mode, e_act );          PTR("\ncurr = [%s] Act = [%s] nextmode= [%s] ",              eGET_LOCK_STR( curr_mode ),             eGET_ACT_STR( e_act ),             eGET_LOCK_STR( next_mode )             );          if ( next_mode == LOCK_STS_ALARM)         {                               PTR("\n Are you a pig?" );             break;         }         else if ( next_mode == LOCK_STS_UNLOCK )         {             PTR( "\n You are very clever." );             break;         }         else if ( next_mode >= LOCK_STS_ALARM )         {             PTR( "\n This is a bug." );             break;         }         else         {             /* Nothing to do */         }         curr_mode = next_mode;      };       return 0; }   感谢感谢，谢谢大佬

user: user-1
content: 没怎么测试，你看看吧，希望有帮助。   #include <stdio.h> #include <stdlib.h>  typedef enum {     KEY_ACT_1L = 0,     KEY_ACT_1R,     KEY_ACT_2L,     KEY_ACT_2R,     KEY_ACT_3L,     KEY_ACT_3R,      KEY_ACT_MAX }eKEY_ACT;  typedef enum {     LOCK_STS_LOCKED = 0,     LOCK_STS_KEY_A_IN,     LOCK_STS_KEY_B_IN,     LOCK_STS_ALARM,     LOCK_STS_UNLOCK,      LOCK_STS_MAX }eLOCK_STATUS;  #define eGET_ACT_STR( idx )     scpKEY_ACT_str[ ( idx ) ]  #define eGET_LOCK_STR( idx )    scpLOCK_STS_str[ ( idx ) ]  #define eGET_NEXT_MODE( currMode, act )   stMODE_TBL[ act ][ currMode ]  #define PTR                     printf_s   static const char *scpLOCK_STS_str [LOCK_STS_MAX] = {     "LOCK_STS_LOCKED",     "LOCK_STS_KEY_A_IN",     "LOCK_STS_KEY_B_IN",     "LOCK_STS_ALARM",     "LOCK_STS_UNLOCK",  };  static const char* scpKEY_ACT_str[KEY_ACT_MAX] = {     "KEY_ACT_1L",     "KEY_ACT_1R",     "KEY_ACT_2L",     "KEY_ACT_2R",     "KEY_ACT_3L",     "KEY_ACT_3R", };  static const eLOCK_STATUS  stMODE_TBL[6][3] = {     { LOCK_STS_KEY_A_IN,        LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_UNLOCK     },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_ALARM,        LOCK_STS_ALARM      },     { LOCK_STS_ALARM,           LOCK_STS_KEY_B_IN,     LOCK_STS_ALARM      } };   int main( int argc, char* argv[] ) {     eLOCK_STATUS    curr_mode = LOCK_STS_LOCKED;     eKEY_ACT        e_act;     eLOCK_STATUS    next_mode;     int cnt = 0, input_num;       while ( 1 )     {         do         {             PTR( "\nPlease input No. of the password as the following: " );             for ( cnt = 0; cnt < KEY_ACT_MAX; cnt++ )             {                 PTR( "\n No.%01d %s ", cnt + 1, eGET_ACT_STR( cnt ) );             }             PTR("\n" );             scanf_s("%d", &input_num );             if ( (input_num <= 0) || (input_num > KEY_ACT_MAX) )             {                 e_act = KEY_ACT_MAX;                 PTR( "\n Your input is wrong!" );              }             else             {                 e_act = (eKEY_ACT)(input_num - 1);                 PTR( "\n Your input is right!" );             }         } while ( e_act == KEY_ACT_MAX );          next_mode =  eGET_NEXT_MODE ( curr_mode, e_act );          PTR("\ncurr = [%s] Act = [%s] nextmode= [%s] ",              eGET_LOCK_STR( curr_mode ),             eGET_ACT_STR( e_act ),             eGET_LOCK_STR( next_mode )             );          if ( next_mode == LOCK_STS_ALARM)         {                               PTR("\n Are you a pig?" );             break;         }         else if ( next_mode == LOCK_STS_UNLOCK )         {             PTR( "\n You are very clever." );             break;         }         else if ( next_mode >= LOCK_STS_ALARM )         {             PTR( "\n This is a bug." );             break;         }         else         {             /* Nothing to do */         }         curr_mode = next_mode;      };       return 0; }

user: user-1
content: 状态转换表都有了，用C语言做成表。 然后，初始化初始状态， 主循环查表， 到终态返回，就行了。