Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- # include <stdlib.h>
- # include <string.h>
- struct node
- {
- char imie[30];
- char nazwisko[30];
- struct node* parent;
- struct node* left;
- struct node* right;
- };
- struct node* talloc()
- {
- return ((struct node*) malloc(sizeof(struct node)));
- }
- struct node* dodajl(struct node* a, struct node **h)
- {
- int t;
- struct node** head;
- head=h;
- if (*head!=NULL)
- if ((t=strcmp(a->imie, (*head)->imie))==0)
- t=strcmp(a->nazwisko, (*head)->nazwisko);
- while((*head)!=NULL && t!=0)
- {
- if (t>0) head=&((*head)->right);
- else head=&((*head)->left);
- if ((*head)!=NULL)
- if ((t=strcmp(a->imie, (*head)/*->next*/->imie))==0)
- t=strcmp(a->nazwisko, (*head)/*->next*/->nazwisko);
- }
- if ((*head)!=NULL) return *head;
- else
- {
- //a->left=NULL;
- //a->right=NULL;
- *head=a;
- return *head;
- }
- }
- void popraw(struct node* a, struct node* b)
- {
- struct node* ta;
- struct node* tb;
- struct node* tmp;
- ta=a->parent;
- tb=b->parent;
- while(ta!=NULL && tb!=NULL)
- {
- if (ta->imie[0]!='\'')
- tmp=ta;
- else
- tmp=tb;
- a->parent=tmp;
- b->parent=tmp;
- ta=ta->parent;
- tb=tb->parent;
- a=a->parent;
- b=a;
- }
- if(ta!=NULL)
- {
- b->parent=ta;
- a->parent=ta;
- }
- else if(tb!=NULL)
- {
- b->parent=tb;
- a->parent=tb;
- } else
- {
- a->parent=NULL;
- //b->parent=NULL;
- }
- }
- void popraw2(struct node* a, struct node* ta, struct node* tb)
- {
- struct node* tmp;
- while(ta!=NULL && tb!=NULL)
- {
- if (ta->imie[0]!='\'')
- tmp=ta;
- else
- tmp=tb;
- a->parent=tmp;
- ta=ta->parent;
- tb=tb->parent;
- a=a->parent;
- }
- if(ta!=NULL)
- {
- a->parent=ta;
- }
- else if(tb!=NULL)
- {
- a->parent=tb;
- } else a->parent=NULL;
- }
- void dodajd(char* operator, struct node* a, struct node* b)
- {
- int i;
- struct node* ch;
- if (strcmp(operator,"grandparent-of")==0)
- {
- if(b->parent!=NULL)
- {
- b=b->parent;
- }
- else
- {
- ch=talloc();
- ch->parent=NULL;
- ch->imie[0]='\'';
- ch->nazwisko[0]='0';
- b->parent=ch;
- b=b->parent;
- }
- operator="parent-of";
- dodajd(operator,a,b);
- } else
- if (strcmp(operator,"parent-of")==0)
- popraw2(b,a,b->parent);
- else
- if (strcmp(operator,"sibling-of")==0)
- popraw(a,b);
- else
- if (strcmp(operator,"grandchild-of")==0)
- {
- operator="grandparent-of";
- dodajd(operator,b,a);
- }
- else
- if (strcmp(operator,"child-of")==0)
- {
- operator="parent-of";
- dodajd(operator,b,a);
- }
- else
- if (strcmp(operator,"cousin-of")==0)
- {
- if ((a->parent)!=NULL)
- {
- a=a->parent;
- }
- else
- {
- ch=talloc();
- ch->imie[0]='\'';
- ch->nazwisko[0]='\'';
- ch->parent=NULL;
- a->parent=ch;
- a=a->parent;
- }
- if ((b->parent)!=NULL)
- {
- b=b->parent;
- }
- else
- {
- ch=talloc();
- //ch->next=NULL;
- ch->imie[0]='\'';
- ch->nazwisko[0]='\'';
- ch->parent=NULL;
- b->parent=ch;
- b=b->parent;
- }
- operator="sibling-of";
- dodajd(operator,a,b);
- }
- }
- znajdz(struct node* a, struct node* b)
- {
- struct node* c;
- b=b->parent;
- c=b;
- a=a->parent;
- while (a!=NULL && a!=b)
- {
- b=c;
- while (b!=NULL && a!=b)
- b=b->parent;
- if (a!=b)
- a=a->parent;
- }
- if (a!=NULL)
- while(a!=NULL && a->imie[0]=='\'')
- a=a->parent;
- if (a==NULL)
- printf("NN\n");
- else
- printf("%s %s\n", a->imie, a->nazwisko);
- }
- int main()
- {
- struct node* a;
- struct node* b;
- struct node* head;
- char* operator;
- operator=malloc(16*(sizeof(char)));
- head=NULL;
- a=talloc();
- b=talloc();
- while(scanf("%s %s %s %s %s",a->imie, a->nazwisko, operator, b->imie, b->nazwisko)!=EOF)
- {
- a->right=NULL;
- a->left=NULL;
- a->parent=NULL;
- b->parent=NULL;
- b->right=NULL;
- b->left=NULL;
- a=dodajl(a, &head);
- b=dodajl(b, &head);
- if (strcmp(operator,":")!=0)
- dodajd(operator, a, b);
- else
- znajdz(a,b);
- a=talloc();
- b=talloc();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement