Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.77 KB | None | 0 0
  1. #include <wchar.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5. #include <wctype.h>
  6.  
  7. #define BASE_COUNT_STRUCTURE 20
  8. #define BASE_LENGHT_STR 50
  9. #define Current_symbol text->all_str[line]->str[i]
  10. #define BASE_WORD_COUNT 30
  11. #define BASE_WORD_LENGHT 20
  12.  
  13.  
  14. struct Sentence//предложение и количество символов в нем
  15. {
  16.     wchar_t* str;
  17.     int lenght;
  18.     int latin_count;
  19. };
  20.  
  21. struct Text//Текст, массив указателей на структуры предложений и количество предложений
  22. {
  23.     struct Sentence** all_str;
  24.     int lenght;
  25. };
  26.  
  27. struct Word
  28. {
  29.     wchar_t* characters;
  30.     int count;
  31. };
  32.  
  33.  
  34. struct Words//Слово и его количество в тексте
  35. {
  36.     struct Word** str;
  37.     int count;
  38. };
  39.  
  40. void more_memory_word(struct Word *word, int *count_sym)
  41. {
  42.     *count_sym+=*count_sym;
  43.     word->characters=(wchar_t*)realloc(word->characters,*count_sym * sizeof(wchar_t));
  44.  
  45. }
  46.  
  47. void more_memory_words(struct Words *words, int *count_words)//Выделение памяти под слова. Способно увеличить количество слов и выдает каждому базовую длину.
  48. {
  49.     int i;
  50.     words->str=(struct Word**)realloc(words->str,(*count_words+BASE_WORD_COUNT)*sizeof(struct Word*));
  51.     for (i=*count_words;i<*count_words+BASE_WORD_COUNT;i++)
  52.     {
  53.         words->str[i]=(struct Word*)malloc(sizeof(struct Word));
  54.     }
  55.     for (i=*count_words;i<*count_words+BASE_WORD_COUNT;i++)
  56.     {
  57.         words->str[i]->characters=(wchar_t*)malloc(BASE_WORD_LENGHT*sizeof(wchar_t));
  58.     }
  59.     *count_words+=BASE_WORD_COUNT;
  60. }
  61.  
  62. void more_memory_struct(struct Text *text, int *count_struct)//Выделение памяти под Текст, способно увеличивать количество предложений, выделяет базовое количество символов в предложении.
  63. {
  64.     int i = 0;
  65.     text->all_str=(struct Sentence**)realloc(text->all_str,(*count_struct+BASE_COUNT_STRUCTURE)*sizeof(struct Sentence*));
  66.     for (i=*count_struct;i<((*count_struct)+BASE_COUNT_STRUCTURE);i++)
  67.     {
  68.         text->all_str[i]=(struct Sentence*)malloc(sizeof(struct Sentence));
  69.     }
  70.     for (i=*count_struct;i<((*count_struct)+BASE_COUNT_STRUCTURE);i++)
  71.     {
  72.         (text->all_str[i])->str=(wchar_t*)malloc(BASE_LENGHT_STR*sizeof(wchar_t));
  73.     }
  74.     *count_struct+=BASE_COUNT_STRUCTURE;
  75. }
  76.  
  77.  
  78. void more_memory_sentence(struct Sentence *sentence,int* lenght_str)//Выделение памяти по Предложения, увеличивает количество памяти под каждое.
  79. {
  80.     sentence->str=(wchar_t*)realloc(sentence->str,(*lenght_str+BASE_LENGHT_STR)*sizeof(wchar_t));
  81.     *lenght_str+=BASE_LENGHT_STR;
  82. }
  83.  
  84.  
  85. int compare_latin_count (const void ** num1, const void ** num2)
  86. {
  87.   if ( (**(struct Sentence**)num1).latin_count <  (**(struct Sentence**)num2).latin_count ) return -1;
  88.   if ( (**(struct Sentence**)num1).latin_count == (**(struct Sentence**)num2).latin_count ) return 0;
  89.   if ( (**(struct Sentence**)num1).latin_count >  (**(struct Sentence**)num2).latin_count ) return 1;
  90. }
  91.  
  92. int found_upper_reg_and_special_sym(struct Sentence *sentence)
  93. {
  94.     int i;
  95.     int flag_special_symbol = 0;
  96.     int flag_upper_reg = 0;
  97.     for (i=0;i<sentence->lenght;i++)
  98.     {
  99.         if (((sentence->str[i]>'A') && (sentence->str[i]<'Z')) || ((sentence->str[i]>'А') || (sentence->str[i]<'Я')))
  100.             flag_upper_reg=1;
  101.         if (iswalnum(sentence->str[i])==0)
  102.             flag_special_symbol=1;
  103.     }
  104.     if (flag_special_symbol>flag_upper_reg)
  105.         return 1;
  106.     else
  107.         return 0;
  108. }
  109.  
  110. int compare(const wchar_t* str1,const wchar_t* str2)//Компаратор, сравнивает посимвольно две строки без учета регистра, подойдет и для слов.
  111. {
  112.     int i;
  113.     if (wcslen(str1) != wcslen(str2))
  114.         return 0;
  115.     else
  116.     {
  117.         for (i=0;i<wcslen(str1);i++)
  118.             if (towlower(str1[i])!=towlower(str2[i]))
  119.                 return 0;
  120.     }
  121.     return 1;
  122. }
  123.  
  124. void delete_repeating_sentence(struct Text *text, int num)//Удаление повторяющегося предложение, переданного функцией find_repeating_sentence.
  125. {
  126.     int i;
  127.     free (text->all_str[num]);
  128.     for (i=num;i<(text->lenght)-1;i++)
  129.     {
  130.         text->all_str[i]=text->all_str[i+1];
  131.     }
  132.     text->all_str[text->lenght]= NULL;
  133.     text->lenght-=1;
  134. }
  135.  
  136. void find_repeating_sentence(struct Text *text)//Поиск повторящихся предложений.
  137. {
  138.     int i,j,g;
  139.     for (i=0;i<((text->lenght)-1);i++)
  140.     {
  141.         for (j=(i+1);j<(text->lenght);j++)
  142.         {
  143.             if (compare(text->all_str[i]->str,text->all_str[j]->str))
  144.             {
  145.                 delete_repeating_sentence(text, j);
  146.                 j--;
  147.             }
  148.         }
  149.     }
  150. }
  151.  
  152.  
  153. int input_text(struct Text *text, int* count_struct)//Считывание текста, считывает посимвольно, пока не встретит знак переноса строки, начинает записывать в новое предложение, когда встречает точку. ЗАЩИТЫ ОТ КРИВОРУКОСТИ ПОЛЬЗОВАТЕЛЯ НЕТ ПОКА ЧТО.
  154. {
  155.  
  156.     int i = 0;
  157.     int line = 0;
  158.     int current_lenght_str = 0;
  159.     int flag_first_symbol = 0;
  160.     int lenght_str=BASE_LENGHT_STR;
  161.     wchar_t symbol;
  162.     symbol=getwchar();
  163.     while (symbol!='\n')
  164.     {
  165.         if (symbol!=' ')
  166.             flag_first_symbol=1;
  167.         if (flag_first_symbol)
  168.         {
  169.             Current_symbol=symbol;
  170.             if (i==(lenght_str-1))
  171.                 more_memory_sentence(text->all_str[line],&lenght_str);
  172.             i+=1;
  173.             if (symbol=='.')
  174.             {
  175.                 Current_symbol='\0';
  176.                 text->all_str[line]->lenght=i;
  177.                 line+=1;
  178.                 i=0;
  179.                 lenght_str=BASE_LENGHT_STR;
  180.                 flag_first_symbol = 0;
  181.             }
  182.         }
  183.         symbol=getwchar();
  184.         if (line == *count_struct)
  185.             more_memory_struct(text, count_struct);
  186.  
  187.     }
  188.     text->lenght=line;
  189. }
  190.  
  191.  
  192.  
  193. void print_words_with_count(struct Text *text)//проблема с этой функцией
  194. //она должна создавать временный массив длиной равной текущему предложению, записывать туда его. Делать wcstok
  195. //а дальше в переменной current_word хранить текущее слово, сравнивая его с уже записанными ранее. в случа если слово уже было
  196. // должно добавить 1 к его количеству, если не было, записать его с количеством 1.
  197. // сейчас я сделал дебажный вывод, который показывает: сначала строку которая подается на вход и ее длину, потом
  198. // либо показывает какое новое слово было записано, либо если были сравнение показывает пары сравниваемых слов
  199. // если  было найдено соответствие, то пишет слово и знак +.
  200. // на некоторых тестах происходит дичь.
  201. // На тесте q Q a a. q a. t. t. откуда-то берет букву а после t.
  202. // в тесте q q q q w. e w w. r. вообще появляются какие-то неизведанные символы.
  203. //Во втором тесте при первом копировании строки в временный массив первое предложение копирует как q q q q w.\x1db61
  204. // хз что это за дичь
  205. {
  206.     int line,i,num;
  207.     wchar_t *current_word;
  208.     int space_for_word = BASE_WORD_LENGHT;
  209.     int flag_word_detected = 0;
  210.     struct Words words = {NULL, 0};
  211.     int count_words= 0;
  212.     wchar_t *check;
  213.     wchar_t *state;
  214.     wchar_t *temp_sentence = NULL;
  215.     words.count=0;
  216.     more_memory_words(&words, &count_words);
  217.     for (line=0;line<(text->lenght);line++)
  218.     {
  219.         //wprintf(L"%ls %d\n",text->all_str[line]->str, (text->all_str[line]->lenght));
  220.         temp_sentence = NULL;
  221.         temp_sentence = (wchar_t*)realloc(temp_sentence,text->all_str[line]->lenght * sizeof(wchar_t));
  222.         wcsncpy(temp_sentence,text->all_str[line]->str,((text->all_str[line]->lenght)+1));// здесь
  223.        // wprintf(L"%ls\n",temp_sentence);
  224.         current_word = wcstok(temp_sentence,L" .,",&state);
  225.         while (current_word!= NULL)
  226.         {
  227.             flag_word_detected = 0;
  228.             for (num = 0; num < words.count; num++)
  229.             {
  230.                 //wprintf(L"|%ls| %ls|   ",current_word,words.str[num]->characters);
  231.                 if (compare(current_word, words.str[num]->characters)==1)
  232.                 {
  233.                     words.str[num]->count += 1;
  234.                     flag_word_detected = 1;
  235.                     //wprintf(L"%ls +\n",current_word);
  236.                 }
  237.             }
  238.             if (flag_word_detected==0)
  239.             {
  240.                 while (space_for_word < wcslen(current_word))
  241.                     more_memory_word(words.str[words.count], &space_for_word);
  242.                 //wprintf(L"%ls  1",current_word);
  243.                  wcsncpy(words.str[words.count]->characters,current_word,(wcslen(current_word)+1));
  244.                
  245.                 //wprintf(L"%ls  2\n",current_word);
  246.                 words.str[words.count]->count = 1;
  247.                 space_for_word = BASE_WORD_LENGHT;
  248.                 words.count+=1;
  249.                 //wprintf(L"новое слово %ls",current_word);
  250.                 //wprintf(L"\n");
  251.                 if (words.count==count_words)
  252.                 {
  253.                   more_memory_words(&words, &count_words);  
  254.                 }
  255.             }
  256.  
  257.             current_word = wcstok(NULL,L" .,",&state);
  258.            
  259.         }
  260.        
  261.         free (temp_sentence);
  262.     }
  263.     //wprintf(L"\n");
  264.     for (i=0; i<words.count;i++)
  265.     {
  266.         wprintf(L"%ls %d\n", words.str[i]->characters, words.str[i]->count);
  267.     }
  268.  
  269. }
  270.  
  271. int count_latin_symbol(struct Sentence *sentence)
  272. {
  273.   int i,count;
  274.   count=0;
  275.   for (i=0;i<(sentence->lenght);i++)
  276.   {
  277.       if ((towlower(sentence->str[i])<='z') && (towlower(sentence->str[i])>='a'))
  278.       count+=1;
  279.      
  280.   }
  281.   return count;
  282. }
  283.  
  284. void sort_by_lalin_count(struct Text *text)
  285. {
  286.     int line;
  287.     for (line=0;line<(text->lenght);line++)
  288.     {
  289.         text->all_str[line]->latin_count=count_latin_symbol(text->all_str[line]);
  290.     }
  291.     qsort(text->all_str,text->lenght,sizeof(struct Sentence*),compare_latin_count);
  292. }
  293.  
  294. void delete_specsym_and_noupsym_sentence(struct Text *text)
  295. {
  296.     int i;
  297.     for (i=0;i<text->lenght;i++)
  298.     {
  299.         if (found_upper_reg_and_special_sym(text->all_str[i])==1)
  300.             delete_repeating_sentence(text->all_str, i);
  301.     }
  302. }
  303.  
  304.  
  305.  
  306.  
  307. int main()
  308. {
  309.     int i=0;
  310.     setlocale(LC_CTYPE, "");
  311.     int count_struct = 0;
  312.     struct Text text = {NULL, 0};
  313.     more_memory_struct(&text, &count_struct );
  314.     input_text(&text, &count_struct);
  315.     find_repeating_sentence(&text);
  316.     for (i=0; i<(text.lenght);i++)
  317.     wprintf(L"%ls %d", text.all_str[i]->str, text.all_str[i]->lenght);
  318.     wprintf(L"\n");    
  319.     print_words_with_count(&text);
  320.     //sort_by_lalin_count(&text);
  321.     for (i=0; i<(text.lenght);i++)
  322.     wprintf(L"%ls %d", text.all_str[i]->str, text.all_str[i]->lenght);
  323.     wprintf(L"\n");  
  324.     return 0;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement