|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <cctype> |
| 4 | +#include <cassert> |
| 5 | +#include <unordered_map> |
| 6 | +#include <vector> |
| 7 | +#include <algorithm> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +constexpr auto INPUT_ARGS_COUNT = 3; |
| 12 | + |
| 13 | +using WordToCountDictionary = unordered_map<string, unsigned int>; |
| 14 | +using CountToWordsDictionary = unordered_map<unsigned int, vector<string>>; |
| 15 | +using SortedCountToWordsDictionary = vector<pair<unsigned int, vector<string>*>>; |
| 16 | + |
| 17 | +WordToCountDictionary ReadWords(ifstream & inputStream) |
| 18 | +{ |
| 19 | + WordToCountDictionary wordToCountDictionary; |
| 20 | + |
| 21 | + string wordBuf; |
| 22 | + using buf_iter = std::istreambuf_iterator<char>; |
| 23 | + const buf_iter eof; |
| 24 | + for (buf_iter it = inputStream; it != eof; ++it) |
| 25 | + { |
| 26 | + const auto symbol = tolower(static_cast<unsigned char>(*it)); |
| 27 | + if (isalpha(symbol)) |
| 28 | + { |
| 29 | + wordBuf += symbol; |
| 30 | + } |
| 31 | + else if (!wordBuf.empty()) |
| 32 | + { |
| 33 | + ++wordToCountDictionary[wordBuf]; |
| 34 | + wordBuf.clear(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return wordToCountDictionary; |
| 39 | +} |
| 40 | + |
| 41 | +CountToWordsDictionary GetCountToWordsDictionary(const WordToCountDictionary & wordToCountDictionary) |
| 42 | +{ |
| 43 | + CountToWordsDictionary countToWordsDictionary; |
| 44 | + for (const auto & [word, count] : wordToCountDictionary) |
| 45 | + { |
| 46 | + countToWordsDictionary[count].push_back(word); |
| 47 | + } |
| 48 | + |
| 49 | + return countToWordsDictionary; |
| 50 | +} |
| 51 | + |
| 52 | +SortedCountToWordsDictionary SortWordsByCount(CountToWordsDictionary & countToWordsDictionary) |
| 53 | +{ |
| 54 | + SortedCountToWordsDictionary sortedCountToWordsDictionary; |
| 55 | + sortedCountToWordsDictionary.reserve(countToWordsDictionary.size()); |
| 56 | + |
| 57 | + for (auto & [count, words] : countToWordsDictionary) |
| 58 | + { |
| 59 | + sort(begin(words), end(words)); |
| 60 | + sortedCountToWordsDictionary.push_back({ count, &words }); |
| 61 | + } |
| 62 | + |
| 63 | + sort(begin(sortedCountToWordsDictionary), end(sortedCountToWordsDictionary), |
| 64 | + [] (const auto & left, const auto & right) |
| 65 | + { |
| 66 | + return left.first > right.first; |
| 67 | + }); |
| 68 | + |
| 69 | + return sortedCountToWordsDictionary; |
| 70 | +} |
| 71 | + |
| 72 | +void PrintResults(ofstream & outputStream, const SortedCountToWordsDictionary & sortedCountToWords) |
| 73 | +{ |
| 74 | + for (const auto & [count, pWords] : sortedCountToWords) |
| 75 | + { |
| 76 | + assert(pWords != nullptr); |
| 77 | + for (const auto & word : *pWords) |
| 78 | + { |
| 79 | + outputStream << count << " " << word << endl; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +int main(int _argc, char * _argv[]) |
| 85 | +{ |
| 86 | + if (_argc != INPUT_ARGS_COUNT) |
| 87 | + { |
| 88 | + cerr << "Usage: " << _argv[0] << " <inputfile> <outputfile>" << endl; |
| 89 | + return 1; |
| 90 | + } |
| 91 | + |
| 92 | + const string inputFileName(_argv[1]); |
| 93 | + ifstream inputStream{ inputFileName }; |
| 94 | + if (!inputStream) |
| 95 | + { |
| 96 | + cerr << "Cannot open input file: " << inputFileName << endl; |
| 97 | + return 2; |
| 98 | + } |
| 99 | + |
| 100 | + const string outputFileName(_argv[2]); |
| 101 | + ofstream outputStream{ outputFileName }; |
| 102 | + if (!outputStream) |
| 103 | + { |
| 104 | + cerr << "Cannot create output file: " << outputFileName << endl; |
| 105 | + return 3; |
| 106 | + } |
| 107 | + |
| 108 | + const auto wordToCountDictionary = ReadWords(inputStream); |
| 109 | + auto countToWordsDictionary = GetCountToWordsDictionary(wordToCountDictionary); |
| 110 | + const auto sortedCountToWordsDictionary = SortWordsByCount(countToWordsDictionary); |
| 111 | + |
| 112 | + PrintResults(outputStream, sortedCountToWordsDictionary); |
| 113 | + |
| 114 | + inputStream.close(); |
| 115 | + outputStream.close(); |
| 116 | + |
| 117 | + cout << "Results were successfully written to file: \"" << outputFileName << "\"" << endl; |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments