Advertisement
Guest User

Untitled

a guest
Sep 30th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. //#include <windows.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <sys/time.h>
  9.  
  10. typedef unsigned long DWORD;
  11.  
  12. bool cont = true;
  13.  
  14. void
  15. stop( int sig )
  16. {
  17.     cont = false;
  18. }
  19.  
  20. void
  21. Sleep( int msec )
  22. {
  23.     usleep( msec * 1000 );
  24. }
  25.  
  26. DWORD
  27. GetTickCount()
  28. {
  29.     static unsigned long base = 0;
  30.     struct timeval tv;
  31.     gettimeofday( &tv, 0 );
  32.     if ( base == 0 )
  33.         base = tv.tv_sec;
  34.     return (tv.tv_sec - base) * 1000 + tv.tv_usec / 1000;
  35. }
  36.  
  37. int main( int argc, char *argv[] )
  38. {
  39.     int delay = 250;
  40.     std::string word;
  41.  
  42.     if ( argc == 2 )
  43.         delay = atoi(argv[1]);
  44.  
  45.     signal( SIGINT, stop );
  46.  
  47.     std::cout << "\n\n\n";
  48.  
  49.     int words = 0;
  50.     DWORD start = GetTickCount();
  51.     std::string display_word = "";
  52.     while ( std::cin >> word && cont )
  53.     {
  54.         Sleep(delay);
  55.         ++words;
  56.         if ( display_word.length() )
  57.             display_word += ' ';
  58.         display_word += word;
  59.         //if ( display_word.length() < 5 )
  60.             //continue;
  61.         word = display_word;
  62.         display_word = "";
  63.         int punc = word.find(".");
  64.         if ( punc == std::string::npos )
  65.             punc = word.find(",");
  66.         int pad = 15 - word.length()/2;
  67.         for ( int i = 0; i < pad; ++i )
  68.             std::cout << " ";
  69.         std::cout << " " << word << "                                  \r";
  70.         std::cout.flush();
  71.         if ( punc != std::string::npos )
  72.             Sleep(2*delay);
  73.     }
  74.     DWORD stop = GetTickCount();
  75.     float seconds = (stop - start) / 1000.0;
  76.     printf( "\n%d words in %f seconds\n", words, seconds );
  77.     if ( seconds != 0 )
  78.         printf( "%f words/second, %f words/minute\n", words/seconds,
  79.                 (60*words)/seconds );
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement