Advertisement
Guest User

Untitled

a guest
May 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <string.h>
  4. struct node
  5. {
  6.     char imie[30];
  7.     char nazwisko[30];
  8.     struct node* parent;
  9.     struct node* left;
  10.     struct node* right;
  11. };
  12.  
  13. struct node* talloc()
  14. {
  15.     return ((struct node*) malloc(sizeof(struct node)));
  16. }
  17.  
  18.  
  19. struct node* dodajl(struct node* a, struct node **h)       
  20. {
  21.     int t;
  22.     struct node** head;
  23.     head=h;
  24.         if (*head!=NULL)
  25.         if ((t=strcmp(a->imie, (*head)->imie))==0)
  26.             t=strcmp(a->nazwisko, (*head)->nazwisko);
  27.            
  28.         while((*head)!=NULL && t!=0)   
  29.         {
  30.             if (t>0) head=&((*head)->right);
  31.             else head=&((*head)->left);
  32.             if ((*head)!=NULL)
  33.                 if ((t=strcmp(a->imie, (*head)/*->next*/->imie))==0)
  34.                     t=strcmp(a->nazwisko, (*head)/*->next*/->nazwisko);
  35.         }
  36.        
  37.         if ((*head)!=NULL) return *head;
  38.         else
  39.         {
  40.             //a->left=NULL;
  41.             //a->right=NULL;
  42.             *head=a;
  43.             return *head;
  44.         }
  45. }
  46.  
  47. void popraw(struct node* a, struct node* b)
  48. {
  49.     struct node* ta;
  50.     struct node* tb;
  51.     struct node* tmp;
  52.    
  53.     ta=a->parent;
  54.     tb=b->parent;
  55.    
  56.     while(ta!=NULL && tb!=NULL)
  57.     {
  58.         if (ta->imie[0]!='\'')
  59.             tmp=ta;
  60.         else
  61.             tmp=tb;
  62.         a->parent=tmp;
  63.         b->parent=tmp;
  64.         ta=ta->parent;
  65.         tb=tb->parent;
  66.         a=a->parent;
  67.         b=a;
  68.     }
  69.     if(ta!=NULL)
  70.     {
  71.         b->parent=ta;
  72.         a->parent=ta;
  73.     }
  74.     else if(tb!=NULL)
  75.     {
  76.         b->parent=tb;
  77.         a->parent=tb;
  78.     } else
  79.     {
  80.         a->parent=NULL;
  81.         //b->parent=NULL;
  82.     }
  83. }
  84.  
  85.  
  86. void popraw2(struct node* a, struct node* ta, struct node* tb)
  87. {
  88.     struct node* tmp;
  89.    
  90.     while(ta!=NULL && tb!=NULL)
  91.     {
  92.         if (ta->imie[0]!='\'')
  93.             tmp=ta;
  94.         else
  95.             tmp=tb;
  96.         a->parent=tmp;
  97.         ta=ta->parent;
  98.         tb=tb->parent;
  99.         a=a->parent;
  100.     }
  101.     if(ta!=NULL)
  102.     {
  103.         a->parent=ta;
  104.     }
  105.     else if(tb!=NULL)
  106.     {
  107.         a->parent=tb;
  108.     } else a->parent=NULL;
  109. }
  110.  
  111.  
  112. void dodajd(char* operator, struct node* a, struct node* b)
  113. {
  114.     int i;
  115.     struct node* ch;
  116.     if (strcmp(operator,"grandparent-of")==0)
  117.     {
  118.         if(b->parent!=NULL)
  119.         {
  120.             b=b->parent;
  121.         }
  122.         else
  123.         {
  124.             ch=talloc();
  125.             ch->parent=NULL;
  126.             ch->imie[0]='\'';
  127.             ch->nazwisko[0]='0';
  128.             b->parent=ch;
  129.             b=b->parent;
  130.         }
  131.         operator="parent-of";
  132.         dodajd(operator,a,b);
  133.     }   else
  134.    
  135.     if (strcmp(operator,"parent-of")==0)
  136.         popraw2(b,a,b->parent);
  137.     else
  138.    
  139.     if (strcmp(operator,"sibling-of")==0)
  140.         popraw(a,b);
  141.     else
  142.     if (strcmp(operator,"grandchild-of")==0)
  143.     {
  144.         operator="grandparent-of";
  145.         dodajd(operator,b,a);
  146.     }
  147.     else
  148.     if (strcmp(operator,"child-of")==0)
  149.     {
  150.         operator="parent-of";
  151.         dodajd(operator,b,a);
  152.     }
  153.     else
  154.     if (strcmp(operator,"cousin-of")==0)
  155.     {
  156.         if ((a->parent)!=NULL)
  157.         {
  158.             a=a->parent;
  159.         }
  160.         else
  161.         {
  162.             ch=talloc();
  163.             ch->imie[0]='\'';
  164.             ch->nazwisko[0]='\'';
  165.             ch->parent=NULL;
  166.             a->parent=ch;  
  167.             a=a->parent;
  168.         }  
  169.         if ((b->parent)!=NULL)
  170.         {
  171.             b=b->parent;
  172.         }
  173.         else
  174.         {
  175.             ch=talloc();
  176.             //ch->next=NULL;
  177.        
  178.             ch->imie[0]='\'';
  179.             ch->nazwisko[0]='\'';
  180.        
  181.             ch->parent=NULL;
  182.             b->parent=ch;  
  183.             b=b->parent;
  184.         }
  185.         operator="sibling-of";
  186.         dodajd(operator,a,b);
  187.        
  188.        
  189.     }
  190. }
  191.  
  192.  
  193. znajdz(struct node* a, struct node* b)
  194. {
  195.     struct node* c;
  196.     b=b->parent;
  197.     c=b;
  198.     a=a->parent;
  199.     while (a!=NULL && a!=b)
  200.     {
  201.         b=c;
  202.         while (b!=NULL && a!=b)
  203.             b=b->parent;
  204.         if (a!=b)
  205.             a=a->parent;
  206.     }          
  207.     if (a!=NULL)
  208.     while(a!=NULL && a->imie[0]=='\'')
  209.         a=a->parent;
  210.     if (a==NULL)
  211.         printf("NN\n");
  212.     else
  213.         printf("%s %s\n", a->imie, a->nazwisko);
  214. }
  215.  
  216.  
  217. int main()
  218. {
  219.     struct node* a;
  220.     struct node* b;
  221.     struct node* head;
  222.     char* operator;
  223.    
  224.     operator=malloc(16*(sizeof(char)));
  225.     head=NULL;
  226.     a=talloc();
  227.     b=talloc();
  228.    
  229.     while(scanf("%s %s %s %s %s",a->imie, a->nazwisko, operator, b->imie, b->nazwisko)!=EOF)
  230.     {
  231.         a->right=NULL;
  232.         a->left=NULL;
  233.         a->parent=NULL;
  234.         b->parent=NULL;
  235.         b->right=NULL;
  236.         b->left=NULL;
  237.         a=dodajl(a, &head);
  238.         b=dodajl(b, &head);
  239.         if (strcmp(operator,":")!=0)
  240.             dodajd(operator, a, b);
  241.         else
  242.             znajdz(a,b);
  243.         a=talloc();
  244.         b=talloc();
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement