Advertisement
codegod313

Lexa_volk_3

Apr 16th, 2020
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <locale.h>
  6.  
  7.  
  8. bool chek(char *s, char **words, int &num, int count[])
  9. {
  10.     for (int i = 0; i < num; i++)
  11.     {
  12.         if (strcmp(s, words[i]) == 0)
  13.         {
  14.             count[i]++;
  15.             return true;
  16.         }
  17.            
  18.     }
  19.     return false;
  20. }
  21.  
  22. void q_sort(int b, int e,int arr[],char **words)
  23. {
  24.     int l = b, r = e;
  25.     int piv = arr[(l + r) / 2];
  26.     while (l <= r)
  27.     {
  28.         while (arr[l] < piv)
  29.             l++;
  30.         while (arr[r] > piv)
  31.             r--;
  32.         if (l <= r)
  33.         {
  34.             int c = arr[l];
  35.             arr[l] = arr[r];
  36.             arr[r] = c;
  37.             char *s = words[l];
  38.             words[l] = words[r];
  39.             words[r] = s;
  40.             l++;
  41.             r--;
  42.         }
  43.            
  44.     }
  45.     if (b < r)
  46.         q_sort(b, r, arr, words);
  47.     if (e > l)
  48.         q_sort(l, e, arr, words);
  49. }  
  50.  
  51.  
  52.  
  53. int main()
  54. {
  55.     setlocale(LC_ALL, "Russian");
  56.     FILE *fin = fopen("input.txt", "r");
  57.     char *s = (char *)malloc(sizeof(char) * 256);
  58.     char **words = (char **)malloc(sizeof(char **) * 256);
  59.     int num = 0, count[256];
  60.     int n;
  61.     printf("Введите n\n");
  62.     scanf_s("%d", &n);
  63.     for (int i = 0; i < 256; i++)
  64.     {
  65.         count[i] = 0;
  66.     }
  67.     while (!feof(fin))
  68.     {
  69.         fscanf(fin, "%s", s);
  70.         if (!chek(s, words, num, count))
  71.         {
  72.             words[num] = (char *)malloc(sizeof(char) * 256);
  73.             strcpy(words[num],s);
  74.             count[num]++;
  75.             num++;
  76.         }
  77.     }
  78.     count[num - 1]--;
  79.     q_sort(0, num - 1, count, words);
  80.     if (num < n)
  81.     {
  82.         printf("Количество разных слов в файле меньше n\n");
  83.     }
  84.     else
  85.     {
  86.         for (int i = num-1; i >= num-n; i--)
  87.         {
  88.             printf("%s = %d\n", words[i], count[i]);
  89.  
  90.         }
  91.     }
  92.     free(s);
  93.     free(words);
  94.     fclose(fin);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement