Advertisement
informaticage

Automata sim C

Dec 8th, 2021
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. // Alphabet A B C D E X
  2. // Strings to be recognized
  3. // String 1: CABED
  4. // String 2: ABBACCA
  5. // String 3: ECADE
  6. // String 4: ADACE
  7.  
  8. #include <stdio.h>
  9.  
  10. int automata_1(char c, int state);
  11. int automata_2(char c, int state);
  12. int automata_3(char c, int state);
  13. int automata_4(char c, int state);
  14.  
  15. #define STATE_AUTOMATA_1_END 5
  16. #define STATE_AUTOMATA_2_END 7
  17. #define STATE_AUTOMATA_3_END 5
  18. #define STATE_AUTOMATA_4_END 5
  19.  
  20. int main() {
  21.   char user_input;
  22.   int state_automata_1 = 0,
  23.       state_automata_2 = 0,
  24.       state_automata_3 = 0,
  25.       state_automata_4 = 0;
  26.  
  27.   do {
  28.     // Input character
  29.     user_input = getchar();
  30.     // Clearing input buffer
  31.     getchar();
  32.  
  33.     // Exec and check automata 1
  34.     state_automata_1 = automata_1(user_input, state_automata_1);
  35.     if (state_automata_1 == STATE_AUTOMATA_1_END) {
  36.       printf("Riconosciuta CABED\n");
  37.     }
  38.  
  39.     // Exec and check automata 2
  40.     state_automata_2 = automata_2(user_input, state_automata_2);
  41.     if (state_automata_2 == STATE_AUTOMATA_2_END) {
  42.       printf("Riconosciuta ABBACCA\n");
  43.     }
  44.  
  45.     // Exec and check automata 3
  46.     state_automata_3 = automata_3(user_input, state_automata_3);
  47.     if (state_automata_3 == STATE_AUTOMATA_3_END) {
  48.       printf("Riconosciuta ECADE\n");
  49.     }
  50.  
  51.     // Exec and check automata 4
  52.     state_automata_4 = automata_4(user_input, state_automata_4);
  53.     if (state_automata_4 == STATE_AUTOMATA_4_END) {
  54.       printf("Riconosciuta ADACE\n");
  55.     }
  56.  
  57.     // printf(
  58.     //     "Current states: \n"
  59.     //     "Automata 1: %d"
  60.     //     "Automata 2: %d"
  61.     //     "Automata 3: %d"
  62.     //     "Automata 4: %d\n",
  63.     //     state_automata_1, state_automata_2, state_automata_3, state_automata_4
  64.     // );
  65.      
  66.   } while (user_input != 'X');
  67.  
  68.   return 0;
  69. }
  70.  
  71. int automata_1(char c, int state) {
  72.   // C, 0
  73.   // A, 1
  74.   // B, 2
  75.   // E, 3
  76.   // D, 4
  77.   if ((c == 'C' && state == 0) || (c == 'A' && state == 1) ||
  78.       (c == 'B' && state == 2) || (c == 'E' && state == 3) ||
  79.       (c == 'D' && state == 4)) {
  80.     return state + 1;
  81.   }
  82.  
  83.   return 0;
  84. }
  85.  
  86. // ABBACCA
  87. int automata_2(char c, int state) {
  88.   if ((c == 'A' && state == 0) || (c == 'B' && state == 1) ||
  89.       (c == 'B' && state == 2) || (c == 'A' && state == 3) ||
  90.       (c == 'C' && state == 4) || (c == 'C' && state == 5) ||
  91.       (c == 'A' && state == 6)) {
  92.     return state + 1;
  93.   }
  94.  
  95.   return 0;
  96. }
  97.  
  98. // ECADE
  99. int automata_3(char c, int state) {
  100.   if ((c == 'E' && state == 0) || (c == 'C' && state == 1) ||
  101.       (c == 'A' && state == 2) || (c == 'D' && state == 3) ||
  102.       (c == 'E' && state == 4)) {
  103.     return state + 1;
  104.   }
  105.  
  106.   return 0;
  107. }
  108.  
  109. // ADACE
  110. int automata_4(char c, int state) {
  111.   if ((c == 'A' && state == 0) || (c == 'D' && state == 1) ||
  112.       (c == 'A' && state == 2) || (c == 'C' && state == 3) ||
  113.       (c == 'E' && state == 4)) {
  114.     return state + 1;
  115.   }
  116.  
  117.   return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement