]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - util/charstat.cpp
Agrego utilidad wordstat.
[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< unsigned 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                 }
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() << ":" << unsigned(p.first);
35                 if (isgraph(p.first)) os << "]";
36                 os << " (" << double(p.second) / total * 100 << "% : "
37                         << unsigned((double(p.second) / total) * 4294967296ull) << ")\n";
38         }
39 };
40
41 int main(void) 
42 {
43         std::setlocale(LC_ALL, "es_AR");
44         char c = 0;
45         unsigned total = 0;
46         std::map< char, unsigned > freq;
47         while (std::cin.get(c)) {
48                 ++freq[c];
49                 ++total;
50         }
51         for_each(freq.begin(), freq.end(), print(std::cout, total));
52         return 0;
53 }
54