Advertisement
maxim_shlyahtin

read

Nov 17th, 2022 (edited)
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wctype.h>
  4. #include <wchar.h>
  5. #include <locale.h>
  6. #include <time.h>
  7.  
  8. #define BUFF_SIZE 200
  9. #define MEM_STEP 20
  10. struct Sentence {
  11.     wchar_t *str;
  12.     int length;
  13. };
  14.  
  15. struct Text {
  16.     struct Sentence **text;
  17.     int length;
  18. };
  19.  
  20. struct SubTime {
  21.     double time_dif;
  22.     int sent_numb;
  23. };
  24.  
  25. typedef struct Sentence Sentence;
  26. typedef struct Text Text;
  27. typedef struct SubTime SubTime;
  28.  
  29. Sentence *readSentence() {
  30.     wchar_t c;
  31.     int length = 0;
  32.     int size = MEM_STEP;
  33.     wchar_t *str;
  34.     str = (wchar_t *) malloc(size * sizeof(wchar_t));
  35.     do {
  36.         if (length >= size - 2) {
  37.             wchar_t *tmp;
  38.             size += MEM_STEP;
  39.             tmp = (wchar_t *) realloc(str, size * sizeof(wchar_t));
  40.             if (tmp == NULL) {
  41.                 free(str);
  42.                 return NULL;
  43.             }
  44.             str = tmp;
  45.         }
  46.         c = (wchar_t) getwchar();
  47.         str[length++] = c;
  48.     } while (c != L'.' && c != L'!' && c != L'?' && c != L'\n');
  49.     str[++length] = L'\0';
  50.     Sentence *sentence = (Sentence *) malloc(sizeof(Sentence));
  51.     sentence->str = str;
  52.     sentence->length = length;
  53.     return sentence;
  54. }
  55.  
  56.  
  57. Sentence *fix_of_sentence(Sentence *sentence) {
  58.     if (sentence->str[0] == '\t' || sentence->str[0] == ' ' || sentence->str[0] == '\n' || sentence->str[0] == '.') {
  59.         for (int i = 0; i <= sentence->length; i++)
  60.             sentence->str[i] = sentence->str[i + 1];
  61.         sentence->length--;
  62.     }
  63.     return sentence;
  64. }
  65.  
  66.  
  67. Text readText() {
  68.     int size = MEM_STEP;
  69.     int length = 0;
  70.     int nlcount = 0;
  71.     Sentence **text = (Sentence **)malloc(size * sizeof(Sentence *));
  72.     Sentence *sentence;
  73.     do {
  74.         sentence = readSentence();
  75.         sentence->length != 1 ? fix_of_sentence(sentence) : sentence;
  76.         if (sentence->str[0] == L'\n') {
  77.             nlcount++;
  78.             free(sentence->str);
  79.             free(sentence);
  80.         } else {
  81.             if (length == size - 4) {
  82.                 size += MEM_STEP;
  83.                 text = (Sentence **)realloc(text, size * sizeof(Sentence *));
  84.                 if (text == NULL) {
  85.                     for (int i = 0; i < length; i++) {
  86.                         free(text[i]->str);
  87.                         free(text[i]);
  88.                     }
  89.                     exit(0);
  90.                 }
  91.             }
  92.             nlcount = 0;
  93.             text[length++] = sentence;
  94.         }
  95.     } while (nlcount < 2);
  96.     Text txt;
  97.     txt.text = text;
  98.     txt.length = length;
  99.     return txt;
  100. }
  101.  
  102.  
  103. SubTime *check_time(Text txt, struct tm timeinfo, int *len) {
  104.     int size = MEM_STEP, fl;
  105.     SubTime *times = (SubTime *) malloc(size * sizeof(SubTime *));
  106.     SubTime time_stamp;
  107.     struct tm tmp = timeinfo;
  108.     for (int i = 0; i < txt.length; i++) {
  109.         fl = 0;
  110.         for (int j = 0; j < txt.text[i]->length - 5; j++) {
  111.             if (iswdigit(txt.text[i]->str[j]) && iswdigit(txt.text[i]->str[j + 1]) && iswdigit(txt.text[i]->str[j + 3]) &&
  112.                 iswdigit(txt.text[i]->str[j + 4]) && txt.text[i]->str[j + 2] == L':'){
  113.                 wchar_t *p_end;
  114.                 wchar_t hours[2] = {txt.text[i]->str[j], txt.text[i]->str[j + 1]};
  115.                 tmp.tm_hour = (int)wcstol(hours, &p_end, 10);
  116.                 wchar_t minutes[2] = {txt.text[i]->str[j + 3], txt.text[i]->str[j + 4]};
  117.                 tmp.tm_min = (int)wcstol(minutes, &p_end, 10);
  118.                 double seconds = abs((int)difftime(mktime(&timeinfo), mktime(&tmp)));
  119.                 time_stamp.time_dif = seconds;
  120.                 time_stamp.sent_numb = i;
  121.                 fl = 1;
  122.             }
  123.         }
  124.         if(fl) {
  125.             times[*len] = time_stamp;
  126.             ++*len;
  127.         }
  128.     }
  129.     return times;
  130. }
  131.  
  132. void printSubTime(SubTime *times, int len){
  133.     for(int i = 0; i < len; i++){
  134.         wprintf(L"%lf %d\n", times[i].time_dif, times[i].sent_numb);
  135.     }
  136. }
  137.  
  138. void free_text(Text text) {
  139.     for (int i = 0; i < text.length; i++) {
  140.         free(text.text[i]->str);
  141.     }
  142.     free(text.text);
  143. }
  144.  
  145.  
  146. void print_txt(Text txt) {
  147.     for (int i = 0; i < txt.length; i++) {
  148.         wprintf(L"%ls\n", txt.text[i]->str);
  149.     }
  150. }
  151.  
  152.  
  153. Text del_copy(Text txt) {
  154.     int i, j;
  155.     int size = txt.length;
  156.     for (i = 0; i < size; i++) {
  157.         for (j = i + 1; j < size; j++) {
  158.             if (!wcscasecmp(txt.text[i]->str, txt.text[j]->str)) {
  159.                 free(txt.text[j]->str);
  160.                 free(txt.text[j]);
  161.                 txt.text[j] = txt.text[--size];
  162.                 j--;
  163.             }
  164.         }
  165.     }
  166.     txt.length = size;
  167.     return txt;
  168. }
  169.  
  170. //добавить удаление по спец символу к удалению копий
  171.  
  172. void del_caps(Text txt) {
  173.     for (int k = 0; k < txt.length; k++) {
  174.         int i = 0;
  175.         Sentence *sentence = txt.text[k];
  176.         while (sentence->str[i]) {
  177.             if (iswupper(sentence->str[i])) {
  178.                 for (int j = i; sentence->str[j]; j++)
  179.                     sentence->str[j] = sentence->str[j + 1];
  180.                 i--;
  181.             }
  182.             i++;
  183.         }
  184.         fix_of_sentence(sentence);
  185.     }
  186. }
  187.  
  188. int count_cyrillic(Sentence *sentence) {
  189.     int count = 0;
  190.     for (int i = 0; i < sentence->length; i++) {
  191.         if ((65 > (int) sentence->str[i] || (int) sentence->str[i] > 122) && iswalpha(sentence->str[i])) {
  192.             count++;
  193.         }
  194.     }
  195.     return count;
  196. }
  197.  
  198. int cmp(const void *s1, const void *s2) {
  199.     Sentence **f1 = (Sentence **) s1;
  200.     Sentence **f2 = (Sentence **) s2;
  201.     if (count_cyrillic(*f1) < count_cyrillic(*f2))
  202.         return 1;
  203.     if (count_cyrillic(*f1) > count_cyrillic(*f2))
  204.         return -1;
  205.     return 0;
  206. }
  207.  
  208.  
  209. Text del_sent_without_spec_symbols(Text txt) {
  210.     int i, j;
  211.     int size = txt.length;
  212.     for (i = 0; i < size; i++) {
  213.         int sent_size = txt.text[i]->length, fl = 0;
  214.         for (j = 0; j < sent_size; j++) {
  215.             if (iswcntrl(txt.text[i]->str[j])) {
  216.                 fl = 1;
  217.                 break;
  218.             }
  219.         }
  220.         if (fl) {
  221.             continue;
  222.         } else {
  223.             free(txt.text[i]->str);
  224.             free(txt.text[i]);
  225.             txt.text[i] = txt.text[--size];
  226.             i--;
  227.         }
  228.     }
  229.     txt.length = size;
  230.     return txt;
  231. }
  232.  
  233. void sort_txt(Text txt) {
  234.     qsort(txt.text, txt.length, sizeof(Sentence *), cmp);
  235. }
  236.  
  237. int main() {
  238.     setlocale(LC_ALL, "");
  239.     time_t rawtime;
  240.     time(&rawtime);
  241.     struct tm timeinfo;
  242.     timeinfo = *localtime (&rawtime);
  243.     Text txt = readText();
  244.     print_txt(txt);
  245. //    txt = del_copy(txt);
  246. //    del_caps(txt);
  247. //    sort_txt(txt);
  248. //    int len = 0;
  249. //    SubTime *times = check_time(txt, timeinfo, &len);
  250. ////    txt = del_sent_without_spec_symbols(txt);
  251. //    print_txt(txt);
  252. //    printSubTime(times, len);
  253.     free_text(txt);
  254.  
  255.     return 0;
  256. }
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement