Advertisement
Guest User

koko

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