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>
- #include <wctype.h>
- enum { L_bit = 8, l_bit = 4, W_bit = 2, w_bit = 1, BUF_SIZE = 100 };
- 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 *right;
- struct node *left;
- short height;
- wchar_t data;
- int quantity;
- };
- char height(struct node *p)
- {
- if (p) {
- return p->height;
- } else {
- return 0;
- }
- }
- int BF (struct node *p)
- {
- return height(p->right)-height(p->left);
- }
- void OverHeight(struct 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;
- y->quantity = 1;
- y->height = 1;
- y->left = y->right = NULL;
- return y;
- }
- if (k < x->data) {
- x->left = Insert(x->left, k);
- } else if (k == x->data) {
- x->quantity ++;
- } else {
- x->right = Insert(x->right, k);
- }
- return Balance(x);
- }
- void print_tree (struct node* root, FILE *f) {
- if (root) {
- print_tree(root->left,f);
- fprintf(f, "%lc - %d\n",root->data, root->quantity);
- print_tree(root->right,f);
- }
- }
- void Init_List (struct node* root, struct List *head) {
- if (root) {
- Init_List(root->left,head);
- addToList(root->quantity,head);
- Init_List(root->right,head);
- }
- }
- void Go_Through (struct node* root, int q, FILE *f) {
- if (root) {
- Go_Through(root->left,q,f);
- if (root->quantity == q) {
- fprintf(f,"%lc - %d\n", root->data, q);
- }
- Go_Through(root->right,q,f);
- }
- }
- void Print_On_Q (struct node* root, struct List *a, FILE *f)
- {
- struct Elem *pTemp = a->pHead;
- while (pTemp != NULL)
- {
- Go_Through(root,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");
- 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);
- }
- wint_t x;
- wchar_t c;
- 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;
- } */
- struct node *arr = NULL;
- wchar_t *buf = calloc (BUF_SIZE, sizeof(wchar_t *));
- int i = 0;
- while ((x = fgetwc(fin)) != EOF) {
- if (!isspace(x)) {
- if (iswupper(x)) {
- x = towlower(x);
- }
- c = (wchar_t) x;
- arr = Insert(arr,c);
- if (iswalpha(x)) {
- buf[i] = c;
- i++;
- } else if (i > 0) {
- i++;
- buf[i] = '\0';
- buf = realloc(buf, (i + 1) * sizeof(wchar_t));
- //printf("i = %d\n",i);
- wprintf(L".%ls.\n",buf);
- buf = calloc (BUF_SIZE, sizeof(wchar_t *));
- i = 0;
- }
- } else if (i) {
- i++;
- buf[i] = '\0';
- buf = realloc(buf, (i + 1) * sizeof(wchar_t));
- //printf("i = %d\n",i);
- wprintf(L".%ls.\n",buf);
- buf = calloc (BUF_SIZE, sizeof(wchar_t *));
- i = 0;
- }
- }
- if (i != 0) {
- printf("%d\n",i);
- i++;
- buf[i] = '\0';
- buf = realloc(buf, (i + 1) * sizeof(wchar_t));
- //printf("i = %d\n",i);
- wprintf(L"%ls\n",buf);
- buf = calloc (BUF_SIZE, sizeof(wchar_t *));
- i = 0;
- }
- /*
- Init_List(arr,&a);
- print_tree(arr,fout);
- Print_On_Q (arr,&a,fout);
- 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_tree(arr,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) {
- fprintf(fout,"\nChar tree by quantity:\n\n");
- Init_List(arr,&a);
- Print_On_Q (arr,&a,fout);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement