Advertisement
Guest User

Untitled

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