Created
May 28, 2020 19:35
-
-
Save DieTime/503118e3cab147f0249b8147fb74c8c7 to your computer and use it in GitHub Desktop.
Finding root words in file
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
/* Отключение предупреждений Visual Studio при | |
использовании "небезопасных" функций (strtok) */ | |
#pragma warning(disable : 4996) | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <locale> | |
#include <windows.h> | |
using namespace std; | |
// Расположение файла | |
const char* filepath = "text.txt"; | |
void toLower(char* str) { | |
// Пробегаем по всем символам и переводим в нижний регистр | |
for (int i = 0; str[i] != '\0'; i++) { | |
str[i] = tolower(str[i], locale("rus_rus.1251")); | |
} | |
} | |
int main() { | |
// Включение поддержки русского | |
SetConsoleCP(1251); | |
SetConsoleOutputCP(1251); | |
ifstream fin(filepath); // Открытие потока для чтения файла | |
char buffer[256]; // Буфер для чтения строк | |
char root[32]; // Корень | |
// Проверка на открытие файла | |
if (!fin.is_open()) { | |
cerr << "Ошибка! Неудачное открытие файла." << endl; | |
return 1; | |
} | |
// Считывание корня | |
cout << "Введите корень для поиска: "; | |
cin.getline(root, 32, '\n'); | |
// Перевод корня в нижний регистр | |
toLower(root); | |
// Вывод начальной фразы | |
cout << "\nОднокоренные слова: [ "; | |
// Процесс считывания всех слов | |
while (!fin.eof()) { | |
// Читаем строку из файла в буффер | |
fin.getline(buffer, 256); | |
// Пока есть слово в строке - проверяем его на корень | |
char* token = strtok(buffer, " ,.;:()!?-"); | |
while (token != NULL) { | |
// Переводим слово в нижний регистр | |
toLower(token); | |
// Если слово содержит корень - выводим слово | |
if (strstr(token, root) != NULL) { | |
cout << token << ", "; | |
} | |
// Получаем новое слово | |
token = strtok(NULL, " ,.;:()!?-"); | |
} | |
} | |
// Завершение вывода с удалением последней запятой | |
cout << "\b\b ]" << endl; | |
// Закрытие потока | |
fin.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment