Advertisement
salron3

Automat

Mar 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int Automat(int c, int status, int *match);
  6.  
  7. int main()
  8. {
  9.     FILE *input;
  10.     int c;
  11.     int status = 0;
  12.     int match = 0;
  13.     char fName[32];
  14.     int count = 0;
  15.  
  16.     printf("Enter name: ");
  17.     scanf("%s", &fName);
  18.  
  19.     if (!(input = fopen(fName, "r")))
  20.     {
  21.         printf("The file does not exist.\n");
  22.         system("pause");
  23.         exit(1);
  24.     }
  25.  
  26.     while ((c = fgetc(input)) != EOF)
  27.     {
  28.         status = Automat(c, status, &match);
  29.         if (match == 2)
  30.         {
  31.             count++;
  32.             match = 0;
  33.         }
  34.     }
  35.  
  36.     printf("Found matches: %d\n", count);
  37.  
  38.     system("pause");
  39.     return 0;
  40. }
  41.  
  42. int Automat(int c, int status, int *match)
  43. {
  44.     switch (status)
  45.     {
  46.     case 0:
  47.         if (c == 'a')
  48.         {
  49.             *match = 1;
  50.             status = 1;
  51.         }
  52.         else
  53.         {
  54.             *match = 0;
  55.             status = 0;
  56.         }
  57.         break;
  58.  
  59.     case 1:
  60.         if (c == 'b')
  61.         {
  62.             *match = 2;
  63.             status = 0;
  64.         }
  65.         else
  66.         {
  67.             *match = 0;
  68.             status = 0;
  69.         }
  70.         break;
  71.     }
  72.     return status;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement