Derga

Untitled

May 9th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. /******************************************************************************
  2. В задачах предполагается, что текст разбит на слова. Под словом понимается группа символов, не содержащая пробелов и отделенная пробелами (одним или несколькими) от других слов.
  3. Синтаксическую правильность написания слов проверять не надо.
  4. Предложение (если есть такая задача) может заканчиваться: точкой (.), !, ?
  5. Введите строку. Добавьте перед последним символом строки три вопросительных знака.
  6. Написать программу, которая вводит текст, состоящий из нескольких предложений, и определяет, сколько в нем слов, состоящих из трех букв
  7. *******************************************************************************/
  8.  
  9. #include <ctime>
  10. #include <iostream>
  11. #include <string>
  12. #include <vector>
  13.  
  14. using namespace std;
  15.  
  16. const int MAX_WORD_SIZE = 10;
  17.  
  18. string GetText(int words_count, int sentences_count)
  19. {
  20.     string text;
  21.     const int min = (int)('a');
  22.     const int max = (int)('z');
  23.     const vector<char> end_of_sentences_symbols = { '.', '!', '?' };
  24.  
  25.     for (int a = 0; a < sentences_count; a++)
  26.     {
  27.         bool is_first_word_in_sentence = true;
  28.         for (int b = 0; b < words_count; b++)
  29.         {
  30.             int word_size = 1 + rand() % MAX_WORD_SIZE;
  31.             string word;
  32.             for (int i = 0; i < word_size; i++)
  33.             {
  34.                 char letter = min + rand() % (max - min + 1);
  35.                 if (is_first_word_in_sentence)
  36.                 {
  37.                     is_first_word_in_sentence = false;
  38.                     letter -= ('a' - 'A');
  39.                 }
  40.                 word += letter;
  41.             }
  42.  
  43.             if (!text.empty())
  44.             {
  45.                 word = " " + word;
  46.             }
  47.             text += word;
  48.         }
  49.         int idx = rand() % end_of_sentences_symbols.size();
  50.         text += end_of_sentences_symbols[idx];
  51.     }
  52.  
  53.     return text;
  54. }
  55.  
  56. int main()
  57. {
  58.     srand(time(0));
  59.    
  60.     int words_count;
  61.     int sentences_count;
  62.     cin >> words_count >> sentences_count;
  63.    
  64.     string str = GetText(words_count, sentences_count);
  65.     int count_of_three_letter_words = 0;
  66.  
  67.     int prev = 0;
  68.     int pos = 0;
  69.    
  70.     while ((pos = str.find_first_of(" ", prev)) != string::npos)
  71.     {
  72.         if (pos - prev == 3)
  73.         {
  74.             count_of_three_letter_words++;
  75.         }
  76.         else if (pos - prev == 4 && (str[pos - 1] == '.' || str[pos - 1] == '!' || str[pos - 1] == '?'))
  77.         {
  78.             count_of_three_letter_words++;
  79.         }
  80.         prev = pos + 1;
  81.     }
  82.  
  83.     //Так как мы сами генерируем текст, и делаем это так, что предложение всегда завершается символом ./!/?
  84.     //- можно не делать проверку наличия символа завершения предложения
  85.     if (prev < str.length() && str.size() - prev == 4)
  86.     {
  87.         count_of_three_letter_words++;
  88.     }
  89.    
  90.     cout << str << '\n' << count_of_three_letter_words << endl;
  91. }
Add Comment
Please, Sign In to add comment