/****************************************************************************** * This program reads characters from STDIN and then computes a frequency table. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #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 totalCount = 0; unordered_map countTable = {}; int c; while ((c = getwchar()) != EOF) { if (!countTable.count(c)) { countTable[c] = 1; } else { ++countTable[c]; } ++totalCount; } wcout << L"Hex\tChar\tCount\tFreq" << endl; const vector sortedKeys = Utils::sort(Utils::keys(countTable)); for (int x : sortedKeys) { double const freq = countTable.at(x) / double(totalCount); if (iswcntrl(x)) { wcout << fmt::format(L"{0:05x}\t0x{0:x}\t{1:d}\t{2:.4f}", x, countTable.at(x), freq) << endl; } else { wcout << fmt::format(L"{0:05x}\t{1:c}\t{2:d}\t{3:.4f}", x, wchar_t(x), countTable.at(x), freq) << endl; } } return 0; }