From: Leandro Lucarella Date: Sun, 27 Jun 2004 22:02:55 +0000 (+0000) Subject: Agrego utilidad wordstat. X-Git-Tag: svn_import~34 X-Git-Url: https://git.llucax.com/z.facultad/75.06/jacu.git/commitdiff_plain/1ae7669af401aa21cbd495e2dffa883608c540a6 Agrego utilidad wordstat. --- diff --git a/util/Makefile b/util/Makefile index 46147dc..9dc79cb 100644 --- a/util/Makefile +++ b/util/Makefile @@ -51,6 +51,7 @@ ifeq ($(extension), c) enlazador = $(CC) else enlazador = $(CXX) +CC=$(CXX) endif # Si no especifica archivos, tomo todos. diff --git a/util/charstat.cpp b/util/charstat.cpp index ae96135..ba4631b 100644 --- a/util/charstat.cpp +++ b/util/charstat.cpp @@ -7,7 +7,7 @@ #include #include -typedef std::map< char, unsigned > freq_type; +typedef std::map< unsigned char, unsigned > freq_type; class print: public std::unary_function< freq_type::value_type, void > { diff --git a/util/wordstat.cpp b/util/wordstat.cpp new file mode 100644 index 0000000..24b445d --- /dev/null +++ b/util/wordstat.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef std::map< std::string, unsigned > freq_type; + +class print: public std::unary_function< freq_type::value_type, void > +{ + std::ostream& os; +public: + unsigned total; + unsigned total_chars; + print(std::ostream& out, unsigned total, unsigned total_chars) + : os(out), total(total), total_chars(total_chars) {} + void operator() (const freq_type::value_type& p) + { + os.fill('0'); + os.width(8); + os << p.second * (p.first.length()+2) << ": " << p.first << " (" + << double(p.second) * (p.first.length()+2) / total_chars * 100 + << "% / " << double(p.second) / total * 100 << "%)\n"; + } +}; + +int main(void) +{ + std::setlocale(LC_ALL, "es_AR"); + std::string buf; + unsigned total = 0, total_chars = 0; + freq_type freq; + while (std::cin >> buf) { + ++freq[buf]; + ++total; + total_chars += buf.length(); + } + for_each(freq.begin(), freq.end(), print(std::cout, total, total_chars)); + return 0; +} +