Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <locale.h>
- #define SIZE 40
- #define STANDART_LENGHT_LINE 255
- int countRecurringCharacters(char *line);
- int funcmp(const void *x1, const void* x2);
- struct list {
- int numberLine;
- int repeatCount;
- };
- int main(void) {
- struct list list[40];
- FILE *file;
- char *fileName = malloc(20 * sizeof(char));
- char *lineOperating = malloc(255 * sizeof(char));
- register int i = 0;
- register int count = 0;
- printf("Введите имя файла: ");
- scanf("%20s", fileName);
- if ((file = fopen(fileName, "r")) == NULL) {
- printf("Возникла ошибка при открытие файла!\n");
- exit(1);
- }
- while (fscanf(file, "%254s", lineOperating) != EOF) {
- list[count].numberLine = count + 1;
- list[count].repeatCount = countRecurringCharacters(lineOperating);
- count++;
- }
- printf("Исходная структура :\n");
- for (i = 0; i < count; i++) {
- printf("%d) %d\n", list[i].numberLine, list[i].repeatCount);
- }
- printf("\n");
- qsort(list, count, sizeof(struct list), funcmp);
- printf("Отсортированная структура :\n");
- for (i = 0; i < count; i++) {
- printf("%d) %d\n", list[i].numberLine, list[i].repeatCount);
- }
- fclose(file);
- return 0;
- }
- int countRecurringCharacters(char *line) {
- int count = 0;
- char countRepeat[256];
- register int i;
- for(int i = 0; i < 256; i++) {
- countRepeat[i] = 0;
- }
- while (line) {
- countRepeat[*line]++;
- line++;
- }
- for (i = 0; i < 256; i++) {
- if (countRepeat[i] > 1) {
- count++;
- }
- }
- return count;
- }
- int funcmp(const void *x1, const void* x2) {
- struct list *s1 = (struct list*)x1;
- struct list *s2 = (struct list*)x2;
- return s1->repeatCount - s2->repeatCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment