]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - util/charstat.cpp
Bugfixes
[z.facultad/75.06/jacu.git] / util / charstat.cpp
1 #include <map>
2 #include <iostream>
3 #include <iomanip>
4 #include <algorithm>
5 #include <sstream>
6 #include <cctype>
7 #include <clocale>
8 #include <cstdlib>
9
10 typedef std::map< char, unsigned > freq_type;
11
12 class print: public std::unary_function< freq_type::value_type, void >
13 {
14         std::ostream& os;
15 public:
16         unsigned total;
17         print(std::ostream& out, unsigned total) : os(out), total(total) {}
18         void operator() (const freq_type::value_type& p)
19         {
20                 os.fill('0');
21                 os.width(8);
22                 os << p.second << ": ";
23                 os.width(4);
24                 os.fill(' ');
25                 if (isgraph(p.first)) {
26                         os << p.first;
27                 } else {
28                         std::ostringstream oss;
29                         oss << "0x";
30                         oss.width(2);
31                         oss.fill('0');
32                         oss.setf(std::ios::hex, std::ios::basefield);
33                         oss << int(p.first);
34                         os << oss.str();
35                 }
36                 os << " (" << double(p.second) / total * 100 << "%)\n";
37         }
38 };
39
40 int main(void) 
41 {
42         std::setlocale(LC_ALL, "es_AR");
43         char c = 0;
44         unsigned total = 0;
45         std::map< char, unsigned > freq;
46         while (std::cin.get(c)) {
47                 ++freq[c];
48                 ++total;
49         }
50         for_each(freq.begin(), freq.end(), print(std::cout, total));
51         return 0;
52 }
53