/****************************************************************************** * This program reads characters from STDIN and then computes a frequency table. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include "Utils.hpp" #include #include #include #include #include #include #include #include #include std::locale utf8loc(std::locale(), new std::codecvt_utf8); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); int total_count = 0; unordered_map count_table = {}; int c; while ((c = getwchar()) != EOF) { if (!count_table.count(c)) { count_table[c] = 1; } else { ++count_table[c]; } ++total_count; } wcout << L"Hex\tChar\tCount\tFreq" << endl; const vector sorted_keys = Utils::sort(Utils::keys(count_table)); for (int x : sorted_keys) { double const FREQ = count_table.at(x) / double(total_count); if (iswcntrl(x)) { wcout << fmt::format(L"{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, count_table.at(x), FREQ) << endl; } else { wcout << fmt::format(L"{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, wchar_t(x), count_table.at(x), FREQ) << endl; } } return 0; }