Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdlib.h>
- #include <stdio.h>
- void func(char** str, int a, int start, char* curStr, int lenCurrentStr) {
- bool flag = false; // если в списке слов есть подходящее под условие слово, то меняем Флаг
- int endLine = 0;
- while (str[start][endLine] != ' ') {
- endLine++;
- }
- endLine--;
- for (int i = start + 1; i < a; i++) {
- if (str[i][0] == str[start][endLine]) {
- flag = true;
- for (int j = 0; str[i][j - 1] != ' '; j++) {
- lenCurrentStr++;
- curStr = (char*)realloc(curStr, (lenCurrentStr + 1) * sizeof(char));
- curStr[lenCurrentStr] = str[i][j];
- }
- char* buff = str[i]; //меняем местами I-тый и Start-овый эл-ты
- str[i] = str[start + 1]; //после завершения работы функции нужнно поменять их обратно
- str[start + 1] = buff;
- func(str, a, start + 1, curStr, lenCurrentStr);
- buff = str[start + 1];
- str[start + 1] = str[i];
- str[i] = buff;
- //после каждого прохода нужно уменьшать длинну текущего кроссворда до последнего пробела.
- lenCurrentStr--;
- while (curStr[lenCurrentStr] != ' ') {
- lenCurrentStr--;
- }
- /*for (int i = 0; i < lenCurrentStr; i++) {
- printf_s("%c", curStr[i]);
- }
- printf_s("\n");*/
- }
- }
- if (!flag) {
- for (int i = 0; i < lenCurrentStr; i++) {
- printf_s("%c", curStr[i]);
- }
- printf_s("\n");
- }
- }
- int main()
- {
- char** str; //список слов
- int a = 0; // колличество слов в списке
- scanf_s("%d", &a);
- str = (char**)malloc(a * sizeof(char*));
- str[0] = (char*)malloc(sizeof(char));
- scanf_s("%c", &str[0][0]);//после считывания А остался не считаный символ \n, который тоже надо считать.
- free(str[0]);
- for (int i = 0; i < a; i++) { //считывание слов кроссворда
- str[i] = (char*)malloc(256 * sizeof(char));
- int j = -1;
- do {
- j++;
- scanf_s("%c", &str[i][j]);
- if (j > 256) {
- str[i] = (char*)realloc(str[i], (j + 1) * sizeof(char));
- }
- if (str[i][j] == '\n') {
- str[i][j] = ' ';
- }
- } while (str[i][j] != ' ');
- }
- char* curStr = (char*)malloc(256 * sizeof(char));
- int lenCurrentStr = -1;
- for (int i = 0; i < a; i++) {
- do {
- lenCurrentStr++;
- if (lenCurrentStr > 255) {
- curStr = (char*)realloc(curStr, (lenCurrentStr + 1) * sizeof(char));
- }
- curStr[lenCurrentStr] = str[i][lenCurrentStr];
- } while (str[i][lenCurrentStr] != ' ');
- char* buff = str[0];
- str[0] = str[i];
- str[i] = buff;
- func(str, a, 0, curStr, lenCurrentStr);
- free(curStr);
- curStr = (char*)malloc(256 * sizeof(char));
- lenCurrentStr = -1;
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement