Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <locale.h>
- #include <wchar.h>
- enum { L_bit = 8, l_bit = 4, W_bit = 2, w_bit = 1 };
- struct Elem
- {
- int data;
- struct Elem *next;
- };
- struct List
- {
- struct Elem *pHead;
- struct Elem *pTail;
- };
- struct List * MakeList(void)
- {
- struct List *a = calloc (1, sizeof(struct List));
- a->pHead = NULL;
- return a;
- }
- void addToList(int data, struct List *a)
- {
- struct Elem *temp = calloc(1, sizeof(struct Elem));
- struct Elem *cnt;
- if (a->pHead == NULL) {
- a->pHead = temp;
- temp->data = data;
- temp->next = NULL;
- a->pTail = temp;
- } else {
- if (a->pHead->data < data) {
- temp->data = data;
- temp->next = a->pHead;
- a->pHead = temp;
- } else if (a->pHead->data > data) {
- cnt = a->pHead;
- while (cnt->next && (cnt->next->data > data)) {
- cnt = cnt->next;
- }
- if (!(cnt->next)) {
- a->pTail->next = temp;
- temp->data = data;
- temp->next = NULL;
- a->pTail = temp;
- } else if (cnt->next->data < data) {
- temp->next = cnt->next;
- cnt->next = temp;
- temp->data = data;
- }
- }
- }
- }
- struct node
- {
- struct node * link[2];
- short bal;
- wchar_t data;
- long q;
- };
- struct node
- {
- int key;
- char height;
- Node *right;
- Node *left;
- Node(int k) { key=k; height=1; left=right=0; }
- };
- char height(Node *p)
- {
- if (p) return p->height;
- else return 0;
- }
- int BF(Node *p)
- { return height(p->right)-height(p->left); }
- void OverHeight(Node *p)
- {
- char hleft=height(p->left);
- char hright=height(p->right);
- p->height=(hleft>hright ? hleft : hright)+1;
- }
- struct node* RightRotation(struct node *x)
- {
- struct node *y=x->left;
- x->left=y->right;
- y->right=x;
- OverHeight(x);
- OverHeight(y);
- return y;
- }
- struct node *LeftRotation(struct node *y)
- {
- struct node *x=y->right;
- y->right=x->left;
- x->left=y;
- OverHeight(y);
- OverHeight(x);
- return x;
- }
- struct node *Balance(struct node *x)
- {
- OverHeight(x);
- if (BF(x) == 2) {
- if (BF(x->right)<0) x->right=RightRotation(x->right);
- return LeftRotation(x);
- }
- if (BF(x) == -2) {
- if (BF(x->left)>0) x->left=LeftRotation(x->left);
- return RightRotation(x);
- }
- return x;
- }
- struct node *Insert(struct node *x, wchar_t k)
- {
- if (!x) {
- struct node *y = calloc(1,sizeof(struct node));
- y->data = k;
- return y;
- }
- if (k < x->key) {
- x->left = Insert(x->left, k);
- } else {
- x->right=Insert(x->right, k);
- }
- return Balance(x);
- }
- /*
- void Init_List (struct node* root, struct List *head) {
- if (root) {
- Init_List(root->left,head);
- addToList(root->q,head);
- Init_List(root->right,head);
- }
- }
- void Go_Through (struct node* root, int type, int q, FILE *f) {
- if (root) {
- Go_Through(root->left,type,q,f);
- if (root->q == q) {
- if (type) {
- fprintf(f,"%c - %d\n", root->x, root -> q);
- } else {
- fprintf(f,"%s - %d\n", root->s, root -> q);
- }
- }
- Go_Through(root->right,type,q,f);
- }
- }
- void Print_on_q (struct node* root, int type, struct List *a, FILE *f)
- {
- struct Elem *pTemp = a->pHead;
- while (pTemp != NULL)
- {
- Go_Through(root,type,pTemp->data,f);
- pTemp = pTemp->next;
- }
- } */
- int correct (int flags)
- {
- return (!((( (flags & 8) > 0) && ( (flags & 4) > 0)) ||
- (( (flags & 2) > 0) && ( (flags & 1) > 0))));
- }
- /* In flags bytes corresponding to lay as foolows: LlWw,
- 1 if exists, 0 if doesn't. For example if we met
- lWL we'll try to write 14 = 1110 in binary.
- As it is innapropriate for our programm we'll check it
- with correct(int) function. */
- FILE * flags (int argc, char *argv[], int * flags)
- {
- int out = 0;
- FILE *fout;
- int i = 2;
- for (; i < argc; i++) {
- if (argv[i][0] == '-') {
- int l = strlen(argv[i]);
- for (int j = 1; j < l; j++) {
- if (argv[i][j] == 'o') {
- fout = fopen(argv[i + 1],"w");
- if (!fout) {
- fprintf(stderr,"Error opening output file.\n");
- return NULL;
- }
- } else if (argv[i][j] == 'L') {
- out = out | L_bit;
- } else if (argv[i][j] == 'l') {
- out = out | l_bit;
- } else if (argv[i][j] == 'W') {
- out = out | W_bit;
- } else if (argv[i][j] == 'w') {
- out = out | w_bit;
- }
- }
- }
- }
- if (!correct(out)) {
- fprintf(stderr,"Error with programm flags.\n");
- return NULL;
- }
- (*flags) = out;
- return fout;
- }
- int letter (wchar_t c)
- {
- return ((((c>='A') && (c<='Z')) || ((c>='a') && (c<='z')))
- || (((c >= 1040) && (c <= 1071)) || ((c >= 1072) && (c <= 1103))));
- }
- int main (int argc, char *argv[])
- {
- FILE *fin = fopen(argv[1],"r");
- wchar_t c;
- wint_t x;
- setlocale(LC_ALL, "");
- if (!fin) {
- fprintf(stderr,"Error opening input file.\n");
- exit(1);
- }
- int flag = 0;
- FILE *fout = flags(argc,argv,&flag);
- if (!fout) {
- exit(1);
- }
- int x;
- struct List a;
- a.pHead = NULL;
- /*
- for (int i = 0; i < 25; i++)
- {
- scanf("%d",&x);
- addToList(x,&a);
- }
- struct Elem *pTemp = a.pHead;
- while (pTemp != NULL)
- {
- printf("%d ",pTemp->data);
- pTemp = pTemp->next;
- } */
- while ((x = fgetwc(stdin)) != EOF) {
- if (!isspace(x)) {
- }
- }
- /*
- if ((flag & w_bit) == w_bit) {
- fprintf(fout,"Strings tree by lexics:\n\n");
- print(STree,0,fout);
- }
- if ((flag & l_bit) == l_bit) {
- fprintf(fout,"\nChar tree by lexics:\n\n");
- print(CTree,1,fout);
- }
- if ((flag & W_bit) == W_bit) {
- struct List *slist = MakeList();
- fprintf(fout,"\nStrings tree by quantity:\n\n");
- Init_List(STree,slist);
- Print_on_q(STree,0,slist,fout);
- }
- if ((flag & L_bit) == L_bit) {
- struct List *wlist = MakeList();
- fprintf(fout,"\nChar tree by quantity:\n\n");
- Init_List(CTree,wlist);
- Print_on_q(CTree,1,wlist,fout);
- } */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement