Advertisement
maxim_shlyahtin

cw

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