Advertisement
Guest User

Untitled

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