Advertisement
Guest User

GG

a guest
Apr 3rd, 2008
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <ctime>
  5. #include <cstring>
  6.  
  7. static const int num_morons = ('z' - 'a' + 1) * 2;
  8.  
  9. template <typename T> class MoronicArray {  T arr[num_morons];
  10.    
  11. public:
  12.         T &operator[](unsigned char c) {
  13.                 int n;
  14.                 if(c >= 'a' && c <= 'z')
  15.                         n = c - 'a';
  16.                 else if(c >= 'A' && c <= 'Z')
  17.                         n = c- 'A' + num_morons/2;
  18.                 else
  19.                         n = 0;
  20.                 return arr[n];
  21.         }
  22.         MoronicArray() { memset(this, '\0', sizeof(*this)); }
  23. };
  24.  
  25. class Moron {
  26.         MoronicArray<int>       counts;
  27.         MoronicArray<Moron *>   morons;
  28. public:
  29.         Moron *operator[](unsigned char c) {
  30.                 Moron *&m = morons[c];
  31.                 if(m != NULL)
  32.                         return m;
  33.                 return m = new Moron();
  34.         };
  35.         void inc(unsigned char c) {
  36.                 ++counts[c];
  37.         };
  38.         void moronificator(const std::string &s) {
  39.                 for(unsigned char c = 'a'; c <= 'z'; ++c) {
  40.                         if(counts[c] > 0)
  41.                                 std::cout << s << c << "   " << counts[c] << std::endl;
  42.                         if(morons[c])
  43.                                 morons[c]->moronificator(s + std::string(1, c));
  44.                 }
  45.                 for(unsigned char c = 'A'; c <= 'Z'; ++c) {
  46.                         if(counts[c] > 0)
  47.                                 std::cout << s << c << "   " << counts[c] << std::endl;
  48.                         if(morons[c])
  49.                                 morons[c]->moronificator(s + std::string(1, c));
  50.                 }
  51.         } };
  52.  
  53. int main(int argc, char **argv)
  54. {
  55.         int w_total = 0, l_total = 0, c_total = 0;
  56.         Moron m;
  57.  
  58.         printf("   lines   words   bytes file\n" );
  59.         clock_t start=clock();
  60.  
  61.         for(int i = 1; i <argc; ++i) {
  62.                 std::ifstream input_file(argv[i]);
  63.                 std::ostringstream buffer;
  64.                 buffer << input_file.rdbuf();
  65.                 const std::string &s = buffer.str();
  66.                 int l = s.size();
  67.  
  68.                 int w_cnt = 0, l_cnt = 0, c_cnt = 0;
  69.                 Moron *act = &m;
  70.                 unsigned char last = '\0';
  71.                 for(int j = 0; j < l; ++j) {
  72.                         unsigned char c = s[j];
  73.                         if(c == '\n') {
  74.                                 ++l_cnt;
  75.                         }
  76.                         if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
  77.                                 if(last)
  78.                                         act = (*act)[last];
  79.                                 last = c;
  80.                         } else if(last) {
  81.                                 act->inc(last);
  82.                                 act = &m;
  83.                                 last = '\0';
  84.                         } else {
  85.                                 last = '\0';
  86.                         }
  87.                         ++c_cnt;
  88.                 }
  89.  
  90.                 printf("%d\t%d\t%d\t %s\n", l_cnt, w_cnt, c_cnt, argv[i]);
  91.                 l_total += l_cnt;
  92.                 w_total += w_cnt;
  93.                 c_total += c_cnt;
  94.         }
  95.  
  96.         clock_t end=clock();
  97.  
  98.         if(argc > 2) {
  99.                 printf("--------------------------------------\n%d\t%d\t%d\t total",
  100.                         l_total, w_total, c_total);
  101.         }
  102.  
  103.         printf("--------------------------------------\n");
  104.  
  105.         m.moronificator(std::string());
  106.  
  107.         std::cout <<"Time: " << double(end-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
  108. }
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement