]> git.llucax.com Git - z.facultad/75.06/jacu.git/commitdiff
Agrego utilidad wordstat.
authorLeandro Lucarella <llucax@gmail.com>
Sun, 27 Jun 2004 22:02:55 +0000 (22:02 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Sun, 27 Jun 2004 22:02:55 +0000 (22:02 +0000)
util/Makefile
util/charstat.cpp
util/wordstat.cpp [new file with mode: 0644]

index 46147dcda9251018d062ed5f3bfe65671eeda006..9dc79cbc2659613c25cca53310aa6a07f82f2eca 100644 (file)
@@ -51,6 +51,7 @@ ifeq ($(extension), c)
 enlazador = $(CC)
 else
 enlazador = $(CXX)
+CC=$(CXX)
 endif
 
 # Si no especifica archivos, tomo todos.
index ae961359f43f611a7a022d53256c10a88ac71ac3..ba4631b8ae001eecfacef9fef4c1d3203f8425cd 100644 (file)
@@ -7,7 +7,7 @@
 #include <clocale>
 #include <cstdlib>
 
-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 (file)
index 0000000..24b445d
--- /dev/null
@@ -0,0 +1,45 @@
+#include <map>
+#include <iostream>
+#include <string>
+#include <iomanip>
+#include <algorithm>
+#include <sstream>
+#include <cctype>
+#include <clocale>
+#include <cstdlib>
+
+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;
+}
+