home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / FSM.C < prev    next >
Text File  |  1994-04-03  |  823b  |  38 lines

  1. /*
  2. **  code snippet demonstrating a finite state machine (FSM)
  3. */
  4.  
  5. typedef enum {s0,s1,s2,s3,s4,...,sn,sexit} state;
  6.  
  7. state nextstate;
  8. int done = 0;
  9.  
  10. nextstate = s0;  /* set up to start with the first state */
  11. while(!done)
  12.    switch(nextstate)
  13.       {
  14.          case s0:
  15.             nextstate = do_state_0();
  16.             break;
  17.          case s1:
  18.             nextstate = do_state_1();
  19.             break;
  20.          case s2:
  21.             nextstate = do_state_2();
  22.             break;
  23.          case s3:
  24.              .
  25.              .
  26.              .
  27.              .
  28.          case sn:
  29.             nextstate = do_state_n();
  30.             break;
  31.          case sexit:
  32.             done = TRUE;
  33.             break;
  34.          default:
  35.             /*  some sort of unknown state */
  36.             break;
  37.       }
  38.