Vla_DOS

Untitled

Sep 5th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <list>
  6. #include <locale>
  7. #include <Windows.h>
  8. #include <iterator>
  9. #include <sstream>
  10.  
  11. #define і i
  12.  
  13. using namespace std;
  14.  
  15. int getRateWordByPattern(const string& word, const string& pattern) // пошук слова в словнику
  16. {
  17.     int rate = 0;
  18.  
  19.     for (int i = 0; i < pattern.size(); ++i)
  20.         rate += std::count(word.begin(), word.end(), pattern[i]);
  21.  
  22.     return rate;
  23. }
  24.  
  25. list<string> GetListInFile(string path) {
  26.     list<string> newListStr;
  27.     ifstream ifs(path);
  28.     list<string> words{ istream_iterator<string>(ifs), istream_iterator<string>() };
  29.  
  30.     for (auto word : words) {
  31.  
  32.         for (int i = 0; i < word.length(); i++)
  33.         {
  34.             if (word[i] == ',' || word[i] == '.' || word[i] == '!' || word[i] == '?')
  35.             {
  36.                 word[i] = ' ';
  37.             }
  38.         }
  39.         newListStr.push_back(word);
  40.     }
  41.     ifs.close();
  42.  
  43.     return newListStr;
  44. }
  45.  
  46. void main()
  47. {
  48.     setlocale(0, "");
  49.     SetConsoleCP(1251);
  50.     SetConsoleOutputCP(1251);
  51.     string pattern = "прй hjuh\nf";
  52.     string path = "dictionary.txt";
  53.     string pathCheck = "text.txt";
  54.     ifstream f;
  55.     f.open(path);
  56.  
  57.     if (!f.is_open())
  58.         return;
  59.  
  60.     int max_rate = 0;
  61.     string res_word = "not found";
  62.     list<string> l;
  63.     string word, w;
  64.  
  65.     while (f >> word)
  66.     {
  67.  
  68.         for (auto w : GetListInFile(pathCheck)) {
  69.             int rate = getRateWordByPattern(word, w);
  70.             if (rate > max_rate)
  71.             {
  72.                 max_rate = rate;
  73.                 res_word = word;
  74.             }
  75.         }
  76.     }
  77.     wcout << max_rate << endl;
  78.     cout << res_word << endl;
  79.     cout << w << endl;  
  80.  
  81.     string s;
  82.     while (getline(f, s))
  83.     {
  84.         //str.str
  85.         //int rate = getRateWordByPattern(word, pattern);
  86.  
  87.         //if (max_rate != rate) {
  88.            l.push_back(s);
  89.         //}
  90.     }
  91.  
  92.     f.close();
  93.  
  94.     for (auto word : GetListInFile(pathCheck)) {
  95.          //cout << word << endl;
  96.     }
  97.  
  98.  
  99.     for (auto item : l)
  100.     {
  101.         //cout << item << "\n";
  102.     }
  103.  
  104.     std::ofstream out("dictionary.txt", std::ios::app);
  105.     if (out.is_open())
  106.     {
  107.         //out << "привіт\nуяви\nпрощавай gh, j" << std::endl;
  108.     }
  109.     out.close();
  110.     //std::string line;
  111.  
  112.     //ifstream in("dictionary.txt"); // окрываем файл для чтения
  113.     //if (in.is_open())
  114.     //{
  115.     //    while (getline(in, line))
  116.     //    {
  117.     //        //std::cout << line << std::endl;
  118.     //    }
  119.     //}
  120.     //string ls;
  121.     //while (in >> line)
  122.     //{
  123.     //    if (line == " ") {
  124.     //        line += "\n";
  125.     //    }
  126.     //    
  127.     //        //l.push_back((line));
  128.     //}
  129.     //for (auto item : l)
  130.     //{
  131.     //    //cout << item << "\n";
  132.     //}
  133.  
  134.     //in.close();  
  135.     string res;
  136.     list<string> lres;
  137.     for (auto item : l)
  138.     {
  139.         res += item + " ";
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment