Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.87 KB | None | 0 0
  1. #include "pch.h"
  2.  
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. char** arrStr;              //массив строк текста
  10. int countStr;               //кол-во строк
  11.  
  12. char* separators;           //массив разделителей
  13. int countSep;               //кол-во разделителей 
  14.  
  15. char** result;              //Результат работы
  16. int countRes;               //Кол-во результирующих строк
  17. int countWord;              //Кол-во слов в строке
  18.  
  19. void ReadAllStr();
  20. void ReadAllSep();
  21.  
  22. void ReadStr(char* str);
  23. void ReadSep(char sep);
  24.  
  25. bool CheckSep(char symbol);
  26. bool CheckStop(char* str);
  27. bool CheckSame(char sep);
  28. bool GoodStr(char* str);
  29.  
  30. void PrintAllStr();
  31. void PrintAllSep();
  32. void PrintResult();
  33.  
  34. int GetCountWord(char* str);
  35. char* ProcStr(char* str);
  36. char** Split(char* str);
  37. int FindMinSep(char** words);
  38. int GetCountSep(char* words);
  39.  
  40.  
  41. void PrintHello()
  42. {
  43.     printf("***************************************************************************************\n");
  44.     printf("*                                                                                     *\n");
  45.     printf("*      *           *   * * *   *         * * *    * * *    *       *   * * *   |      *\n");
  46.     printf("*       *         *    *       *        *        *     *   * *   * *   *       |      *\n");
  47.     printf("*        *   *   *     * * *   *       *         *     *   *   *   *   * * *   |      *\n");
  48.     printf("*         * * * *      *       *        *        *     *   *       *   *              *\n");
  49.     printf("*          *   *       * * *   * * *     * * *    * * *    *       *   * * *   *      *\n");
  50.     printf("*                                                                                     *\n");
  51.     printf("***************************************************************************************\n");
  52.  
  53.     printf("\n\n");
  54. }
  55.  
  56. int main()
  57. {
  58.     setlocale(LC_CTYPE, "rus"); // вызов функции настройки локали
  59.  
  60.     countSep = 0;
  61.     countStr = 0;
  62.  
  63.     PrintHello();
  64.  
  65.     printf("Ввод строк. Вводить на англ. Если строка будет повторяться, ввод строк прекратиться\n");
  66.     printf("Внимательно следите за пробелами. Каждый пробел обозначает разделение слова.\n");
  67.     printf("Не вводить несколько пробелов подрят и перед концом строки\n");
  68.     ReadAllStr();
  69.     PrintAllStr();
  70.  
  71.     printf("Ввод разделителей\n");
  72.     //вводим разделители
  73.     ReadAllSep();
  74.     PrintAllSep();
  75.  
  76.     countRes = 0;
  77.  
  78.     for (int i = 0; i < countStr; i++)
  79.     {
  80.         //Если в строке нечетное кол-во слов, отправляем на обработку
  81.         if (GoodStr(arrStr[i]))
  82.         {
  83.             char* newStr = ProcStr(arrStr[i]);
  84.  
  85.             countRes++;
  86.             result = (char**)realloc(result, countRes * sizeof(char*));
  87.             int k = strlen(newStr);
  88.             result[countRes - 1] = (char*)malloc(k + 1);
  89.  
  90.             for (int j = 0; j < k; j++)
  91.             {
  92.                 result[countRes - 1][j] = newStr[j];
  93.             }
  94.             result[countRes - 1][k] = '\0';
  95.         }
  96.     }
  97.  
  98.     PrintResult();
  99.  
  100.     for (int i = 0; i < countStr; i++)
  101.     {
  102.         free(arrStr[i]);
  103.     }
  104.  
  105.     for (int i = 0; i < countRes; i++)
  106.     {
  107.         free(result[i]);
  108.     }
  109.  
  110.     free(arrStr);
  111.     free(separators);
  112.     free(result);
  113.  
  114.     bool f;
  115. }
  116.  
  117. void ReadAllStr()
  118. {
  119.     bool stop = false;
  120.  
  121.     do
  122.     {
  123.         char str[80];
  124.         printf("Введите строку не превышающую 80 символов:\n");
  125.  
  126.         gets_s(str);
  127.  
  128.         if (CheckStop(str))
  129.             stop = true;
  130.         else
  131.             ReadStr(str);
  132.  
  133.     } while (!stop);
  134. }
  135.  
  136. void ReadStr(char* str)
  137. {
  138.     //найдем размер строки
  139.     int k = 0;
  140.  
  141.     for (int i = 0; i < strlen(str) && str[i] != '\0'; i++)
  142.     {
  143.         k++;
  144.     }
  145.  
  146.     //выделим память
  147.     countStr++;
  148.     arrStr = (char**)realloc(arrStr, countStr * sizeof(char*));
  149.  
  150.     arrStr[countStr - 1] = (char*)malloc(k + 1);
  151.  
  152.     //перезапишем в наш массив
  153.     for (int i = 0; i < k; i++)
  154.     {
  155.         arrStr[countStr - 1][i] = str[i];
  156.     }
  157.     arrStr[countStr - 1][k] = '\0';
  158. }
  159.  
  160. bool CheckStop(char* str)
  161. {
  162.     //проверка на существование похожей строки
  163.     for (int i = 0; i < countStr; i++)
  164.     {
  165.         //если строки идентичны
  166.         if (strcmp(arrStr[i], str) == 0) return true;
  167.     }
  168.     return false;
  169. }
  170.  
  171. void ReadAllSep()
  172. {
  173.     bool stop = false;
  174.     char sep;
  175.     //scanf("%c", &sep);;
  176.  
  177.     do
  178.     {
  179.         printf("Введите разделитель (Считывает только 1 символ из введеной строки): \n");
  180.         //чтение 1 символа
  181.         scanf("%c", &sep);
  182.  
  183.         //Все остальное в этой строке выкидываем
  184.         for (char c = getchar(); c != '\n' && c != EOF; c = getchar());
  185.  
  186.         //проверка разделителя
  187.  
  188.         if (CheckSame(sep)) printf("Такой разделитель уже существует: %c \n", sep);
  189.         else
  190.         {
  191.             ReadSep(sep);
  192.             printf("Получен разделитель : %c \n", separators[countSep - 1]);
  193.         }
  194.  
  195.         printf("Хотите ввести еще 1? (1 - да, 0 - нет) ");
  196.         sep = getchar();
  197.  
  198.         if (sep != '1') stop = true;
  199.  
  200.         //Все остальное в этой строке выкидываем, если ввели больше 1 символа
  201.         for (char c = getchar(); c != '\n' && c != EOF; c = getchar());
  202.     } while (!stop);
  203. }
  204.  
  205. bool CheckSame(char sep)
  206. {
  207.     //проверка на существование похожего разделителя
  208.     for (int i = 0; i < countSep; i++)
  209.     {
  210.         //если строки идентичны
  211.         if (separators[i] == sep) return true;
  212.     }
  213.     return false;
  214. }
  215.  
  216. void ReadSep(char sep)
  217. {
  218.     //выделим еще память
  219.     countSep++;
  220.     separators = (char*)realloc(separators, countSep * sizeof(char));
  221.     separators[countSep - 1] = sep;
  222. }
  223.  
  224. void PrintAllStr()
  225. {
  226.     printf("Введенный текст:\n");
  227.     for (int i = 0; i < countStr; i++)
  228.     {
  229.         printf("%s\n", arrStr[i]);
  230.     }
  231.     printf("\n");
  232. }
  233.  
  234. void PrintAllSep()
  235. {
  236.     printf("Получены разделители:\n");
  237.     for (int i = 0; i < countSep; i++)
  238.     {
  239.         printf("%c \n", separators[i]);
  240.     }
  241.     printf("\n");
  242. }
  243.  
  244. bool GoodStr(char* str)
  245. {
  246.     if (GetCountWord(str) % 2 == 0) return false;
  247.     return true;
  248. }
  249.  
  250. int GetCountWord(char* str)
  251. {
  252.     countWord = 0;
  253.  
  254.     for (int i = 0; i < strlen(str) && str[i] != '\0'; i++)
  255.     {
  256.         if (str[i] == ' ') countWord++;
  257.     }
  258.  
  259.     if (strlen(str) != 0) countWord++;
  260.  
  261.     return countWord;
  262. }
  263.  
  264. char* ProcStr(char* str)
  265. {
  266.     char* newStr = NULL;
  267.     int k = 0;
  268.  
  269.     char** words = Split(str);
  270.  
  271.     int min = FindMinSep(words);
  272.  
  273.     for (int i = 0; i < countWord; i++)
  274.     {
  275.         if (GetCountSep(words[i]) != min)
  276.         {
  277.             for (int j = 0; words[i][j] != '\0'; j++)
  278.             {
  279.                 newStr = (char*)realloc(newStr, (k + 1) * sizeof(char));
  280.                 newStr[k] = words[i][j];
  281.                 k++;
  282.             }
  283.             newStr = (char*)realloc(newStr, (k + 1) * sizeof(char));
  284.             newStr[k] = ' ';
  285.             k++;
  286.         }
  287.     }
  288.  
  289.     newStr = (char*)realloc(newStr, (k + 1) * sizeof(char));
  290.     newStr[k] = '\0';
  291.     k++;
  292.  
  293.     //free(words);
  294.     //words = NULL;
  295.     for (int i = 0; i < countWord; i++)
  296.     {
  297.         free(words[i]);
  298.     }
  299.     countWord = 0;
  300.     return newStr;
  301. }
  302.  
  303. char** Split(char* str)
  304. {
  305.     char** arrWords = (char**)malloc(countWord * sizeof(char*));
  306.  
  307.     int cur = 0;
  308.     char* buffer = NULL;
  309.     int countBuff = 0;
  310.  
  311.     for (int i = 0; i < strlen(str) && str[i] != '\0'; i++)
  312.     {
  313.         if (str[i] != ' ')
  314.         {
  315.             countBuff++;
  316.             buffer = (char*)realloc(buffer, countBuff * sizeof(char));
  317.             buffer[countBuff - 1] = str[i];
  318.         }
  319.         else
  320.         {
  321.             countBuff++;
  322.             buffer = (char*)realloc(buffer, countBuff * sizeof(char));
  323.             buffer[countBuff - 1] = '\0';
  324.  
  325.             arrWords[cur] = (char*)malloc(countBuff);
  326.             for (int j = 0; j < countBuff; j++)
  327.             {
  328.                 arrWords[cur][j] = buffer[j];
  329.             }
  330.             cur++;
  331.             free(buffer);
  332.             buffer = NULL;
  333.             countBuff = 0;
  334.         }
  335.     }
  336.  
  337.     if (buffer != NULL)
  338.     {
  339.         countBuff++;
  340.         buffer = (char*)realloc(buffer, countBuff * sizeof(char));
  341.         buffer[countBuff - 1] = '\0';
  342.         arrWords[cur] = (char*)malloc(countBuff);
  343.  
  344.         for (int j = 0; j < countBuff; j++)
  345.         {
  346.             arrWords[cur][j] = buffer[j];
  347.         }
  348.         cur++;
  349.     }
  350.  
  351.     free(buffer);
  352.     return arrWords;
  353. }
  354.  
  355. int FindMinSep(char** words)
  356. {
  357.     int min = 1000;
  358.     for (int i = 0; i < countWord; i++)
  359.     {
  360.         int newMin = GetCountSep(words[i]);
  361.  
  362.         if (newMin < min && newMin != 0) min = newMin;
  363.     }
  364.  
  365.     return min;
  366. }
  367.  
  368. int GetCountSep(char* words)
  369. {
  370.     int k = 0;
  371.     for (int i = 0; i < strlen(words); i++)
  372.     {
  373.         if (CheckSep(words[i])) k++;
  374.     }
  375.     return k;
  376. }
  377.  
  378. bool CheckSep(char symbol)
  379. {
  380.     for (int i = 0; i < countSep; i++)
  381.     {
  382.         if (separators[i] == symbol) return true;
  383.     }
  384.     return false;
  385. }
  386.  
  387. void PrintResult()
  388. {
  389.     printf("Результат:\n");
  390.     printf("********\n");
  391.     for (int i = 0; i < countRes; i++)
  392.     {
  393.         printf("%s\n", result[i]);
  394.     }
  395.     printf("********\n");
  396.     printf("\n");
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement