Advertisement
andruhovski

prog-0202c

Oct 22nd, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 KB | None | 0 0
  1. // prog0202.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #define nSpouse 10
  6. #define nChild 10
  7. struct Person {
  8.     char name[20];
  9.     unsigned int yearFrom, yearTo;
  10.     char gender;
  11. };
  12.  
  13. struct PersonNode {
  14.     Person person;
  15.     struct PersonNode *father;
  16.     struct PersonNode *mother;
  17.     struct PersonNode *spouse[nSpouse];
  18.     struct PersonNode *child[nChild];
  19.     unsigned int numOfChildren;
  20. };
  21.  
  22. struct Family {
  23.     PersonNode *head;
  24. };
  25.  
  26. void addHead(Family *family, Person *person); //ініціалізація дерева
  27. Person *getHead(Family *family);
  28. PersonNode *addSpouse(PersonNode *person, Person *spouse);
  29. PersonNode *addChild (PersonNode *human,  Person *child, PersonNode* another_parent);
  30. PersonNode *makeNode(Person *person);
  31. bool hasChild(PersonNode *person);
  32. bool hasSpouse(PersonNode *person);
  33. void display(Family *family);
  34. void displayPerson(PersonNode *human);
  35. int _tmain(int argc, _TCHAR* argv[])
  36. {
  37.     setlocale(LC_ALL,"Ukrainian");
  38.     Person ryurik = { "Рюрик", 862, 879,'M' }; //M - W
  39.     Person ihor =   { "Iгор" , 912,945, 'M' };
  40.     Person olga=    { "Ольга", 945,972, 'W' };
  41.     Person svatoslav= {"Святослав", 972,972,'M'};
  42.     Person yaropolk=  {"Ярополк" ,972,980,'M'};
  43.     Person oleg =   {"Олег ",977,0,'M'};
  44.     Person volodymyr= {"Володимир ",980,1015,'M'};
  45.     Person svyatopolk={ "Святополк ",1015,1019,'M'};
  46.     Person yaroslav={ "Ярослав Мудрий ",1019,1054,'M'};
  47.     Person izyaslav={ "Iзяслав ",1054,1078,'M'};
  48.     Person svyatopolk2={ "Святополк II ",1093,0,'M'};
  49.     Person svatoslav2={ "Святослав ",1073,1076,'M'};
  50.     Person oleg2={ "Олег ",1115,0,'M'};
  51.     Person vsevolod={ "Всеволод ",1078,1093,'M'};
  52.     Person volodymyr_m={ "Володимир Мономах ",1113,1125,'M'};
  53.     Person mstyslav={ "Мстислав ",1036,0,'M'};
  54.     Person borys={ "Борис",1015, 0 ,'M'};
  55.     Person glib={ "Гліб",1015,0,'M'};
  56.     Family russian_kings = { NULL };
  57.     PersonNode *tmp, *tmp2;
  58.    
  59.     addHead (&russian_kings, &ryurik);
  60.     tmp=russian_kings.head;
  61.     tmp=addChild (tmp, &ihor, NULL);
  62.     tmp2=addSpouse(tmp,&olga);
  63.     tmp=addChild(tmp,&svatoslav,tmp2);
  64.     addChild(tmp,&yaropolk,NULL);
  65.     addChild(tmp,&oleg,NULL);
  66.     tmp=addChild(tmp,&volodymyr,NULL);
  67.  
  68.     display (&russian_kings);
  69.  
  70.     return 0;
  71. }
  72.  
  73. PersonNode *makeNode(Person *person)
  74. {
  75.     PersonNode *newNode = (PersonNode *) calloc(1, sizeof(PersonNode));
  76.     newNode->person = *person;
  77.     newNode->father = newNode->mother = NULL;
  78.     newNode->numOfChildren = 0;
  79.     return newNode;
  80. }
  81.  
  82. void addHead(Family *family, Person *person)
  83. {
  84.     if (family->head == NULL) {
  85.         PersonNode *newNode = makeNode(person);
  86.         family->head = newNode;
  87.     }
  88.     else
  89.         fprintf_s(stderr, "Family already had a head.");
  90. }
  91.  
  92. Person *getHead(Family *family)
  93. {
  94.     if (family != NULL )
  95.         return &family->head->person;
  96.     else
  97.         fprintf_s(stderr, "Family is empty.");
  98.     return NULL;
  99. }
  100.  
  101. PersonNode *addSpouse(PersonNode *person, Person *spouse)
  102. {
  103.     if (person != NULL) {
  104.         PersonNode *newNode = makeNode(spouse);
  105.         for (int i=0;i<nSpouse;i++)
  106.         {
  107.             if (person->spouse[i]==NULL)
  108.             {
  109.                 person->spouse[i]=newNode;
  110.                 return newNode;
  111.             }
  112.         }
  113.     }
  114.     fprintf_s(stderr, "Can't add spouse.");
  115.     return NULL;
  116. }
  117.  
  118. PersonNode *addChild (PersonNode *human,  Person *child, PersonNode* another_parent)
  119. {
  120.     if (human !=NULL) {
  121.         PersonNode *newNode = makeNode(child);
  122.         if (human->person.gender == 'M')
  123.         {
  124.             newNode->father = human;
  125.             newNode->mother = another_parent;
  126.         }
  127.         else
  128.         {
  129.             newNode->mother = human;
  130.             newNode->father = another_parent;
  131.         }
  132.         human->child[human->numOfChildren++] = newNode;
  133.         return newNode;
  134.     }
  135.     fprintf_s(stderr, "Can't add spouse.");
  136.     return NULL;
  137. }
  138.  
  139. bool hasChild(PersonNode *person) {
  140.     if (person)
  141.         return person->numOfChildren>0;
  142.     else
  143.         return false;
  144. }
  145.  
  146. bool hasSpouse(PersonNode *person) {
  147.     if (person->spouse[0]) return true;
  148.     else return false;
  149. }
  150.  
  151. void display(Family *family) {
  152.     puts("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  153.     puts("<family>");
  154.     family->head;
  155.     displayPerson(family->head);
  156.     puts("</family>");
  157. }
  158.  
  159. void displayPerson(PersonNode *human)
  160. {
  161.     if (hasChild(human))
  162.     {
  163.         printf_s("<person name=\"%s\" married=\"%d\" child=\"%d\" >\n",
  164.             human->person.name, hasSpouse(human), human->numOfChildren);
  165.         for (size_t i = 0; i < human->numOfChildren; i++)
  166.             displayPerson(human->child[i]);
  167.         puts("</person>");
  168.     }
  169.     else
  170.     {
  171.         printf_s("<person name=\"%s\" married=\"%d\" child=\"0\" />\n",
  172.             human->person.name, hasSpouse(human));
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement