Advertisement
Guest User

20.2

a guest
May 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "stdafx.h"
  3. #include "iostream"
  4. #include "fstream"
  5. #include "windows.h"
  6. #include "ctime"
  7. #include "conio.h"
  8. #include "iomanip"
  9. #include "map"
  10. #include "algorithm"
  11. #include "string"
  12. using std::cout;
  13. using std::cin;
  14. using std::endl;
  15. using std::getline;
  16. using std::string;
  17. using std::map;
  18.  
  19. bool checkword(map <string, int> myMap, string &word)
  20. {
  21.     std::transform(word.begin(), word.end(), word.begin(), ::tolower);//переводим всё слово в нижний регистр
  22.     for (int i = word.size() - 1; !(isalpha((unsigned char)word[i])); --i)//избавляемся от знаков препинания
  23.     {
  24.         if(!(isalpha((unsigned char)word[i])))
  25.             word.erase(word.begin() + i);
  26.     }
  27.     if (myMap.find(word) != myMap.end())//если нашли слово, то возвращаем true
  28.         return true;
  29.     else
  30.         return false;
  31. }
  32.  
  33. void readingFROMfile(map <string, int> &myMap)
  34. {
  35.     std::ifstream file("file.txt");
  36.     string word;
  37.     while (file >> word)
  38.     {
  39.         if (word.size() != 0 && isalpha((unsigned char)word[0]))
  40.         {
  41.             if (checkword(myMap, word))//если слово есть в map
  42.             {
  43.                 myMap[word]++;//добавляем количество повторов
  44.             }
  45.             else
  46.             {
  47.                 myMap.insert(std::pair<string, int>(word, 1));//вставляем элемент в map
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     setlocale(NULL, "ru");
  56.     srand(time(NULL));
  57.     map <string, int> myMap;
  58.     readingFROMfile(myMap);
  59.     for (auto it = myMap.begin(); it != myMap.end(); ++it)
  60.     {
  61.         cout << it->first << " : " << it->second << endl;
  62.     }
  63.     system("pause");
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement