Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "stdafx.h"
- #include "iostream"
- #include "fstream"
- #include "windows.h"
- #include "ctime"
- #include "conio.h"
- #include "iomanip"
- #include "map"
- #include "algorithm"
- #include "string"
- using std::cout;
- using std::cin;
- using std::endl;
- using std::getline;
- using std::string;
- using std::map;
- bool checkword(map <string, int> myMap, string &word)
- {
- std::transform(word.begin(), word.end(), word.begin(), ::tolower);//переводим всё слово в нижний регистр
- for (int i = word.size() - 1; !(isalpha((unsigned char)word[i])); --i)//избавляемся от знаков препинания
- {
- if(!(isalpha((unsigned char)word[i])))
- word.erase(word.begin() + i);
- }
- if (myMap.find(word) != myMap.end())//если нашли слово, то возвращаем true
- return true;
- else
- return false;
- }
- void readingFROMfile(map <string, int> &myMap)
- {
- std::ifstream file("file.txt");
- string word;
- while (file >> word)
- {
- if (word.size() != 0 && isalpha((unsigned char)word[0]))
- {
- if (checkword(myMap, word))//если слово есть в map
- {
- myMap[word]++;//добавляем количество повторов
- }
- else
- {
- myMap.insert(std::pair<string, int>(word, 1));//вставляем элемент в map
- }
- }
- }
- }
- int main()
- {
- setlocale(NULL, "ru");
- srand(time(NULL));
- map <string, int> myMap;
- readingFROMfile(myMap);
- for (auto it = myMap.begin(); it != myMap.end(); ++it)
- {
- cout << it->first << " : " << it->second << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement