-
Notifications
You must be signed in to change notification settings - Fork 0
/
hz.cpp
51 lines (44 loc) · 1.35 KB
/
hz.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <map>
#include <fstream>
#include <iostream>
static std::map<int, size_t> parse(std::ifstream &stream);
static void report(const std::map<int, size_t> &symbols);
int main(int argc, char **argv) {
std::ifstream stream;
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " FILE" << std::endl;
return 1;
}
stream.open(argv[1], std::ifstream::in);
if (stream) {
report(parse(stream));
stream.close();
return 0;
}
std::cerr << "ERROR: unable to open file: '" << argv[1] << "'" << std::endl;
return 2;
}
std::map<int, size_t> parse(std::ifstream &stream) {
std::map<int, size_t> symbols;
for (int c = stream.get(); EOF != c; c = stream.get()) {
auto it = symbols.find(c);
if (it == symbols.end()) {
symbols[c] = 1;
} else {
it->second++;
}
}
return symbols;
}
void report(const std::map<int, size_t> &symbols) {
size_t i = 1;
for (const auto &pair : symbols) {
const auto ch = pair.first;
std::cout << "No: " << i++
<< "\tASCII: " << ch
<< "\tCh: " << (isprint(ch) ? static_cast<char>(ch) : ' ')
<< "\tHz: " << pair.second
<< std::endl;
}
std::cout << std::endl << "Total symbols: " << symbols.size() << std::endl;
}