Advertisement
salron3

Short Automats

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