Advertisement
MadCortez

2_20

Sep 15th, 2021
1,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <locale.h>
  5. #include <string.h>
  6. #include <string>
  7.  
  8. std::string inputString() {
  9.     std::string s;
  10.     char inputc[255];
  11.     printf("%s", "Введите строку: ");
  12.     gets_s(inputc);
  13.     for (int i = 0; i < strlen(inputc); i++)
  14.         s += inputc[i];
  15.     return s;
  16. }
  17.  
  18. std::string correctingString(std::string s) {
  19.     std::string outString;
  20.     for (int i = 0; i < s.size(); i++)
  21.         if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == ' ')
  22.             outString += s[i];
  23.     outString += ' ';
  24.     int i = 0;
  25.     while (i < outString.size()) {
  26.         while (outString[i] == ' ' && outString[i + 1] == ' ')
  27.             outString.erase(i, 1);
  28.         i++;
  29.     }
  30.     while (outString[0] == ' ')
  31.         outString.erase(0, 1);
  32.     return outString;
  33. }
  34.  
  35. void printTask() {
  36.     printf("%s", "1) - Напечатать все слова, отличные от последнего слова, в которые входят те же  согласные буквы, что и в первое слово.\n");
  37.     printf("%s", "2) - Напечатать все слова, отличные от последнего слова, удаляя все гласные буквы, которые уже встречались раньше.\n");
  38. }
  39.  
  40. void printNewString(std::string s) {
  41.     printf("%s", "Введённая строка: ");
  42.     for (int i = 0; i < s.size() - 1; i++)
  43.         printf("%c", s[i]);
  44.     printf("%c", '\n');
  45. }
  46.  
  47. std::string *findWords(std::string s) {
  48.     int kol = 0;
  49.     std::string* words = new std::string[100];
  50.     while (s != "") {
  51.         if (s[0] == ' ')
  52.             kol++;
  53.         else
  54.             words[kol] += s[0];
  55.         s.erase(0, 1);
  56.     }
  57.     return words;
  58. }
  59.  
  60. int getNumOfWords(std::string* words) {
  61.     int i = 0;
  62.     while (words[i] != "")
  63.         i++;
  64.     return i;
  65. }
  66.  
  67. void printWords(std::string* words, int n) {
  68.     printf("%s", "Найденные слова: \n");
  69.     for (int i = 0; i < n; i++) {
  70.         for (int j = 0; j < words[i].size(); j++)
  71.             printf("%c", words[i][j]);
  72.         printf("%c", '\n');
  73.     }
  74. }
  75.  
  76. std::string createFirstString(std::string* words, int kol) {
  77.     int numLetters, num = 0;
  78.     std::string letters = "";
  79.     std::string consonants = "qwrtpsdfghjklzxcvbnmQWRTPSDFGHJKLZXCVBNM";
  80.     std::string ansString = "";
  81.     int z = 0;
  82.     for (int i = 0; i < words[0].size(); i++)
  83.         for (int j = 0; j < consonants.size() - 20; j++)
  84.             if (words[0][i] == consonants[j] || words[0][i] == consonants[j + 20]) {
  85.                 letters += consonants[j];
  86.                 letters += consonants[j + 20];
  87.                 break;
  88.             }
  89.     for (int i = 0; i < kol; i++)
  90.         if (words[i] != words[kol - 1]) {
  91.             if (i == 3) {
  92.                 z++;
  93.             }
  94.             numLetters = 0;
  95.             num = 0;
  96.             for (int j = 0; j < words[i].size(); j++)
  97.                 for (int jj = 0; jj < consonants.size() - 20; jj++)
  98.                     if (words[i][j] == consonants[jj] || words[i][j] == consonants[jj + 20]) {
  99.                         num++;
  100.                         for (int c = 0; c < letters.size(); c++)
  101.                             if (words[i][j] == letters[c]) {
  102.                                 numLetters++;
  103.                                 break;
  104.                             }
  105.                         break;
  106.                     }
  107.             if (num == numLetters)
  108.                 ansString += words[i] + ' ';
  109.         }
  110.     return ansString;
  111. }
  112.  
  113. std::string createSecondString(std::string* words, int kol) {
  114.     std::string vowels = "eyuioaEYUIOA";
  115.     std::string ansString = "";
  116.     bool usedVowels[6] = { false };
  117.     bool flag;
  118.     for (int i = 0; i < kol; i++)
  119.         if (words[i] != words[kol - 1]) {
  120.             for (int j = 0; j < words[i].size(); j++) {
  121.                 flag = false;
  122.                 for (int c = 0; c < 6; c++)
  123.                     if (words[i][j] == vowels[c] || words[i][j] == vowels[c + 6]) {
  124.                         if (!usedVowels[c]) {
  125.                             ansString += words[i][j];
  126.                             usedVowels[c] = true;
  127.                         }
  128.                         flag = true;
  129.                     }
  130.                 if (!flag)
  131.                     ansString += words[i][j];
  132.             }
  133.             ansString += ' ';
  134.         }
  135.     return ansString;
  136. }
  137.  
  138. void printString1(std::string s) {
  139.     printf("%s", "Первая полученная строка: ");
  140.     for (int i = 0; i < s.size(); i++)
  141.         printf("%c", s[i]);
  142.     printf("%c", '\n');
  143. }
  144.  
  145. void printString2(std::string s) {
  146.     printf("%s", "Вторая полученная строка: ");
  147.     for (int i = 0; i < s.size(); i++)
  148.         printf("%c", s[i]);
  149.     printf("%c", '\n');
  150. }
  151.  
  152. int main()
  153. {
  154.     setlocale(LC_ALL, "Russian");
  155.     printTask();
  156.     std::string inputs, s;
  157.     inputs = inputString();
  158.     s = correctingString(inputs);
  159.     if (s == "") {
  160.         printf("%s", "Введена пустая строка");
  161.         return 0;
  162.     }
  163.     printNewString(s);
  164.     std::string* words = (std::string*)malloc(sizeof(std::string));
  165.     words = findWords(s);
  166.     int kol = getNumOfWords(words);
  167.     printWords(words, kol);
  168.     std::string s1 = createFirstString(words, kol);
  169.     if (s1 == "")
  170.         s1 = "получена пустая строка";
  171.     printString1(s1);
  172.     std::string s2 = createSecondString(words, kol);
  173.     if (s2 == "")
  174.         s2 = "получена пустая строка";
  175.     printString2(s2);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement