Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- bool chek(char *s, char **words, int &num, int count[])
- {
- for (int i = 0; i < num; i++)
- {
- if (strcmp(s, words[i]) == 0)
- {
- count[i]++;
- return true;
- }
- }
- return false;
- }
- void q_sort(int b, int e,int arr[],char **words)
- {
- int l = b, r = e;
- int piv = arr[(l + r) / 2];
- while (l <= r)
- {
- while (arr[l] < piv)
- l++;
- while (arr[r] > piv)
- r--;
- if (l <= r)
- {
- int c = arr[l];
- arr[l] = arr[r];
- arr[r] = c;
- char *s = words[l];
- words[l] = words[r];
- words[r] = s;
- l++;
- r--;
- }
- }
- if (b < r)
- q_sort(b, r, arr, words);
- if (e > l)
- q_sort(l, e, arr, words);
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- FILE *fin = fopen("input.txt", "r");
- char *s = (char *)malloc(sizeof(char) * 256);
- char **words = (char **)malloc(sizeof(char **) * 256);
- int num = 0, count[256];
- int n;
- printf("Введите n\n");
- scanf_s("%d", &n);
- for (int i = 0; i < 256; i++)
- {
- count[i] = 0;
- }
- while (!feof(fin))
- {
- fscanf(fin, "%s", s);
- if (!chek(s, words, num, count))
- {
- words[num] = (char *)malloc(sizeof(char) * 256);
- strcpy(words[num],s);
- count[num]++;
- num++;
- }
- }
- count[num - 1]--;
- q_sort(0, num - 1, count, words);
- if (num < n)
- {
- printf("Количество разных слов в файле меньше n\n");
- }
- else
- {
- for (int i = num-1; i >= num-n; i--)
- {
- printf("%s = %d\n", words[i], count[i]);
- }
- }
- free(s);
- free(words);
- fclose(fin);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement