Advertisement
Guest User

Лаба 1.2

a guest
Mar 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. void func(char** str, int a, int start, char* curStr, int lenCurrentStr) {
  6.     bool flag = false; // если в списке слов есть подходящее под условие слово, то меняем Флаг
  7.     int endLine = 0;
  8.     while (str[start][endLine] != ' ') {
  9.         endLine++;
  10.     }
  11.     endLine--;
  12.  
  13.     for (int i = start + 1; i < a; i++) {
  14.  
  15.         if (str[i][0] == str[start][endLine]) {
  16.             flag = true;
  17.  
  18.             for (int j = 0; str[i][j - 1] != ' '; j++) {
  19.                 lenCurrentStr++;
  20.                 curStr = (char*)realloc(curStr, (lenCurrentStr + 1) * sizeof(char));
  21.                 curStr[lenCurrentStr] = str[i][j];
  22.             }
  23.  
  24.             char* buff = str[i]; //меняем местами I-тый и Start-овый эл-ты
  25.             str[i] = str[start + 1]; //после завершения работы функции нужнно поменять их обратно
  26.             str[start + 1] = buff;
  27.  
  28.             func(str, a, start + 1, curStr, lenCurrentStr);
  29.  
  30.             buff = str[start + 1];
  31.             str[start + 1] = str[i];
  32.             str[i] = buff;
  33.  
  34.             //после каждого прохода нужно уменьшать длинну текущего кроссворда до последнего пробела.
  35.  
  36.             lenCurrentStr--;
  37.  
  38.             while (curStr[lenCurrentStr] != ' ') {
  39.                 lenCurrentStr--;
  40.             }
  41.  
  42.             /*for (int i = 0; i < lenCurrentStr; i++) {
  43.             printf_s("%c", curStr[i]);
  44.             }
  45.             printf_s("\n");*/
  46.         }
  47.     }
  48.     if (!flag) {
  49.         for (int i = 0; i < lenCurrentStr; i++) {
  50.             printf_s("%c", curStr[i]);
  51.         }
  52.         printf_s("\n");
  53.     }
  54. }
  55.  
  56. int main()
  57. {
  58.     char** str; //список слов
  59.     int a = 0; // колличество слов в списке
  60.     scanf_s("%d", &a);
  61.     str = (char**)malloc(a * sizeof(char*));
  62.  
  63.     str[0] = (char*)malloc(sizeof(char));
  64.     scanf_s("%c", &str[0][0]);//после считывания А остался не считаный символ \n, который тоже надо считать.
  65.     free(str[0]);
  66.  
  67.     for (int i = 0; i < a; i++) { //считывание слов кроссворда
  68.         str[i] = (char*)malloc(256 * sizeof(char));
  69.         int j = -1;
  70.  
  71.         do {
  72.             j++;
  73.             scanf_s("%c", &str[i][j]);
  74.  
  75.             if (j > 256) {
  76.                 str[i] = (char*)realloc(str[i], (j + 1) * sizeof(char));
  77.             }
  78.  
  79.             if (str[i][j] == '\n') {
  80.                 str[i][j] = ' ';
  81.             }
  82.         } while (str[i][j] != ' ');
  83.     }
  84.  
  85.     char* curStr = (char*)malloc(256 * sizeof(char));
  86.     int lenCurrentStr = -1;
  87.  
  88.     for (int i = 0; i < a; i++) {
  89.         do {
  90.             lenCurrentStr++;
  91.             if (lenCurrentStr > 255) {
  92.                 curStr = (char*)realloc(curStr, (lenCurrentStr + 1) * sizeof(char));
  93.             }
  94.             curStr[lenCurrentStr] = str[i][lenCurrentStr];
  95.         } while (str[i][lenCurrentStr] != ' ');
  96.  
  97.         char* buff = str[0];
  98.         str[0] = str[i];
  99.         str[i] = buff;
  100.  
  101.         func(str, a, 0, curStr, lenCurrentStr);
  102.         free(curStr);
  103.         curStr = (char*)malloc(256 * sizeof(char));
  104.         lenCurrentStr = -1;
  105.     }
  106.  
  107.     getchar();
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement