35657

Untitled

May 28th, 2024
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <map>
  6. #include <Windows.h>
  7.  
  8.  
  9. int main() {
  10.  
  11.     SetConsoleCP(1251);
  12.     SetConsoleOutputCP(1251);
  13.  
  14.     std::ifstream fin("file.txt");
  15.  
  16.     if (!fin.is_open()) {
  17.         std::cout << "Ошибка открытия файла" << std::endl;
  18.     }
  19.     else {
  20.         std::string str;
  21.  
  22.         std::map<std::string, int> dictionary;
  23.  
  24.         while (!fin.eof()) {
  25.             fin >> str;
  26.  
  27.             while (str.size() > 0 && (str[0] < 'А' || str[0] > 'я')) {
  28.                 str.erase(str.begin());
  29.             }
  30.             while (str.size() > 0 && (str.back() < 'А' || str.back() > 'я')) {
  31.                 str.pop_back();
  32.             }
  33.             if (str.size() > 0) {
  34.                 for (char& ch : str) { // если не нужен перевод в нижний регистр, то можно не делать
  35.                     if (ch >= 'А' && ch <= 'Я') {
  36.                         ch += 32;
  37.                     }
  38.                 }
  39.                 dictionary[str]++;
  40.             }
  41.         }
  42.  
  43.         for (auto a : dictionary) {
  44.             std::cout << a.first << " " << a.second << std::endl;
  45.         }
  46.  
  47.         fin.close();
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment