Created
December 21, 2019 06:23
-
-
Save DieTime/d5db979dd9445bb6b9997788236a03aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <locale.h> | |
#include <ctype.h> | |
// Функция сравнения строк | |
int mystrcmp(char* s1, char* s2) { | |
int i = 0; | |
// пока одна из строк не закончится сравниваем символы | |
while (s1[i] != '\0' && s2[i] != '\0') | |
{ | |
// Если нашлось несовпаление, возвращаем 1 | |
if (s1[i] > s2[i]) return 1; | |
if (s1[i] < s2[i]) return -1; | |
i++; | |
} | |
// Если несовпадений нет и длина одинакова выводим 0 | |
if (s1[i] != '\0' && s2[i] == '\0') return 1; | |
if (s1[i] == '\0' && s2[i] != '\0') return -1; | |
// Иначе 1 | |
else return 0; | |
} | |
// Побитовое копирование строк | |
void mystrcpy(char* a, int n, char* b) { | |
int i = 0; | |
for (i = 0; i < n; i++) { | |
a[i] = b[i]; | |
} | |
} | |
int main() { | |
setlocale(LC_ALL, "rus"); | |
int flag = 0; // Флаг для поиска одинаковых слов | |
int j = 0; | |
int i = 0; | |
int count = 0; // Колличество вхождений слова | |
int setIndex = 0; | |
int setSize = 0; | |
int listIndex = 0; | |
int listSize = 0; | |
char** list = (char**)malloc(sizeof(char*) * 256); // Список слов | |
for (i = 0; i < 256; i++) { | |
list[i] = (char*)malloc(sizeof(char) * 32); | |
} | |
char** set = (char**)malloc(sizeof(char*) * 256); // Список слов | |
for (i = 0; i < 256; i++) { | |
set[i] = (char*)malloc(sizeof(char) * 32); | |
} | |
int stringIndex = 0; | |
char* string = (char*)malloc(sizeof(char)*32); // Массив для считывания | |
int ch; // Символ для считывания | |
// Открытие файла | |
FILE* input; | |
fopen_s(&input, "text.txt", "r"); | |
// Чтение всех слов | |
ch = fgetc(input); | |
while (ch != EOF) { | |
while (ch == ' ') { | |
ch = fgetc(input); | |
} | |
while (ch != ' ' && ch != ',' && ch != '.' && ch != '!' && ch != '?' && ch != '\n' && ch != '\0') { | |
string[stringIndex] = tolower(ch); | |
stringIndex++; | |
ch = fgetc(input); | |
} | |
string[stringIndex] = '\0'; | |
if (stringIndex != 0) { | |
mystrcpy(list[listIndex], 32, string); | |
listIndex++; | |
} | |
stringIndex = 0; | |
string[0] = '\0'; | |
ch = fgetc(input); | |
} | |
// Закрытие файла | |
fclose(input); | |
listSize = listIndex; // Запоминаем размер списка | |
// Создание множества | |
for (listIndex = 0; listIndex < listSize; listIndex++) { | |
for (j = 0; j < setIndex; j++) { | |
if (mystrcmp(list[listIndex], set[j]) == 0) { | |
flag = 1; | |
break; | |
} | |
} | |
// Если элемента из списка в множестве еще нет, добавляем его в множество | |
if (flag == 0) { | |
mystrcpy(set[setIndex], 32, list[listIndex]); | |
setIndex++; | |
} | |
else { | |
flag = 0; | |
} | |
} | |
setSize = setIndex; | |
// Сортировка множества | |
for (i = 0; i < setSize - 1; i++) { | |
for (j = i + 1; j < setSize; j++) { | |
if (mystrcmp(set[i], set[j]) > 0) { | |
mystrcpy(string, 32, set[i]); | |
mystrcpy(set[i], 32, set[j]); | |
mystrcpy(set[j], 32, string); | |
} | |
} | |
} | |
// Подсчет вхождений и вывод на экран | |
for (j = 0; j < setSize; j++) { | |
count = 0; | |
for (listIndex = 0; listIndex < listSize; listIndex++) { | |
if (mystrcmp(set[j], list[listIndex]) == 0) { | |
count++; | |
} | |
} | |
printf("%s %d\n", set[j], count); | |
} | |
// Высвобождение выделенной памяти | |
for (i = 0; i < 256; i++) { | |
free(list[i]); | |
free(set[i]); | |
} | |
free(list); | |
free(set); | |
free(string); | |
getch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment