Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // prog0202.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #define nSpouse 10
- #define nChild 10
- struct Person {
- char name[20];
- unsigned int yearFrom, yearTo;
- char gender;
- };
- struct PersonNode {
- Person person;
- struct PersonNode *father;
- struct PersonNode *mother;
- struct PersonNode *spouse[nSpouse];
- struct PersonNode *child[nChild];
- unsigned int numOfChildren;
- };
- struct Family {
- PersonNode *head;
- };
- void addHead(Family *family, Person *person); //ініціалізація дерева
- Person *getHead(Family *family);
- PersonNode *addSpouse(PersonNode *person, Person *spouse);
- PersonNode *addChild (PersonNode *human, Person *child, PersonNode* another_parent);
- PersonNode *makeNode(Person *person);
- bool hasChild(PersonNode *person);
- bool hasSpouse(PersonNode *person);
- void display(Family *family);
- void displayPerson(PersonNode *human);
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"Ukrainian");
- Person ryurik = { "Рюрик", 862, 879,'M' }; //M - W
- Person ihor = { "Iгор" , 912,945, 'M' };
- Person olga= { "Ольга", 945,972, 'W' };
- Person svatoslav= {"Святослав", 972,972,'M'};
- Person yaropolk= {"Ярополк" ,972,980,'M'};
- Person oleg = {"Олег ",977,0,'M'};
- Person volodymyr= {"Володимир ",980,1015,'M'};
- Person svyatopolk={ "Святополк ",1015,1019,'M'};
- Person yaroslav={ "Ярослав Мудрий ",1019,1054,'M'};
- Person izyaslav={ "Iзяслав ",1054,1078,'M'};
- Person svyatopolk2={ "Святополк II ",1093,0,'M'};
- Person svatoslav2={ "Святослав ",1073,1076,'M'};
- Person oleg2={ "Олег ",1115,0,'M'};
- Person vsevolod={ "Всеволод ",1078,1093,'M'};
- Person volodymyr_m={ "Володимир Мономах ",1113,1125,'M'};
- Person mstyslav={ "Мстислав ",1036,0,'M'};
- Person borys={ "Борис",1015, 0 ,'M'};
- Person glib={ "Гліб",1015,0,'M'};
- Family russian_kings = { NULL };
- PersonNode *tmp, *tmp2;
- addHead (&russian_kings, &ryurik);
- tmp=russian_kings.head;
- tmp=addChild (tmp, &ihor, NULL);
- tmp2=addSpouse(tmp,&olga);
- tmp=addChild(tmp,&svatoslav,tmp2);
- addChild(tmp,&yaropolk,NULL);
- addChild(tmp,&oleg,NULL);
- tmp=addChild(tmp,&volodymyr,NULL);
- display (&russian_kings);
- return 0;
- }
- PersonNode *makeNode(Person *person)
- {
- PersonNode *newNode = (PersonNode *) calloc(1, sizeof(PersonNode));
- newNode->person = *person;
- newNode->father = newNode->mother = NULL;
- newNode->numOfChildren = 0;
- return newNode;
- }
- void addHead(Family *family, Person *person)
- {
- if (family->head == NULL) {
- PersonNode *newNode = makeNode(person);
- family->head = newNode;
- }
- else
- fprintf_s(stderr, "Family already had a head.");
- }
- Person *getHead(Family *family)
- {
- if (family != NULL )
- return &family->head->person;
- else
- fprintf_s(stderr, "Family is empty.");
- return NULL;
- }
- PersonNode *addSpouse(PersonNode *person, Person *spouse)
- {
- if (person != NULL) {
- PersonNode *newNode = makeNode(spouse);
- for (int i=0;i<nSpouse;i++)
- {
- if (person->spouse[i]==NULL)
- {
- person->spouse[i]=newNode;
- return newNode;
- }
- }
- }
- fprintf_s(stderr, "Can't add spouse.");
- return NULL;
- }
- PersonNode *addChild (PersonNode *human, Person *child, PersonNode* another_parent)
- {
- if (human !=NULL) {
- PersonNode *newNode = makeNode(child);
- if (human->person.gender == 'M')
- {
- newNode->father = human;
- newNode->mother = another_parent;
- }
- else
- {
- newNode->mother = human;
- newNode->father = another_parent;
- }
- human->child[human->numOfChildren++] = newNode;
- return newNode;
- }
- fprintf_s(stderr, "Can't add spouse.");
- return NULL;
- }
- bool hasChild(PersonNode *person) {
- if (person)
- return person->numOfChildren>0;
- else
- return false;
- }
- bool hasSpouse(PersonNode *person) {
- if (person->spouse[0]) return true;
- else return false;
- }
- void display(Family *family) {
- puts("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
- puts("<family>");
- family->head;
- displayPerson(family->head);
- puts("</family>");
- }
- void displayPerson(PersonNode *human)
- {
- if (hasChild(human))
- {
- printf_s("<person name=\"%s\" married=\"%d\" child=\"%d\" >\n",
- human->person.name, hasSpouse(human), human->numOfChildren);
- for (size_t i = 0; i < human->numOfChildren; i++)
- displayPerson(human->child[i]);
- puts("</person>");
- }
- else
- {
- printf_s("<person name=\"%s\" married=\"%d\" child=\"0\" />\n",
- human->person.name, hasSpouse(human));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement