Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <locale.h>
  6. #include <wchar.h>
  7. #include <wctype.h>
  8.  
  9. enum { L_bit = 8, l_bit = 4, W_bit = 2, w_bit = 1, BUF_SIZE = 100 };
  10.  
  11. struct Elem
  12. {
  13.     int data;
  14.     struct Elem *next;
  15. };
  16.  
  17. struct List
  18. {
  19.     struct Elem *pHead;
  20.     struct Elem *pTail;
  21. };
  22.  
  23. struct List * MakeList(void)
  24. {
  25.     struct List *a = calloc (1, sizeof(struct List));
  26.     a->pHead = NULL;
  27.     return a;
  28. }
  29.  
  30. void addToList(int data, struct List *a)
  31. {
  32.     struct Elem *temp = calloc(1, sizeof(struct Elem));
  33.     struct Elem *cnt;
  34.     if (a->pHead == NULL) {
  35.         a->pHead = temp;
  36.         temp->data = data;
  37.         temp->next = NULL;
  38.         a->pTail = temp;
  39.     } else {
  40.         if (a->pHead->data < data) {
  41.             temp->data = data;
  42.             temp->next = a->pHead;
  43.             a->pHead = temp;
  44.         } else if (a->pHead->data > data) {
  45.             cnt = a->pHead;
  46.             while (cnt->next && (cnt->next->data > data)) {
  47.                 cnt = cnt->next;
  48.             }
  49.             if (!(cnt->next)) {
  50.                 a->pTail->next = temp;
  51.                 temp->data = data;
  52.                 temp->next = NULL;
  53.                 a->pTail = temp;
  54.             } else if (cnt->next->data < data) {
  55.                 temp->next = cnt->next;
  56.                 cnt->next = temp;
  57.                 temp->data = data;
  58.             }
  59.         }
  60.  
  61.     }
  62. }
  63.  
  64. struct node
  65. {
  66.     struct node *right;
  67.     struct node *left;
  68.     short height;
  69.     wchar_t data;
  70.     int quantity;
  71. };
  72.  
  73. char height(struct node *p)
  74. {
  75.     if (p) {
  76.         return p->height;
  77.     } else {
  78.         return 0;
  79.     }
  80. }
  81.  
  82. int BF (struct node *p)
  83. {
  84.     return height(p->right)-height(p->left);
  85. }
  86.  
  87. void OverHeight(struct node *p)
  88. {
  89.     char hleft = height(p->left);
  90.     char hright = height(p->right);
  91.     p->height = (hleft > hright ? hleft : hright) + 1;
  92. }
  93.  
  94. struct node* RightRotation(struct node *x)
  95. {
  96.     struct node *y = x->left;
  97.     x->left = y->right;
  98.     y->right = x;
  99.     OverHeight(x);
  100.     OverHeight(y);
  101.     return y;
  102. }
  103. struct node *LeftRotation(struct node *y)
  104. {
  105.     struct node *x = y->right;
  106.     y->right = x->left;
  107.     x->left = y;
  108.     OverHeight(y);
  109.     OverHeight(x);
  110.     return x;
  111. }
  112.  
  113. struct node *Balance(struct node *x)
  114. {
  115.     OverHeight(x);
  116.     if (BF(x) == 2) {
  117.         if (BF(x->right) < 0)
  118.         {
  119.             x->right = RightRotation(x->right);
  120.         }
  121.         return LeftRotation(x);
  122.     }
  123.     if (BF(x) == -2) {
  124.         if (BF(x->left) > 0) {
  125.             x->left = LeftRotation(x->left);
  126.         }
  127.         return RightRotation(x);
  128.     }
  129.     return x;
  130. }
  131.  
  132. struct node *Insert(struct node *x, wchar_t k)
  133. {
  134.     if (!x) {
  135.         struct node *y = calloc(1,sizeof(struct node));
  136.         y->data = k;
  137.         y->quantity = 1;
  138.         y->height = 1;
  139.         y->left = y->right = NULL;
  140.         return y;
  141.     }
  142.     if (k < x->data) {
  143.         x->left = Insert(x->left, k);
  144.     } else if (k == x->data) {
  145.         x->quantity ++;
  146.     } else {
  147.         x->right = Insert(x->right, k);
  148.     }
  149.     return Balance(x);
  150. }
  151.  
  152. void print_tree (struct node* root, FILE *f) {
  153.     if (root) {
  154.         print_tree(root->left,f);
  155.         fprintf(f, "%lc  - %d\n",root->data, root->quantity);
  156.         print_tree(root->right,f);
  157.     }
  158. }
  159.  
  160. void Init_List (struct node* root, struct List *head) {
  161.     if (root) {
  162.         Init_List(root->left,head);
  163.         addToList(root->quantity,head);
  164.         Init_List(root->right,head);
  165.     }
  166. }
  167.  
  168. void Go_Through (struct node* root, int q, FILE *f) {
  169.     if (root) {
  170.         Go_Through(root->left,q,f);
  171.         if (root->quantity == q) {
  172.             fprintf(f,"%lc - %d\n", root->data, q);
  173.         }
  174.         Go_Through(root->right,q,f);
  175.     }
  176. }
  177.  
  178. void Print_On_Q (struct node* root, struct List *a, FILE *f)
  179. {
  180.     struct Elem *pTemp = a->pHead;
  181.     while (pTemp != NULL)
  182.     {
  183.         Go_Through(root,pTemp->data,f);
  184.         pTemp = pTemp->next;
  185.     }
  186. }
  187.  
  188. int correct (int flags)
  189. {
  190.     return (!((( (flags & 8) > 0) && ( (flags & 4) > 0)) ||
  191.               (( (flags & 2) > 0) && ( (flags & 1) > 0))));
  192. }
  193.  
  194. /* In flags bytes corresponding to lay as foolows: LlWw,
  195.     1 if exists, 0 if doesn't. For example if we met
  196.     lWL we'll try to write 14 = 1110 in binary.
  197.     As it is innapropriate for our programm we'll check it
  198.     with correct(int) function. */
  199. FILE * flags (int argc, char *argv[], int * flags)
  200. {
  201.     int out = 0;
  202.     FILE *fout;
  203.     int i = 2;
  204.     for (; i < argc; i++) {
  205.         if (argv[i][0] == '-') {
  206.             int l = strlen(argv[i]);
  207.             for (int j = 1; j < l; j++) {
  208.                 if (argv[i][j] == 'o') {
  209.                     fout = fopen(argv[i + 1],"w");
  210.                     if (!fout) {
  211.                         fprintf(stderr,"Error opening output file.\n");
  212.                         return NULL;
  213.                     }
  214.                 } else if (argv[i][j] == 'L') {
  215.                     out = out | L_bit;
  216.                 } else if (argv[i][j] == 'l') {
  217.                     out = out | l_bit;
  218.                 } else if (argv[i][j] == 'W') {
  219.                     out = out | W_bit;
  220.                 } else if (argv[i][j] == 'w') {
  221.                     out = out | w_bit;
  222.                 }
  223.             }
  224.         }
  225.     }
  226.     if (!correct(out)) {
  227.         fprintf(stderr,"Error with programm flags.\n");
  228.         return NULL;
  229.     }
  230.     (*flags) = out;
  231.     return fout;
  232. }
  233.  
  234. int letter (wchar_t c)
  235. {
  236.     return ((((c>='A') && (c<='Z')) || ((c>='a') && (c<='z')))
  237.             || (((c >= 1040) && (c <= 1071)) || ((c >= 1072) && (c <= 1103))));
  238. }
  239.  
  240. int main (int argc, char *argv[])
  241. {
  242.     FILE *fin = fopen(argv[1],"r");
  243.     setlocale(LC_ALL, "");
  244.     if (!fin) {
  245.         fprintf(stderr,"Error opening input file.\n");
  246.         exit(1);
  247.     }
  248.     int flag = 0;
  249.     FILE *fout = flags(argc,argv,&flag);
  250.     if (!fout) {
  251.         exit(1);
  252.     }
  253.     wint_t x;
  254.     wchar_t c;
  255.     struct List a;
  256.     a.pHead = NULL;
  257.     /*
  258.     for (int i = 0; i < 25; i++)
  259.     {
  260.         scanf("%d",&x);
  261.         addToList(x,&a);
  262.     }
  263.     struct Elem *pTemp = a.pHead;
  264.     while (pTemp != NULL)
  265.     {
  266.         printf("%d ",pTemp->data);
  267.         pTemp = pTemp->next;
  268.     } */
  269.     struct node *arr = NULL;
  270.     wchar_t *buf = calloc (BUF_SIZE, sizeof(wchar_t *));
  271.     int i = 0;
  272.  
  273.     while ((x = fgetwc(fin)) != EOF) {
  274.         if (!isspace(x)) {
  275.             if (iswupper(x)) {
  276.                 x = towlower(x);
  277.             }
  278.             c = (wchar_t) x;
  279.             arr = Insert(arr,c);
  280.             if (iswalpha(x)) {
  281.                 buf[i] = c;
  282.                 i++;
  283.             } else if (i > 0) {
  284.                 i++;
  285.                 buf[i] = '\0';
  286.                 buf = realloc(buf, (i + 1) * sizeof(wchar_t));
  287.                 //printf("i = %d\n",i);
  288.                 wprintf(L".%ls.\n",buf);
  289.                 buf = calloc (BUF_SIZE, sizeof(wchar_t *));
  290.                 i = 0;
  291.             }
  292.         } else if (i) {
  293.             i++;
  294.             buf[i] = '\0';
  295.             buf = realloc(buf, (i + 1) * sizeof(wchar_t));
  296.             //printf("i = %d\n",i);
  297.             wprintf(L".%ls.\n",buf);
  298.             buf = calloc (BUF_SIZE, sizeof(wchar_t *));
  299.             i = 0;
  300.         }
  301.     }
  302.     if (i != 0) {
  303.         printf("%d\n",i);
  304.         i++;
  305.         buf[i] = '\0';
  306.         buf = realloc(buf, (i + 1) * sizeof(wchar_t));
  307.         //printf("i = %d\n",i);
  308.         wprintf(L"%ls\n",buf);
  309.         buf = calloc (BUF_SIZE, sizeof(wchar_t *));
  310.         i = 0;
  311.     }
  312.     /*
  313.     Init_List(arr,&a);
  314.     print_tree(arr,fout);
  315.     Print_On_Q (arr,&a,fout);
  316.    
  317.     if ((flag & w_bit) == w_bit) {
  318.         fprintf(fout,"Strings tree by lexics:\n\n");
  319.         print(STree,0,fout);
  320.     } */
  321.     if ((flag & l_bit) == l_bit) {
  322.         fprintf(fout,"\nChar tree by lexics:\n\n");
  323.         print_tree(arr,fout);
  324.     } /*
  325.     if ((flag & W_bit) == W_bit) {
  326.         struct List *slist = MakeList();
  327.         fprintf(fout,"\nStrings tree by quantity:\n\n");
  328.         Init_List(STree,slist);
  329.         Print_on_q(STree,0,slist,fout);
  330.     } */
  331.     if ((flag & L_bit) == L_bit) {
  332.         fprintf(fout,"\nChar tree by quantity:\n\n");
  333.         Init_List(arr,&a);
  334.         Print_On_Q (arr,&a,fout);
  335.     }
  336.     return 0;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement