Silent1Snow

Untitled

Dec 20th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. // urs_2.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string.h>
  7. #include <conio.h>
  8. #include <Windows.h>
  9.  
  10. void in(char** &mass_f, char* &razd_f, int &num_f, int* &kol_f, int &kol_w_f, char* &str_f);
  11. void s_index(char** &txt_f, int &num_f, char* &razd_f, int &word_f, int* &index_f, int &kol_w_f, int &razm_i_f);
  12. void out(char** &rez_f, int new_str_r_f);
  13. void obr(char** &txt_f, char** &rez_f, char* &razd_f, int &num_f, int &razm_i_f, int* &index_f, char* &str_f,int new_str_r_f);
  14.  
  15. int main()
  16. {
  17.     setlocale(LC_ALL, ".1251");
  18.     char **txt = (char**)malloc(sizeof(char*) * 1);
  19.     txt[0] = (char*)malloc(sizeof(char) * 1);
  20.     char *razd = (char*)malloc(sizeof(char)*81);
  21.     int num = 0;
  22.     int *kol = (int*)malloc(sizeof(int)*1);
  23.     int kol_w = 0;
  24.     char *str = (char*)malloc(sizeof(char)*1);
  25.     char **rez;
  26.     int word = 0;
  27.     int razm_i;
  28.     int *index = (int*)malloc(sizeof(int)*1);
  29.     int new_str_r = 0;
  30.     in(txt, razd, num, kol, kol_w, str);
  31.     s_index(txt, num, razd, word, index, kol_w, razm_i);
  32.     obr(txt, rez, razd, num, razm_i, index, str, new_str_r);
  33.     out(rez, new_str_r);
  34.     system("pause");
  35.     return 0;
  36. }
  37.  
  38. void in(char** &mass_f, char* &razd_f, int &num_f, int* &kol_f, int &kol_w_f, char* &str_f)
  39. {
  40.     int sr_kol = 0;
  41.     int p_kol = 0;
  42.     int f_3 = 1;
  43.     puts("Введите символы-разделители");
  44.     scanf("%s", razd_f);
  45.     do{
  46.         char *str = (char*)malloc(sizeof(char) * 256);
  47.         printf("Введите строку [%d]\n", (num_f + 1));
  48.         scanf("%s", str);
  49.         kol_f = (int*)realloc(kol_f, sizeof(int)*(num_f + 1));
  50.         int len = strlen(str);
  51.         int r_len = strlen(razd_f);
  52.         mass_f = (char**)realloc(mass_f, sizeof(char*)*(num_f + 1));
  53.         mass_f[num_f] = NULL;
  54.         mass_f[num_f] = (char*)realloc(mass_f[num_f], sizeof(char)*(len + 1));
  55.         strcpy(mass_f[num_f], str);
  56.         int f = 1;
  57.         int r_word = 0;
  58.         int f_2 = 0;
  59.         kol_f[num_f] = len;
  60.         p_kol = p_kol + kol_f[num_f];
  61.         if (num_f != 0)
  62.         {
  63.             sr_kol = (p_kol - kol_f[num_f]) / (num_f);
  64.         }
  65.         if (sr_kol == kol_f[num_f])
  66.         {
  67.             f_3 = 0;
  68.         }
  69.         num_f++;
  70.     } while (f_3 == 1);
  71.     puts("Введите количество слов: ");
  72.     scanf("%d", &kol_w_f);
  73.     puts("Введите строку для вставки: ");
  74.     int i = 0;
  75.     while ((str_f[i] = getch()) != '\r')
  76.     {
  77.         i++;
  78.         str_f = (char*)realloc(str_f, (i + 1)*sizeof(char));
  79.     }
  80.     str_f[i] = '\0';
  81. }
  82.  
  83. void s_index(char** &txt_f, int &num_f, char* &razd_f, int &word_f, int* &index_f, int &kol_w_f, int &razm_i_f)
  84. {
  85.     int flag = 0;
  86.     word_f = 0;
  87.     for (int i = 0; i < num_f; i++)
  88.     {
  89.         int len = strlen(txt_f[i]);
  90.         for (int j = 0; j < len; j++)
  91.         {
  92.             int r_len = strlen(razd_f);
  93.             for (int k = 0; (k < r_len )&& (flag = 0); k++)
  94.             {
  95.                 if (txt_f[i][j] != razd_f[k])
  96.                 {
  97.                     flag = 1;
  98.                 }
  99.             }
  100.         }
  101.         if (flag == 1)
  102.         {
  103.             word_f++;
  104.         }
  105.         if (word_f == kol_w_f)
  106.         {
  107.             razm_i_f = 0;
  108.             index_f = (int*)realloc(index_f, sizeof(int)*(razm_i_f++));
  109.             index_f[razm_i_f] = num_f;
  110.             razm_i_f++;
  111.         }
  112.     }  
  113. }
  114.  
  115. void obr(char** &txt_f, char** &rez_f, char* &razd_f, int &num_f, int &razm_i_f, int* &index_f, char* &str_f, int new_str_r_f)
  116. {
  117.     int len_str = strlen(str_f);
  118.     int len;
  119.     int r_len = strlen(razd_f);
  120.     int k_num = 0;
  121.     int new_razm = 0;
  122.     char *help;
  123.     for (int i = 0; i < num_f; i++)
  124.     {
  125.         len = strlen(txt_f[i]);
  126.         rez_f = (char**)realloc(rez_f, sizeof(char*)*(i++));
  127.         for (int j = 0; j < razm_i_f; j++)
  128.         {
  129.             if (i == index_f[razm_i_f])
  130.             {
  131.                 rez_f[i] = (char*)realloc(rez_f[i], sizeof(char)*len_str);
  132.                 rez_f[i] = NULL;
  133.                 strcpy(rez_f[i], str_f);
  134.                 i++;
  135.                 rez_f = (char**)realloc(rez_f, sizeof(char*)*(i++));
  136.                 rez_f[i] = (char*)realloc(rez_f[i], sizeof(char)*len);
  137.                 rez_f[i] = NULL;
  138.                 strcpy(rez_f[i], txt_f[i--]);
  139.             }
  140.             else
  141.             {
  142.                 int f = 1;
  143.                 int r_word = 0;
  144.                 {
  145.                     for (int k = 0; k < len && (r_word != 1); k++)
  146.                     {
  147.                         for (int j = 0; j < r_len; j++)
  148.                         {
  149.                             if (txt_f[i][k] != razd_f[j] && f == 1)
  150.                             {
  151.                                 f = 1;
  152.                             }
  153.                             else
  154.                             {
  155.                                 f = 0;
  156.                             }
  157.                         }
  158.                         if (f == 1)
  159.                         {
  160.                             r_word++;
  161.                             k_num = k;
  162.                         }
  163.                     }
  164.                 }
  165.                 if (r_word == 1)
  166.                 {
  167.                     new_razm = len - k_num;
  168.                     help = (char*)realloc(help,sizeof(char)*new_razm);
  169.                     help = NULL;
  170.                     for (int k = k_num; k < len; k++)
  171.                     {
  172.                         int h = 0;
  173.                         help[h] = txt_f[i][k];
  174.                         h++;
  175.                     }
  176.                 }
  177.                 rez_f[i] = (char*)realloc(rez_f[i], sizeof(char)*new_razm);
  178.                 rez_f[i] = NULL;
  179.                 strcpy(rez_f[i],help);
  180.             }
  181.         }
  182.         new_str_r_f++;
  183.     }
  184. }
  185. void out(char** &rez_f, int new_str_r_f)
  186. {
  187.     for (int i = 0; i < new_str_r_f; i++)
  188.     {
  189.         printf("%s\n", rez_f[i]);
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment