#include #include #include #include #include #include "utf8/utf8.h" using namespace std; typedef int UnicodeChar; bool comparePairs( const pair& i0, const pair& i1 ); int main() { map russianLetters; int n = 0; while (true) { bool readFail; UnicodeChar c = get_utf8_code_point(cin, readFail); if (readFail) { if (!cin.good()) break; } if (isRussianLetter(c)) { ++n; c = toUpperLetter(c); if (russianLetters.count(c) == 0) { // Add a letter to the alphabet russianLetters[c] = 1; } else { // Increment the number of such letters in a text ++(russianLetters[c]); } } } // end while // Fill the array of pairs (char, number of inclusions in text) vector< pair > charFreq; for (auto i = russianLetters.cbegin(); i != russianLetters.cend(); ++i) { charFreq.push_back(*i); } cout << "Number of different Russian letters in a text: " << charFreq.size() << endl; sort(charFreq.begin(), charFreq.end(), comparePairs); int letterOrder = 0; for (auto i = charFreq.cbegin(); i != charFreq.cend(); ++i) { if (letterOrder > 0) { // Print a delimeter if (letterOrder%5 == 0) cout << endl; else cout << " "; } output_utf8(cout, i->first); cout << " "; double freq = double(i->second); if (n > 0) { freq /= double(n); } cout << fixed << setprecision(4) << freq; ++letterOrder; } cout << endl; return 0; } bool comparePairs( const pair& i0, const pair& i1 ) { return ( i0.second > i1.second || (i0.second == i1.second && compareRussianLetters(i0.first, i1.first) < 0) ); }