Загрузка данных


#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;

string cleanWord(string s) {
    string r = "";
    for (int i = 0; i < s.length(); i++) {
        char c = s[i];
        if ((c >= 'а' && c <= 'я') || (c >= 'А' && c <= 'Я')) {
            if (c >= 'А' && c <= 'Я') {
                r += c + 32;
            } else {
                r += c;
            }
        }
    }
    return r;
}

int main() {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    setlocale(LC_ALL, "rus");
    
    // СОЗДАЁМ ФАЙЛ САМИ (чтобы точно знать содержимое)
    ofstream createFile("24_3ada4a.txt");
    createFile << "универ пара олег. универ комп консоль." << endl;
    createFile.close();
    
    cout << "Файл создан!" << endl;
    
    ifstream file("24_3ada4a.txt");
    
    string text;
    string line;
    while (getline(file, line)) {
        text += line + " ";
    }
    file.close();
    
    cout << "Текст: " << text << endl << endl;
    
    // Разбиваем на предложения
    vector<string> predl;
    string temp = "";
    
    for (char c : text) {
        temp += c;
        if (c == '.') {
            if (temp.length() > 1) {
                predl.push_back(temp);
                temp = "";
            }
        }
    }
    
    cout << "Предложений: " << predl.size() << endl;
    for (int i = 0; i < predl.size(); i++) {
        cout << i+1 << ": " << predl[i] << endl;
    }
    
    if (predl.size() < 2) {
        cout << "Нужно хотя бы 2 предложения!" << endl;
        return 1;
    }
    
    // Слова из первого предложения
    vector<string> firstWords;
    stringstream ss(predl[0]);
    string word;
    
    while (ss >> word) {
        string clean = cleanWord(word);
        if (clean != "") {
            bool exists = false;
            for (string w : firstWords) {
                if (w == clean) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                firstWords.push_back(clean);
                cout << "Добавлено слово из 1-го предл: " << clean << endl;
            }
        }
    }
    
    // Ищем общие слова
    vector<string> commonWords;
    
    for (string w : firstWords) {
        bool inAll = true;
        cout << "\nПроверяем слово: " << w << endl;
        
        for (int j = 1; j < predl.size(); j++) {
            cout << "  Проверка предложения " << j+1 << ": ";
            stringstream ss2(predl[j]);
            string word2;
            bool found = false;
            
            while (ss2 >> word2) {
                string clean2 = cleanWord(word2);
                cout << clean2 << " ";
                if (clean2 == w) {
                    found = true;
                    cout << " -> НАЙДЕНО!" << endl;
                    break;
                }
            }
            if (!found) {
                cout << " -> НЕ НАЙДЕНО!" << endl;
                inAll = false;
                break;
            }
        }
        
        if (inAll) {
            commonWords.push_back(w);
            cout << "Слово '" << w << "' есть во всех предложениях!" << endl;
        }
    }
    
    // Результат
    cout << "\n========================================" << endl;
    if (commonWords.empty()) {
        cout << "Нет слова, которое встречается в каждом предложении!" << endl;
    } else {
        cout << "Слова, встречающиеся в каждом предложении:" << endl;
        for (string w : commonWords) {
            cout << "  -> " << w << endl;
        }
    }
    
    return 0;
}