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