Advertisement
J3st3rs_j0k3

cw_yumvl.c

Apr 19th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>
  6. #include <locale.h>
  7.  
  8.  
  9.  
  10. int del_eq_sen(char** main, char** main_update, int count);
  11. int del_even(char**  first_text, char** new_text, int count);
  12. char **sentence(char *sent, int *count);
  13. char** strt_dig(char** txt_1, int * counter, int count);
  14. void less_then_3(char **txt, int *count);
  15. int sup_func(const void* a, const void* b);
  16. void sorting(char** answer, int car);
  17. void dig_str(char** text, int count);
  18.  
  19.  
  20. int main(){
  21.     setlocale(LC_ALL, "Russian");
  22.     char* input_words;
  23.     char** initial_txt;
  24.     char** main_upd;
  25.     char** txt_rmvd;
  26.  
  27.     initial_txt = malloc(100*sizeof(char*));
  28.     char symb;
  29.     int n;
  30.     int govno;
  31.     //выделение памяти под строку с текстом
  32.     input_words = malloc(100*sizeof(char));
  33.     int size = 100;
  34.  
  35.     int i = 0, j, counter = -1, count = 0;
  36.  
  37.     //считывание символов с клавиатуры
  38.     while((symb = getchar()) != '\n') {
  39.         input_words[i] = symb;
  40.         i++;
  41.         if (i ==  size - 1){
  42.             size = size*2;
  43.             input_words = realloc(input_words, (size+2)*sizeof(char));
  44.         }
  45.     }
  46.  
  47.     //выделение памяти нп массив указателей (предложения)
  48.     initial_txt = malloc(100*sizeof(char*));
  49.     int size_arr = 100;
  50.  
  51.     //разделение исходной строки на предложения
  52.     for(i = 0;i < strlen(input_words); i++){
  53.         counter++;
  54.         if(input_words[i] == '.'){
  55.             initial_txt[count] = malloc(counter*sizeof(char)+10);
  56.             for(j = 0, n = i - counter; n <= i ; n++, j++){
  57.                 initial_txt[count][j] = input_words[n];
  58.             }
  59.             initial_txt = realloc(initial_txt, 100*i*sizeof(char));
  60.             initial_txt[count][j] = '\0';
  61.             count++;
  62.             if (count == size_arr - 1){
  63.                 size_arr = size_arr*2;
  64.                 initial_txt = realloc(initial_txt, size_arr*sizeof(char));
  65.             }
  66.             counter = -1;
  67.             i++;
  68.         }
  69.     }
  70.  
  71.     main_upd = malloc(count * sizeof(char*));
  72.     count = del_eq_sen(initial_txt, main_upd, count);
  73.     initial_txt = main_upd;
  74.     printf("Текст без одинаковых предложений: ");
  75.         for(i = 0; i < count; i ++){
  76.             printf("%s\n", initial_txt[i]);
  77.     }
  78.     //выделение памяти под полученный массив после работы функции del_ev
  79.  
  80.     txt_rmvd = malloc(count*sizeof(char*));
  81.  
  82.     int counterslova = 0;
  83.     int option;
  84.     printf("1 - Удалить все четные по счету предложения в которых четное количество слов.\n"
  85.            "2 - Отсортировать все слова в предложениях по возрастанию количества букв в верхнем регистре в слове.\n"
  86.            "3 - Заменить все слова в тексте длина которых не более 3 символов на подстроку “Less Then 3”.\n"
  87.            "4 - Найти в каждом предложении строку максимальной длины, которая начинается и заканчивается цифрой. Вывести найденные подстроки по убыванию длины подстроки.\n"
  88.            "5 - Выход из программы.\n");
  89.     // //считывание переменной, отвечающей за выбор функции, которую нужно исполнить
  90.      scanf("%d", &option);
  91.      switch(option){
  92.          case 1:
  93.              count = del_even(initial_txt, txt_rmvd, count);
  94.              break;
  95.         case 2:
  96.             sorting(initial_txt, count);
  97.             break;
  98.          case 3:
  99.              less_then_3(initial_txt, &count);
  100.  
  101.              break;
  102.          case 4:
  103.              dig_str(initial_txt, count);
  104.              break;
  105.          case 5:
  106.              printf("Программа завершена досрочно");
  107.              return 0;
  108.  
  109.          default:
  110.              printf("Данные некорректны");
  111.     }
  112.     if (option == 1){
  113.         for(int g = 0; g < count; g++){
  114.         printf("%s ", txt_rmvd[g]);
  115.         }
  116.     }
  117.     return 0;
  118. }
  119.  
  120. //удаление одинаковых предложений
  121. int del_eq_sen(char ** main, char**main_update, int count) {
  122.     int k, z, w = 0;
  123.     char*comp_1;
  124.     char*comp_2;
  125.     for (int i = 0; i < count; i++){
  126.         k = 0;
  127.         comp_1 = main[i];
  128.         for (z = 0; z < count-1; z++) {
  129.             if (z == i)
  130.                 z++;
  131.             if (strlen(main[i]) == strlen(main[z])) {
  132.                 comp_2 = main[z];
  133.                 for (int j  = 0; j < strlen(main[i]); j++){
  134.                     comp_1[j] = (char)(tolower(comp_1[j]));
  135.                     comp_2[j] = (char)(tolower(comp_2[j]));
  136.                 }
  137.                 if (strcmp(comp_1, comp_2) == 0) {
  138.                     k++;
  139.                 }
  140.             }
  141.         }
  142.         if (i != count) {
  143.             if (k == 0)
  144.             {
  145.                 main_update[w] = main[i];
  146.                 w++;
  147.             }
  148.         }
  149.  
  150.     }
  151.     return w;
  152. }
  153.  
  154.  
  155. //функция по удалению четных предложений с четным количеством слов
  156. int del_even(char**  first_text, char** new_text,int count){
  157.     int j; int i; int counter;
  158.     int g = 0;
  159.     for(j = 0; j < count; j++){
  160.         i = 0; counter = 0;
  161.         if(j%2 != 0){
  162.             while(first_text[j][i] != '.'){
  163.                 if(first_text[j][i] == ' ' || first_text[j][i] == ','){
  164.                     if(first_text[j][i] == ','){
  165.                         counter++;
  166.                         i++;
  167.                     }
  168.                     else{
  169.                         counter++;
  170.                     }
  171.                 }
  172.                 i++;
  173.             }
  174.             counter++;
  175.             if(counter%2 != 0){
  176.                 new_text[g] = first_text[j];
  177.                 g++;
  178.             }
  179.         }
  180.         else{
  181.             new_text[g] = first_text[j];
  182.             g++;
  183.         }
  184.     }
  185.     return g;
  186. }
  187.  
  188. //функция, заменяющая слова в предложении на подстроку Less than 3
  189. void less_then_3(char **txt, int *count){
  190.     for (int i = 0; i < *count; i++) {
  191.         char* string = txt[i];
  192.         char* new_string = (char*)malloc(1 * sizeof(char));
  193.         new_string[0] = '\0';
  194.         int len_string = 0;
  195.         int len_word = 0;
  196.         int len_new_string = 0;
  197.         while (string[len_string] != '\0') {
  198.             if ((string[len_string] == ',') || (string[len_string] == '.') || (string[len_string] == ' ')) {
  199.                 if (len_word <= 3) {
  200.                     new_string = (char*)realloc(new_string, (strlen(new_string) + 13) * sizeof(char));
  201.                     strcat(new_string, "Less Then 3");
  202.                     new_string[strlen(new_string) + 1] = '\0';
  203.                     new_string[strlen(new_string)] = string[len_string];
  204.                     len_new_string += 12;
  205.                 }
  206.                 else {
  207.                     new_string = (char*)realloc(new_string, (strlen(new_string) + len_word + 2) * sizeof(char));
  208.                     for (int j = 0; j <= len_word; j++) {
  209.                         new_string[len_new_string] = string[len_string - len_word + j];
  210.                         len_new_string++;
  211.                     }
  212.                     new_string[len_new_string] = '\0';
  213.                 }
  214.                 len_word = -1;
  215.             }
  216.             len_word++;
  217.             len_string++;
  218.         }
  219.         txt[i] = new_string;
  220.  
  221.     }
  222.     for(int s = 0; s < *count; s++)
  223.         printf("%s ",txt[s]);
  224. }
  225.  
  226.  
  227. char **sentence(char *sent, int *count){
  228.     char *istr = sent;
  229.     int id = 0;
  230.     char **senten = malloc (sizeof(char *));
  231.     istr = strtok(istr," ,.");
  232.     while (istr != NULL)
  233.     {
  234.         senten[id++] = istr;
  235.         senten = realloc(senten, (id + 10) * sizeof(char));
  236.         istr = strtok (NULL," ,.");
  237.     }
  238.     *count = id;
  239.     return senten;
  240. }
  241.  
  242.  
  243. //функция 2
  244. int sup_func(const void* a, const void* b){
  245.     const char** str_1 = (const char**)a;
  246.     const char** str_2 = (const char**)b;
  247.     int sum_1 = 0, sum_2 = 0, i;
  248.     for(i = 0; i < strlen(str_1[0]); ++i)
  249.         if(str_1[0][i] == toupper(str_1[0][i]))
  250.             sum_1 += 1;
  251.  
  252.     for(i = 0; i < strlen(str_2[0]); ++i)
  253.         if(str_2[0][i] == toupper(str_2[0][i]))
  254.             sum_2 += 1;
  255.     if(sum_1 != sum_2)
  256.         {
  257.         if(sum_1 > sum_2)
  258.             return 1;
  259.         if(sum_2 > sum_1)
  260.             return -1;
  261.         }
  262.     return 0;
  263. }
  264.  
  265. void sorting(char** answer, int car){
  266.     char** dop_arr;
  267.     char *istr;
  268.     char ** demo;
  269.  
  270.     int count;
  271.     for(int j = 0; j < car; j++){
  272.         count = 0;
  273.  
  274.         dop_arr = sentence(answer[j], &count);
  275.         qsort(dop_arr, count, sizeof(char*), sup_func);
  276.         for(int v = 0; v < count; v++){
  277.             printf("%s ", dop_arr[v]);
  278.         }
  279.         printf("\n");
  280.     }
  281. }
  282.  
  283. //функция по нахождению наибольшей строки в предложении
  284. void dig_str(char** text, int count){
  285.     char** mass_words = (char**)malloc(count*sizeof(char*));
  286.     int* lens = (int*)malloc(count * sizeof(int*));
  287.     // int first = -1;
  288.     // int last = -1;
  289.     for (int i = 0; i < count; i++) {
  290.         int first = -1;
  291.         int last = -1;
  292.         for (int j = 0; j < strlen(text[i]); j++) {
  293.             if (isdigit(text[i][j])) {
  294.                 first = j;
  295.                 break;
  296.             }
  297.         }
  298.         for (int k = strlen(text[i]) - 1; k >= 0; k--) {
  299.             if (isdigit(text[i][k])) {
  300.                 last = k;
  301.                 break;
  302.             }
  303.         }
  304.         /*if (first != last) {
  305.             printf("%d %d. ", first, last);
  306.         }
  307.         else {
  308.             printf("%d. ", first);
  309.         }
  310.         */
  311.         if(first != -1){
  312.             mass_words[i] = (char*)malloc((last - first + 1)*sizeof(char*));
  313.             for (int h = first; h <= last; h++) {
  314.                 mass_words[i][h-first] = text[i][h];
  315.             }
  316.             lens[i] = last - first + 1;
  317.         }
  318.         else{
  319.             lens[i] = 0;
  320.             continue;
  321.         }
  322. }
  323.     for (int kus = 0; kus < count - 1; kus++) {
  324.         for (int gus = kus + 1; gus < count; gus++) {
  325.             if (lens[kus] < lens[gus]) {
  326.                 char* buf = mass_words[kus];
  327.                 mass_words[kus] = mass_words[gus];
  328.                 mass_words[gus] = buf;
  329.                 lens[kus] += lens[gus];
  330.                 lens[gus] = lens[kus] - lens[gus];
  331.                 lens[kus] -= lens[gus];
  332.             }
  333.         }
  334.     }
  335.     printf("\n");
  336.     for (int l = 0; l < count; l++) {
  337.         for(int i = 0; i < lens[l]; i++)
  338.             putchar(mass_words[l][i]);
  339.             printf("\n");
  340.     }
  341. }
  342.  
  343.  
  344.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement