Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct elem * root;
- struct elem {
- int balance,v;
- char k[1000];
- struct elem * parent;
- struct elem * left;
- struct elem * right;
- };
- struct elem * Init(char * k,int balance,int v) {
- struct elem * a = (struct elem*)malloc(sizeof(struct elem));
- strcpy(a->k,k);
- a->parent = NULL;
- a->left = NULL;
- a->right = NULL;
- a->v = v;
- a->balance = balance;
- return a;
- }
- void Replace(struct elem * x,struct elem * y) {
- if (x == root) {
- root = y;
- y->parent = NULL;
- }
- else {
- struct elem * parent = x->parent;
- if (y != NULL) y->parent = parent;
- if (parent->left == x) parent->left = y;
- else parent->right = y;
- }
- }
- struct elem * Search(char * k) {
- struct elem * el = root;
- while((el != NULL) && (strcmp(el->k,k) != 0)) {
- if (strcmp(k,el->k) < 0) el = el->left;
- else el = el->right;
- }
- return el;
- }
- struct elem * RotateLeft(struct elem * x) {
- struct elem * y = x->right;
- Replace(x,y);
- struct elem * b = y->left;
- if (b != NULL) {
- b->parent = x;
- }
- x->right = b;
- x->parent = y;
- y->left = x;
- --x->balance;
- if (y->balance > 0) x->balance -= y->balance;
- --y->balance;
- if (x->balance < 0) y->balance += x->balance;
- }
- struct elem * RotateRight(struct elem * x) {
- struct elem * y = x->left;
- Replace(x,y);
- struct elem * b = y->right;
- if (b != NULL) {
- b->parent = x;
- }
- x->left = b;
- x->parent = y;
- y->right = x;
- ++x->balance;
- if (y->balance < 0) x->balance -= y->balance;
- ++y->balance;
- if (x->balance > 0) y->balance += x->balance;
- }
- struct elem * Insert(int v,char * k) {
- struct elem * elem = Init(k,0,v);
- if (root == NULL) {
- root = elem;
- return 1;
- }
- struct elem * lm = root;
- for(;;) {
- if (strcmp(k,lm->k) == 0) {
- free(elem);
- return 0;
- }
- if (strcmp(k,lm->k) < 0) {
- if (lm->left == NULL) {
- lm->left = elem;
- elem->parent = lm;
- break;
- }
- else lm = lm->left;
- }
- else {
- if (lm->right == NULL) {
- lm->right = elem;
- elem->parent = lm;
- break;
- }
- else lm = lm->right;
- }
- }
- return 1;
- }
- struct elem * InsertAVL(int v,char * k) {
- struct elem * a;
- int t = Insert(v,k);
- if (t == 0) {
- return 0;
- }
- else {
- a = Search(k);
- }
- for(;;) {
- struct elem * x = a->parent;
- if (!x) return 1;
- if (a == x->left) {
- --x->balance;
- if (x->balance == 0) break;
- if (x->balance == -2) {
- if (a->balance == 1) {
- RotateLeft(a);
- }
- RotateRight(x);
- break;
- }
- }
- else {
- ++x->balance;
- if (x->balance == 0) break;
- if (x->balance == 2) {
- if (a->balance == -1) {
- RotateRight(a);
- }
- RotateLeft(x);
- break;
- }
- }
- a = x;
- }
- return 1;
- }
- void freeleks(struct elem * a) {
- if (a != NULL) {
- freeleks(a->left);
- freeleks(a->right);
- free(a);
- }
- }
- int main()
- {
- root = NULL;
- char str[20] ;
- int flag = 0,ident = 0,v = 0,k,n,smbl;
- scanf("%d" , &n);
- smbl = getchar();
- smbl = getchar();
- for(int i = 0;i < n;) {
- if (smbl == ' ') {
- smbl = getchar();
- i++;
- }
- else {
- switch(smbl) {
- case '+':
- flag = 1;
- printf("SPEC 0\n");
- i++;
- break;
- case '-':
- flag = 1;
- printf("SPEC 1\n");
- i++;
- break;
- case '*':
- flag = 1;
- printf("SPEC 2\n");
- i++;
- break;
- case '/':
- flag = 1;
- printf("SPEC 3\n");
- i++;
- break;
- case '(':
- flag = 1;
- printf("SPEC 4\n");
- i++;
- break;
- case ')':
- flag = 1;
- printf("SPEC 5\n");
- i++;
- break;
- }
- if (flag == 1) {
- smbl = getchar();
- flag = 0;
- continue;
- }
- else {
- if ((smbl >= 48) && (smbl <= 57)) {
- printf("CONST ");
- while((smbl >= 48) && (smbl <= 57) && (i < n)) {
- printf("%c" , smbl);
- smbl = getchar();
- i++;
- }
- printf("\n");
- }
- else {
- while((i < n) && (((smbl >= 48) && (smbl <= 57)) || ((smbl >= 65) && (smbl <= 90)) || ((smbl >= 97) && (smbl <= 122)))) {
- str[ident] = smbl;
- smbl = getchar();
- i++;
- ident++;
- }
- if (ident > 0) {
- str[ident] = '\0';
- k = InsertAVL(v,str);
- if (k == 0) {
- printf("IDENT %d\n" , (Search(str))->v);
- }
- else {
- printf("IDENT %d\n" , (Search(str))->v);
- v++;
- }
- ident = 0;
- }
- }
- }
- }
- }
- freeleks(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement