Advertisement
tdulik

FSM - parity detector

Nov 3rd, 2020 (edited)
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. int main()
  5. {
  6.     enum { EVEN, ODD, ERROR } state=EVEN;
  7.     int c;
  8.     printf("Enter stream of 0 and 1 characters. End=CTRL+Z\n");
  9.     while ((c=getchar())!=EOF) {
  10.         if (c=='\n') continue;
  11.         switch (state) {
  12.         case EVEN:
  13.             if (c=='1') {
  14.                 state=ODD;
  15.                 puts("ODD");
  16.             } else if (c=='0') {
  17.                 puts("EVEN");
  18.             } else {
  19.                 puts("ERROR");
  20.                 state=ERROR;
  21.                 return 0;
  22.             }
  23.             break;
  24.         case ODD:
  25.             if (c=='1') {
  26.                 state=EVEN;
  27.                 puts("EVEN");
  28.             } else if (c=='0') {
  29.                 puts("ODD");
  30.             } else {
  31.                 puts("ERROR");
  32.                 state=ERROR;
  33.                 return 0;
  34.             }
  35.             break;
  36.         case ERROR:
  37.             break;
  38.         }
  39.     }
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement