Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <unordered_map>
- #include <cctype>
- // Функция для добавления или обновления элемента в хеш-таблице
- void addToHashTable(std::unordered_map<char, int>& hashTable, char key) {
- if (isalpha(key)) { // Проверяем, что символ является буквой
- key = tolower(key); // Приводим к нижнему регистру
- hashTable[key]++;
- }
- }
- // Функция для удаления элемента из хеш-таблицы
- void removeFromHashTable(std::unordered_map<char, int>& hashTable, char key) {
- if (isalpha(key)) {
- key = tolower(key);
- hashTable.erase(key);
- }
- }
- // Функция для поиска элемента в хеш-таблице
- int searchInHashTable(const std::unordered_map<char, int>& hashTable, char key) {
- if (isalpha(key)) {
- key = tolower(key);
- auto it = hashTable.find(key);
- if (it != hashTable.end()) {
- return it->second;
- }
- }
- return 0; // Если элемент не найден
- }
- int main() {
- std::unordered_map<char, int> hashTable;
- std::ifstream inputFile("input.txt");
- std::ofstream outputFile("output.txt");
- if (!inputFile.is_open()) {
- std::cerr << "Ошибка открытия файла input.txt" << std::endl;
- return 1;
- }
- if (!outputFile.is_open()) {
- std::cerr << "Ошибка открытия файла output.txt" << std::endl;
- return 1;
- }
- // Считываем текст из файла и заполняем хеш-таблицу
- char ch;
- while (inputFile.get(ch)) {
- addToHashTable(hashTable, ch);
- }
- // Выводим хеш-таблицу в файл output.txt
- for (const auto& pair : hashTable) {
- outputFile << pair.first << ": " << pair.second << std::endl;
- }
- // Поиск буквы в хеш-таблице
- char searchChar;
- std::cout << "Введите букву для поиска: ";
- std::cin >> searchChar;
- int count = searchInHashTable(hashTable, searchChar);
- if (count > 0) {
- std::cout << "Буква '" << searchChar << "' встречается " << count << " раз(а)." << std::endl;
- } else {
- std::cout << "Буква '" << searchChar << "' не найдена в тексте." << std::endl;
- }
- inputFile.close();
- outputFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement