Advertisement
Derga

Untitled

May 9th, 2023 (edited)
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 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. int cnt = 0; //only for selftest
  18.  
  19. string GetText(int words_count, int sentences_count)
  20. {
  21.     string text;
  22.     const int min = (int)('a');
  23.     const int max = (int)('z');
  24.     const vector<char> end_of_sentences_symbols = { '.', '!', '?' };
  25.  
  26.     for (int a = 0; a < sentences_count; a++)
  27.     {
  28.         bool is_first_word_in_sentence = true;
  29.         for (int b = 0; b < words_count; b++)
  30.         {
  31.             int word_size = 1 + rand() % MAX_WORD_SIZE;
  32.             if (word_size == 3) {
  33.                 cnt++;
  34.             }
  35.             string word;
  36.             for (int i = 0; i < word_size; i++)
  37.             {
  38.                 char letter = min + rand() % (max - min + 1);
  39.                 if (is_first_word_in_sentence)
  40.                 {
  41.                     is_first_word_in_sentence = false;
  42.                     letter -= ('a' - 'A');
  43.                 }
  44.                 word += letter;
  45.             }
  46.  
  47.             if (!text.empty())
  48.             {
  49.                 word = " " + word;
  50.             }
  51.             text += word;
  52.         }
  53.         int idx = rand() % end_of_sentences_symbols.size();
  54.         text += end_of_sentences_symbols[idx];
  55.     }
  56.  
  57.     return text;
  58. }
  59.  
  60. bool test(int count_of_three_letter_words) {
  61.     return count_of_three_letter_words == cnt;
  62. }
  63.  
  64. int main()
  65. {
  66.     srand(time(0));
  67.  
  68.     int words_count;
  69.     int sentences_count;
  70.     cin >> words_count >> sentences_count;
  71.  
  72.     string str = GetText(words_count, sentences_count);
  73.  
  74.     int count_of_three_letter_words = 0;
  75.     int prev = 0;
  76.     int pos = 0;
  77.  
  78.     while ((pos = str.find_first_of(" ", prev)) != string::npos)
  79.     {
  80.         if (pos - prev == 3 && (str[pos - 1] != '.' && str[pos - 1] != '!' && str[pos - 1] != '?'))
  81.         {
  82.             count_of_three_letter_words++;
  83.         }
  84.         else if (pos - prev == 4 && (str[pos - 1] == '.' || str[pos - 1] == '!' || str[pos - 1] == '?'))
  85.         {
  86.             count_of_three_letter_words++;
  87.         }
  88.         prev = pos + 1;
  89.     }
  90.  
  91.     //Так как мы сами генерируем текст, и делаем это так, что предложение всегда завершается символом ./!/?
  92.     //- можно не делать проверку наличия символа завершения предложения
  93.     if (prev < str.length() && str.size() - prev == 4)
  94.     {
  95.         count_of_three_letter_words++;
  96.     }
  97.  
  98.     if (!test(count_of_three_letter_words)) {
  99.         cout << "Something broke\n" << str;
  100.         return 0;
  101.     }
  102.  
  103.     //cout << str << '\n';
  104.     cout << count_of_three_letter_words << endl;
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement