Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- using namespace std;
- char** arrStr; //массив строк текста
- int countStr; //кол-во строк
- char* separators; //массив разделителей
- int countSep; //кол-во разделителей
- char** result; //Результат работы
- int countRes; //Кол-во результирующих строк
- int countWord; //Кол-во слов в строке
- void ReadAllStr();
- void ReadAllSep();
- void ReadStr(char* str);
- void ReadSep(char sep);
- bool CheckSep(char symbol);
- bool CheckStop(char* str);
- bool CheckSame(char sep);
- bool GoodStr(char* str);
- void PrintAllStr();
- void PrintAllSep();
- void PrintResult();
- int GetCountWord(char* str);
- char* ProcStr(char* str);
- char** Split(char* str);
- int FindMinSep(char** words);
- int GetCountSep(char* words);
- void PrintHello()
- {
- printf("***************************************************************************************\n");
- printf("* *\n");
- printf("* * * * * * * * * * * * * * * * * * | *\n");
- printf("* * * * * * * * * * * * * | *\n");
- printf("* * * * * * * * * * * * * * * * * | *\n");
- printf("* * * * * * * * * * * * * *\n");
- printf("* * * * * * * * * * * * * * * * * * * * * *\n");
- printf("* *\n");
- printf("***************************************************************************************\n");
- printf("\n\n");
- }
- int main()
- {
- setlocale(LC_CTYPE, "rus"); // вызов функции настройки локали
- countSep = 0;
- countStr = 0;
- PrintHello();
- printf("Ввод строк. Вводить на англ. Если строка будет повторяться, ввод строк прекратиться\n");
- printf("Внимательно следите за пробелами. Каждый пробел обозначает разделение слова.\n");
- printf("Не вводить несколько пробелов подрят и перед концом строки\n");
- ReadAllStr();
- PrintAllStr();
- printf("Ввод разделителей\n");
- //вводим разделители
- ReadAllSep();
- PrintAllSep();
- countRes = 0;
- for (int i = 0; i < countStr; i++)
- {
- //Если в строке нечетное кол-во слов, отправляем на обработку
- if (GoodStr(arrStr[i]))
- {
- char* newStr = ProcStr(arrStr[i]);
- countRes++;
- result = (char**)realloc(result, countRes * sizeof(char*));
- int k = strlen(newStr);
- result[countRes - 1] = (char*)malloc(k + 1);
- for (int j = 0; j < k; j++)
- {
- result[countRes - 1][j] = newStr[j];
- }
- result[countRes - 1][k] = '\0';
- }
- }
- PrintResult();
- for (int i = 0; i < countStr; i++)
- {
- free(arrStr[i]);
- }
- for (int i = 0; i < countRes; i++)
- {
- free(result[i]);
- }
- free(arrStr);
- free(separators);
- free(result);
- bool f;
- }
- void ReadAllStr()
- {
- bool stop = false;
- do
- {
- char str[80];
- printf("Введите строку не превышающую 80 символов:\n");
- gets_s(str);
- if (CheckStop(str))
- stop = true;
- else
- ReadStr(str);
- } while (!stop);
- }
- void ReadStr(char* str)
- {
- //найдем размер строки
- int k = 0;
- for (int i = 0; i < strlen(str) && str[i] != '\0'; i++)
- {
- k++;
- }
- //выделим память
- countStr++;
- arrStr = (char**)realloc(arrStr, countStr * sizeof(char*));
- arrStr[countStr - 1] = (char*)malloc(k + 1);
- //перезапишем в наш массив
- for (int i = 0; i < k; i++)
- {
- arrStr[countStr - 1][i] = str[i];
- }
- arrStr[countStr - 1][k] = '\0';
- }
- bool CheckStop(char* str)
- {
- //проверка на существование похожей строки
- for (int i = 0; i < countStr; i++)
- {
- //если строки идентичны
- if (strcmp(arrStr[i], str) == 0) return true;
- }
- return false;
- }
- void ReadAllSep()
- {
- bool stop = false;
- char sep;
- //scanf("%c", &sep);;
- do
- {
- printf("Введите разделитель (Считывает только 1 символ из введеной строки): \n");
- //чтение 1 символа
- scanf("%c", &sep);
- //Все остальное в этой строке выкидываем
- for (char c = getchar(); c != '\n' && c != EOF; c = getchar());
- //проверка разделителя
- if (CheckSame(sep)) printf("Такой разделитель уже существует: %c \n", sep);
- else
- {
- ReadSep(sep);
- printf("Получен разделитель : %c \n", separators[countSep - 1]);
- }
- printf("Хотите ввести еще 1? (1 - да, 0 - нет) ");
- sep = getchar();
- if (sep != '1') stop = true;
- //Все остальное в этой строке выкидываем, если ввели больше 1 символа
- for (char c = getchar(); c != '\n' && c != EOF; c = getchar());
- } while (!stop);
- }
- bool CheckSame(char sep)
- {
- //проверка на существование похожего разделителя
- for (int i = 0; i < countSep; i++)
- {
- //если строки идентичны
- if (separators[i] == sep) return true;
- }
- return false;
- }
- void ReadSep(char sep)
- {
- //выделим еще память
- countSep++;
- separators = (char*)realloc(separators, countSep * sizeof(char));
- separators[countSep - 1] = sep;
- }
- void PrintAllStr()
- {
- printf("Введенный текст:\n");
- for (int i = 0; i < countStr; i++)
- {
- printf("%s\n", arrStr[i]);
- }
- printf("\n");
- }
- void PrintAllSep()
- {
- printf("Получены разделители:\n");
- for (int i = 0; i < countSep; i++)
- {
- printf("%c \n", separators[i]);
- }
- printf("\n");
- }
- bool GoodStr(char* str)
- {
- if (GetCountWord(str) % 2 == 0) return false;
- return true;
- }
- int GetCountWord(char* str)
- {
- countWord = 0;
- for (int i = 0; i < strlen(str) && str[i] != '\0'; i++)
- {
- if (str[i] == ' ') countWord++;
- }
- if (strlen(str) != 0) countWord++;
- return countWord;
- }
- char* ProcStr(char* str)
- {
- char* newStr = NULL;
- int k = 0;
- char** words = Split(str);
- int min = FindMinSep(words);
- for (int i = 0; i < countWord; i++)
- {
- if (GetCountSep(words[i]) != min)
- {
- for (int j = 0; words[i][j] != '\0'; j++)
- {
- newStr = (char*)realloc(newStr, (k + 1) * sizeof(char));
- newStr[k] = words[i][j];
- k++;
- }
- newStr = (char*)realloc(newStr, (k + 1) * sizeof(char));
- newStr[k] = ' ';
- k++;
- }
- }
- newStr = (char*)realloc(newStr, (k + 1) * sizeof(char));
- newStr[k] = '\0';
- k++;
- //free(words);
- //words = NULL;
- for (int i = 0; i < countWord; i++)
- {
- free(words[i]);
- }
- countWord = 0;
- return newStr;
- }
- char** Split(char* str)
- {
- char** arrWords = (char**)malloc(countWord * sizeof(char*));
- int cur = 0;
- char* buffer = NULL;
- int countBuff = 0;
- for (int i = 0; i < strlen(str) && str[i] != '\0'; i++)
- {
- if (str[i] != ' ')
- {
- countBuff++;
- buffer = (char*)realloc(buffer, countBuff * sizeof(char));
- buffer[countBuff - 1] = str[i];
- }
- else
- {
- countBuff++;
- buffer = (char*)realloc(buffer, countBuff * sizeof(char));
- buffer[countBuff - 1] = '\0';
- arrWords[cur] = (char*)malloc(countBuff);
- for (int j = 0; j < countBuff; j++)
- {
- arrWords[cur][j] = buffer[j];
- }
- cur++;
- free(buffer);
- buffer = NULL;
- countBuff = 0;
- }
- }
- if (buffer != NULL)
- {
- countBuff++;
- buffer = (char*)realloc(buffer, countBuff * sizeof(char));
- buffer[countBuff - 1] = '\0';
- arrWords[cur] = (char*)malloc(countBuff);
- for (int j = 0; j < countBuff; j++)
- {
- arrWords[cur][j] = buffer[j];
- }
- cur++;
- }
- free(buffer);
- return arrWords;
- }
- int FindMinSep(char** words)
- {
- int min = 1000;
- for (int i = 0; i < countWord; i++)
- {
- int newMin = GetCountSep(words[i]);
- if (newMin < min && newMin != 0) min = newMin;
- }
- return min;
- }
- int GetCountSep(char* words)
- {
- int k = 0;
- for (int i = 0; i < strlen(words); i++)
- {
- if (CheckSep(words[i])) k++;
- }
- return k;
- }
- bool CheckSep(char symbol)
- {
- for (int i = 0; i < countSep; i++)
- {
- if (separators[i] == symbol) return true;
- }
- return false;
- }
- void PrintResult()
- {
- printf("Результат:\n");
- printf("********\n");
- for (int i = 0; i < countRes; i++)
- {
- printf("%s\n", result[i]);
- }
- printf("********\n");
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement