Advertisement
80LK

LR15 AKT

Nov 4th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. int main()
  2. {
  3.     int countLine = 0, countNumber = 0, x;
  4.     char s[20], c;
  5.     FILE *f;
  6.     fopen_s(&f, "test.txt", "r");
  7.  
  8.     setlocale(0, ".1251");
  9.  
  10.     /* Задание 1 */
  11.     while (!feof(f)) {
  12.         fgets(s, 20, f);
  13.         countLine++;
  14.     }
  15.     printf("Колиество строк: %d\n", countLine);
  16.  
  17.     /* Задание 2 */
  18.     bool a = false;
  19.     fseek(f, 0, 0);
  20.     countLine = 0;
  21.  
  22.     puts("\nСтроки, у которых первый и последний символ равны:");
  23.     while (!feof(f)) {
  24.         fgets(s, 20, f);
  25.         countLine++;
  26.         c = s[strlen(s) - 1];
  27.  
  28.         if(c == 10)
  29.             c = s[strlen(s) - 2];
  30.  
  31.         if (c == s[0]) {
  32.             a = true;
  33.             printf("%d\n", countLine);
  34.         }
  35.     }
  36.  
  37.     if (a == false)
  38.         puts("Таких строк нет.");
  39.  
  40.     /* Задание 3 */
  41.     int *arr = NULL, i = 0;
  42.     fseek(f, 0, 0);
  43.     countLine = 0;
  44.  
  45.     puts("\nСамые коротки строки: ");
  46.     while (!feof(f)) {
  47.         fscanf_s(f, "%d", &x);
  48.         countNumber++;
  49.        
  50.         c = fgetc(f);
  51.         if ((int)c == 10 || feof(f))
  52.         {
  53.             countLine++;
  54.             if (i < 2 || arr[0] > countNumber) {
  55.                 i = 2;
  56.                 arr = (int*)calloc(i, sizeof(int));
  57.                 arr[0] = countNumber;
  58.                 arr[1] = countLine;
  59.             }else if(arr[0] == countNumber){
  60.                 i++;
  61.                 arr = (int*)realloc(arr, i * sizeof(int));
  62.                 arr[i - 1] = countLine;
  63.             }
  64.  
  65.             countNumber = 0;
  66.         }
  67.     }
  68.  
  69.     for (int j = 1; j < i; j++) {
  70.         if (j != 1) printf(", ");
  71.         printf("%d", arr[j]);
  72.     }
  73.     free(arr);
  74.  
  75.     /* Задание 4 */
  76.     puts("\n\nОдинаковые строки:");
  77.     int **lines = (int**)malloc(sizeof(int**));
  78.     lines[0] = NULL;
  79.     fseek(f, 0, 0);
  80.     countLine = 0;
  81.     countNumber = 0;
  82.     a = false;
  83.     arr = (int*)malloc(sizeof(int));
  84.     arr[0] = 0;
  85.  
  86.     while (!feof(f)) {
  87.         if (fscanf_s(f, "%d", &x)) {
  88.             countNumber++;
  89.             lines[countLine] = (int*)realloc(lines[countLine], (countNumber + 1) * sizeof(int));
  90.             lines[countLine][0] = countNumber;
  91.             lines[countLine][countNumber] = x;
  92.         }
  93.  
  94.         c = fgetc(f);
  95.         if ((int)c == 10 || feof(f))
  96.         {
  97.             countLine++;
  98.             lines = (int**)realloc(lines, (countLine + 1) * sizeof(int*));
  99.             lines[countLine] = NULL;
  100.             countNumber = 0;
  101.         }
  102.     }
  103.  
  104.     for (int i = 0; i < countLine - 1; i++) {
  105.         int k;
  106.         for (k = 1; k <= arr[0]; k++)
  107.             if (i == arr[k])
  108.                 break;
  109.  
  110.         if (k != arr[0] + 1)
  111.             continue;
  112.  
  113.         int d = 0;
  114.         for (int j = i + 1; j < countLine; j++) {
  115.             if (lines[i][0] == lines[j][0]) {
  116.                 int l;
  117.                 for (l = 1; l <= lines[i][0]; l++)
  118.                     if (lines[i][l] != lines[j][l])
  119.                         break;
  120.  
  121.                 if (l == lines[i][0] + 1) {
  122.                     if (d == 0)
  123.                         printf("Строка %d идентина строкам: ", i+1);
  124.                     else
  125.                         printf(", ");
  126.  
  127.                     printf("%d", j+1);
  128.                     d++;
  129.  
  130.                     arr[0]++;
  131.                     arr = (int*)realloc(arr, (arr[0] + 1) * sizeof(int));
  132.                     arr[arr[0]] = j;
  133.                     a = true;
  134.                 }
  135.             }
  136.         }
  137.         if (d != 0)
  138.             puts("");
  139.     }
  140.     if (a == false)
  141.         puts("Таких строк нет.");
  142.  
  143.     for (int i = 0; i < countLine; i++) {
  144.         free(lines[i]);
  145.     }
  146.     free(lines);
  147.     free(arr);
  148.  
  149.     /* Задание 5 */
  150.     puts("\nМаксимальное значение строк:");
  151.     int max;
  152.     fseek(f, 0, 0);
  153.     countLine = 0;
  154.     countNumber = 0;
  155.     while (!feof(f)) {
  156.         fscanf_s(f, "%d", &x);
  157.         if (countNumber == 0 || x > max)
  158.             max = x;
  159.  
  160.         countNumber++;
  161.  
  162.         c = fgetc(f);
  163.         if ((int)c == 10 || feof(f))
  164.         {
  165.             countLine++;
  166.             countNumber = 0;
  167.             printf("%d) %d\n", countLine, max);
  168.         }
  169.     }
  170.  
  171.     /* Задание 6 */
  172.     puts("\nНомера введенного элемента:");
  173.     int n;
  174.     fseek(f, 0, 0);
  175.     countLine = 0;
  176.     countNumber = 0;
  177.     a = false;
  178.    
  179.     printf("Введите элемент: ");
  180.     scanf_s("%d", &n);
  181.  
  182.     while (!feof(f)) {
  183.         if (countNumber == 0)
  184.             printf("%d) ", countLine + 1);
  185.  
  186.         fscanf_s(f, "%d", &x);
  187.         countNumber++;
  188.  
  189.         if (x == n) {
  190.             printf("%d ", countNumber);
  191.             a = true;
  192.         }
  193.  
  194.         c = fgetc(f);
  195.         if ((int)c == 10 || feof(f))
  196.         {
  197.             countLine++;
  198.             countNumber = 0;
  199.             if (a == false)
  200.                 printf("Такого элемента нет.");
  201.  
  202.             puts("");
  203.             a = false;
  204.         }
  205.     }
  206.  
  207.     fclose(f);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement