Advertisement
J3st3rs_j0k3

dcsc

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