Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. // только стандартная кодировка
  2. void WordsCounter( const std::string name_in, const std::string name_out )
  3. {
  4.     std::map<std::string, ull> words_map;
  5.     ull max_count = std::numeric_limits<ull>::max();
  6.     std::string word = "";
  7.     std::string line;
  8.     std::ifstream input_stream( name_in );
  9.     try
  10.     {
  11.         for( line; std::getline( input_stream, line ); )
  12.         {
  13.             for( const auto& symb: line )
  14.                 if( std::isalpha( static_cast<unsigned char>( symb ) ) )
  15.                     word.push_back( std::tolower( static_cast<unsigned char>( symb ) ) );
  16.                 else
  17.                 {
  18.                     if( word.length() )
  19.                     {
  20.                         if( words_map.find( word ) != words_map.end() )
  21.                             words_map[word] = words_map[word] == max_count ? -1 : words_map[word] + 1;
  22.                         else
  23.                             words_map[word] = 1;
  24.  
  25.                         word = "";
  26.                     }
  27.                 }
  28.         }
  29.         input_stream.close();
  30.     }
  31.     catch( std::exception const& e )
  32.     {
  33.         std::cout << "Err in reading file " << e.what() << endl;
  34.         input_stream.close();
  35.     }
  36.  
  37.     std::vector< std::pair<std::string, ull> > res_word_list( words_map.begin(), words_map.end() );
  38.     std::sort( res_word_list.begin(), res_word_list.end(),
  39.     [](const pair<std::string, ull>  &p1, const pair<std::string, ull> &p2)
  40.     {
  41.         return p1.second > p2.second || p1.second == -1;
  42.     });
  43.     std::ofstream output_stream( name_out );
  44.     try
  45.     {
  46.         for( const auto& word_info: res_word_list )
  47.         {
  48.                 if( word_info.second == -1 )
  49.                     output_stream << "Очень много";
  50.                 else
  51.                     output_stream << word_info.second;
  52.                 output_stream << " "<< word_info.first;
  53.                 output_stream << std::endl;
  54.         }
  55.         output_stream.close();
  56.     }
  57.     catch( std::exception const& e )
  58.     {
  59.         std::cout << "Err in writing file " << e.what() << endl;
  60.         output_stream.close();
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement