Silent1Snow

Untitled

Dec 21st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 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 = 0;
  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.         for (int i = 0; i < len; i++)
  60.         {
  61.             for (int j = 0; j < r_len; j++)
  62.             {
  63.                 char a = str[i], b = razd_f[j];
  64.                 if (str[i] != razd_f[j] && f == 1)
  65.                 {
  66.                     f = 1;
  67.                 }
  68.                 else
  69.                 {
  70.                     f = 0;
  71.                 }
  72.             }
  73.             if (f == 1)
  74.             {
  75.                 r_word++;
  76.             }
  77.             f = 1;
  78.         }
  79.         kol_f[num_f] = r_word;
  80.         p_kol = p_kol + len;
  81.         if (num_f != 0)
  82.         {
  83.             sr_kol = (p_kol - len) / (num_f);
  84.         }
  85.         if (sr_kol == kol_f[num_f])
  86.         {
  87.             f_3 = 0;
  88.         }
  89.         num_f++;
  90.     } while (f_3 == 1);
  91.     puts("Введите количество слов: ");
  92.     scanf("%d", &kol_w_f);
  93.     puts("Введите строку для вставки: ");
  94.     int i = 0;
  95.     while ((str_f[i] = getch()) != '\r')
  96.     {
  97.         i++;
  98.         str_f = (char*)realloc(str_f, (i + 1)*sizeof(char));
  99.     }
  100.     str_f[i] = '\0';
  101. }
  102.  
  103. 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)
  104. {
  105.     int flag = 0;
  106.     word_f = 0;
  107.     for (int i = 0; i < num_f; i++)
  108.     {
  109.         int len = strlen(txt_f[i]);
  110.         for (int j = 0; j < len; j++)
  111.         {
  112.             int r_len = strlen(razd_f);
  113.             for (int k = 0; (k < r_len) && (txt_f[i][j] != razd_f[k]); k++)
  114.             {
  115.                
  116.             }
  117.             if (txt_f[i][j] == '\0')
  118.             {
  119.                 flag = 0;
  120.             }
  121.         }
  122.             word_f++;
  123.         if (word_f == kol_w_f)
  124.         {
  125.             index_f = (int*)realloc(index_f, sizeof(int)*(razm_i_f+1));
  126.             index_f[razm_i_f] = i;
  127.             razm_i_f++;
  128.         }
  129.         flag = 0;
  130.     }  
  131. }
  132.  
  133. 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)
  134. {
  135.     int len_str = strlen(str_f);
  136.     int len;
  137.     int r_len = strlen(razd_f);
  138.     int k_num = 0;
  139.     int new_razm = 0;
  140.     char *help;
  141.     rez_f = NULL;
  142.     for (int i = 0; i < num_f; i++)
  143.     {
  144.         len = strlen(txt_f[i]);
  145.         rez_f = (char**)realloc(rez_f, sizeof(char*)*(i+1));
  146.         for (int j = 0; j < razm_i_f; j++)
  147.         {
  148.             if (i == index_f[razm_i_f])
  149.             {
  150.                 rez_f[i] = (char*)realloc(rez_f[i], sizeof(char)*len_str);
  151.                 rez_f[i] = NULL;
  152.                 strcpy(rez_f[i], str_f);
  153.                 i++;
  154.                 rez_f = (char**)realloc(rez_f, sizeof(char*)*(i++));
  155.                 rez_f[i] = (char*)realloc(rez_f[i], sizeof(char)*len);
  156.                 rez_f[i] = NULL;
  157.                 strcpy(rez_f[i], txt_f[i--]);
  158.             }
  159.             else
  160.             {
  161.                 int f = 0;
  162.                 int r_word = 0;
  163.                 {
  164.                     for (int k = 0; k < len; k++)
  165.                     {
  166.                         for (int j = 0; j < r_len; j++)
  167.                         {
  168.                             if (txt_f[i][k] == razd_f[j])
  169.                             {
  170.                                 txt_f[i][k] = ' ';
  171.                                 k = 0;
  172.                             }
  173.                             else
  174.                             {
  175.                                 if (txt_f[i][k] == ' ')
  176.                                 {
  177.                                     f = 0;
  178.                                 }
  179.                                 else
  180.                                 {
  181.                                     if (txt_f[i][k] != razd_f[j] && f == 1)
  182.                                     {
  183.                                         f = 1;
  184.                                     }
  185.                                     else
  186.                                     {
  187.                                         f = 0;
  188.                                     }
  189.                                 }
  190.                             }
  191.                         }
  192.                         if (f == 1)
  193.                         {
  194.                             r_word++;
  195.                             k_num = k;
  196.                         }
  197.                     }
  198.                 }
  199.                 if (r_word == 1)
  200.                 {
  201.                     new_razm = len - k_num;
  202.                     help = (char*)realloc(help,sizeof(char)*new_razm);
  203.                     help = NULL;
  204.                     for (int k = k_num; k < len; k++)
  205.                     {
  206.                         int h = 0;
  207.                         help[h] = txt_f[i][k];
  208.                         h++;
  209.                     }
  210.                 }
  211.                 rez_f[i] = (char*)realloc(rez_f[i], sizeof(char)*new_razm);
  212.                 rez_f[i] = NULL;
  213.                 strcpy(rez_f[i],help);
  214.             }
  215.         }
  216.         new_str_r_f++;
  217.     }
  218. }
  219. void out(char** &rez_f, int new_str_r_f)
  220. {
  221.     for (int i = 0; i < new_str_r_f; i++)
  222.     {
  223.         printf("%s\n", rez_f[i]);
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment