Advertisement
Guest User

lab1_2

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