All pastes #965243 Raw Copy code Copy link Edit

Anonymous

public cpp v1 · immutable
#965243 ·published 2008-03-31 21:07 UTC
rendered paste body
//Added time...originally by //Newsgroups: comp.lang.c++.moderated//From: "Vadim Ferderer" <sp...@ferderer.de>#include <iostream> #include <fstream> #include <string> #include <sstream> #include <cstdio> #include <map> #include <ctime>int main( int argc, char* argv[] ) {  int w_total = 0;   int l_total = 0;   int c_total = 0;   std::map< std::string, int > dictionary;   printf("   lines   words   bytes file\n" );   //TIME STARTS HERE  clock_t start=clock();   for ( int i = 1; i < argc; ++i )   {     std::ifstream input_file( argv[i] );     std::ostringstream buffer;     buffer << input_file.rdbuf();     std::string input( buffer.str() );     int w_cnt = 0;     int l_cnt = 0;     int c_cnt = 0;     bool inword = false;     int wstart = 0;     for ( unsigned int j = 0; j < input.length(); j++ )     {       char c = input[j];       if (c == '\n')         ++l_cnt;       if (c >= '0' && c <= '9')       {       }       else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')       {         if (!inword)         {           wstart = j;           inword = true;           ++w_cnt;         }       }       else if (inword)       {         std::string word = input.substr( wstart, j - wstart );         std::map< std::string, int >::iterator it = dictionary.find( word );         if ( it == dictionary.end() )           dictionary[word] = 1;         else           ++it->second;         inword = false;       }       ++c_cnt;     }     if (inword)     {       std::string w = input.substr( wstart );       std::map< std::string, int >::iterator it = dictionary.find( w );       if ( it == dictionary.end() )         dictionary[w] = 1;       else         ++it->second;     }         printf("%d\t%d\t%d\t %s\n", l_cnt, w_cnt, c_cnt, argv[i]);     l_total += l_cnt;     w_total += w_cnt;     c_total += c_cnt;   }     //TIME ENDS HERE    clock_t end=clock();  if (argc > 2)   {     printf("--------------------------------------\n%d\t%\d\t%d\t total", l_total, w_total, c_total);   }   printf("--------------------------------------\n");   for( std::map< std::string, int >::const_iterator cit = dictionary.begin(), cend_it = dictionary.end(); cit != cend_it; ++cit )     printf( "%d %s\n", cit->second, cit->first.c_str() );      std::cout <<"Time: " <<          double(end-start)/CLOCKS_PER_SEC * 1000 << " ms\n";}