salron3

auto

Mar 29th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 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. int Automat1(int c, int status, int *match);
  7.  
  8.  
  9. int main()
  10. {
  11.     FILE *input;
  12.     int c;
  13.     int status = 0;
  14.     int match = 0;
  15.     char fName[32];
  16.     int count = 0;
  17.  
  18.     printf("Enter name: ");
  19.     scanf("%s", &fName);
  20.  
  21.     if (!(input = fopen(fName, "r")))
  22.     {
  23.         printf("The file does not exist.\n");
  24.         system("pause");
  25.         exit(1);
  26.     }
  27.  
  28.     while ((c = fgetc(input)) != EOF)
  29.     {
  30.         //status = Automat(c, status, &match); //извикваме функцията за namirane na 'ab'
  31.         status = Automat1(c, status, &match); //извикваме функцията за namirane na '11'
  32.  
  33.         if (match == 2)
  34.         {
  35.             count++;
  36.             match = 0;
  37.         }
  38.     }
  39.  
  40.     printf("Found matches: %d\n", count);
  41.  
  42.     system("pause");
  43.     return 0;
  44. }
  45.  
  46. int Automat(int c, int status, int *match)//търси колко пъти се среща 'аb' във файлът
  47. {
  48.     switch (status)
  49.     {
  50.     case 0:
  51.         if (c == 'a')
  52.         {
  53.             *match = 1;
  54.             status = 1;
  55.         }
  56.         else
  57.         {
  58.             *match = 0;
  59.             status = 0;
  60.         }
  61.         break;
  62.  
  63.     case 1:
  64.         if (c == 'b')
  65.         {
  66.             *match = 2;
  67.             status = 0;
  68.         }
  69.         else
  70.         {
  71.             *match = 0;
  72.             status = 0;
  73.         }
  74.         break;
  75.     }
  76.     return status;
  77. }
  78.  
  79. int Automat1(int c, int status, int *match) //търси колко пъти се среща '11' във файлът
  80. {
  81.     switch (status)
  82.     {
  83.     case 0:
  84.         if (c == '1')
  85.         {
  86.             *match = 1;
  87.             status = 1;
  88.         }
  89.         else
  90.         {
  91.             *match = 0;
  92.             status = 0;
  93.         }
  94.         break;
  95.  
  96.     case 1:
  97.         if (c == '1')
  98.         {
  99.             *match = 2;
  100.             status = 0;
  101.         }
  102.         else
  103.         {
  104.             *match = 0;
  105.             status = 0;
  106.         }
  107.         break;
  108.     }
  109.     return status;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment