Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate

Advertising

Anonymous
Monday, March 31st, 2008 at 9:07:50pm UTC 

  1. //Added time...originally by
  2. //Newsgroups: comp.lang.c++.moderated
  3. //From: "Vadim Ferderer" <sp...@ferderer.de>
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <sstream>
  9. #include <cstdio>
  10. #include <map>
  11. #include <ctime>
  12.  
  13. int main( int argc, char* argv[] )
  14. {
  15.   int w_total = 0;
  16.   int l_total = 0;
  17.   int c_total = 0;
  18.   std::map< std::string, int > dictionary;
  19.  
  20.  
  21.   printf("   lines   words   bytes file\n" );
  22.  
  23.   //TIME STARTS HERE
  24.   clock_t start=clock();
  25.  
  26.   for ( int i = 1; i < argc; ++i )
  27.   {
  28.     std::ifstream input_file( argv[i] );
  29.     std::ostringstream buffer;
  30.     buffer << input_file.rdbuf();
  31.     std::string input( buffer.str() );
  32.  
  33.  
  34.     int w_cnt = 0;
  35.     int l_cnt = 0;
  36.     int c_cnt = 0;
  37.     bool inword = false;
  38.     int wstart = 0;
  39.     for ( unsigned int j = 0; j < input.length(); j++ )
  40.     {
  41.       char c = input[j];
  42.       if (c == '\n')
  43.         ++l_cnt;
  44.       if (c >= '0' && c <= '9')
  45.       {
  46.       }
  47.       else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
  48.       {
  49.         if (!inword)
  50.         {
  51.           wstart = j;
  52.           inword = true;
  53.           ++w_cnt;
  54.         }
  55.       }
  56.       else if (inword)
  57.       {
  58.         std::string word = input.substr( wstart, j - wstart );
  59.         std::map< std::string, int >::iterator it = dictionary.find( word );
  60.         if ( it == dictionary.end() )
  61.           dictionary[word] = 1;
  62.         else
  63.           ++it->second;
  64.         inword = false;
  65.       }
  66.       ++c_cnt;
  67.     }
  68.  
  69.  
  70.     if (inword)
  71.     {
  72.       std::string w = input.substr( wstart );
  73.       std::map< std::string, int >::iterator it = dictionary.find( w );
  74.       if ( it == dictionary.end() )
  75.         dictionary[w] = 1;
  76.       else
  77.         ++it->second;
  78.     }
  79.    
  80.  
  81.  
  82.     printf("%d\t%d\t%d\t %s\n", l_cnt, w_cnt, c_cnt, argv[i]);
  83.     l_total += l_cnt;
  84.     w_total += w_cnt;
  85.     c_total += c_cnt;
  86.   }
  87.     //TIME ENDS HERE
  88.     clock_t end=clock();
  89.  
  90.   if (argc > 2)
  91.   {
  92.     printf("--------------------------------------\n%d\t%\d\t%d\t total",
  93. l_total, w_total, c_total);
  94.   }
  95.  
  96.   printf("--------------------------------------\n");
  97.   for( std::map< std::string, int >::const_iterator cit =
  98. dictionary.begin(), cend_it = dictionary.end(); cit != cend_it; ++cit )
  99.     printf( "%d %s\n", cit->second, cit->first.c_str() );
  100.  
  101.      std::cout <<"Time: " <<
  102.           double(end-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
  103.  
  104. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will not expire by default. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right
worth-right