類比社會關係,類比社會
本執行個體有求設計一個類比社會關係的資料結構,每個人的資訊用結構表示,包含名字,性別和指向父親,母親,配偶,子女的指標(設只限兩個子女)。要求編寫以下函數:
該執行個體的主要目的是聯絡C中結構體的使用,下面是函數的實現:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define CHILDREN 2/** * 設計一個類比社會關係的資料結構,每個人的資訊用結構表示,包含名字, * 性別和指向父親,母親,配偶,子女的指標(設只限兩個子女)。要求編寫 * 以下函數:(1)增加一個新人的函數 (2)建立人與人之間關係的函數,父子 * 、母子、配偶等 (3)檢查某兩人之間是否是堂兄妹 */struct person{ char *name; /* 人的姓名 */ char sex; /* 性別,'M'表示男性,'F'表示女性 */ struct person *father; /* 該人的父親 */ struct person *mother; /* 該人的母親 */ struct person *mate; /* 該人的配偶 */ struct person *childs[CHILDREN]; /* 該人的孩子 */};/* [函數] 添加一個新人 */struct person * newperson(char *name,char sex){ struct person *p = (struct person *)malloc(sizeof(struct person)); p->name = (char *)malloc(sizeof(name)+1); strcpy(p->name,name); p->sex = sex; p->father = NULL; p->mother = NULL; p->mate = NULL; int i = 0; for(i = 0;i < CHILDREN;i++){ p->childs[i] = NULL; } return p;};/* [函數] 建立父子關係 */void father_child(struct person *father,struct person *child){ int index; for(index = 0;index < CHILDREN-1;index++) /* 尋找一個空缺的位置 */ if(father->childs[index] == NULL) /* 如果沒有,則放到最後 */ break; father->childs[index] = child; child->father = father;}/* [函數] 建立母子關係 */void mother_child(struct person *mother,struct person *child){ int index; for(index = 0;index < CHILDREN-1;index++) /* 尋找一個空缺的位置 */ if(mother->childs[index] == NULL) /* 如果沒有,則放到最後 */ break; mother->childs[index] = child; child->mother = mother;}/* [函數] mate 建立配偶關係 */void mate(struct person *h,struct person *w){ /* 建立配偶關係 */ h->mate = w; w->mate = h;}/** [函數] 判斷是否為堂兄妹 * params: * struct person *p1:被判斷的人 * struct person *p2:被判斷的人 * return: * 0:不是堂兄妹關係 * 1:是堂兄妹關係 */ int brothersinlaw(struct person *p1,struct person *p2) { struct person *f1,*f2; if(p1 == NULL || p2 == NULL || p1 == p2) return 0; if(p1->sex == p2->sex) return 0; /* 不可能是堂兄妹*/ f1 = p1->father; f2 = p2->father; if(f1 != NULL &&f1 == f1) return 0; /* 是兄妹,不是堂兄妹 */ while(f1 != NULL && f2 != NULL && f1 != f2) /* 遠親 */ { f1 = f1->father; f2 = f2->father; if(f1 != NULL && f2 != NULL && f1 == f2) return 1; } return 0; }/* [函數] 輸出人物關係 */void print_relate(struct person *p){ int index,i; if(p->name == NULL) return; if(p->sex == 'M') printf("%s is male.\n",p->name); else printf("%s is female.\n",p->name); if(p->father != NULL) printf("%s's father is %s.\n",p->name,p->father->name); if(p->mother != NULL) printf("%s's mother is %s.\n",p->name,p->mother->name); if(p->mate != NULL) if(p->sex == 'M') printf("His wife is %s.\n",p->mate->name); else printf("Her husband is %s.\n",p->mate->name); if(p->childs != NULL){ for(index = 0;index <CHILDREN-1;index++) if(p->childs[index] == NULL) break; if(index > 0) printf(" Children are : "); for(i = 0;i < index;i++) printf("%s\t",p->childs[i]->name); } printf("\n");}int main(){ char *name[8]={"John","Kate","Maggie","Herry","Jason","Peter","Marry","Jenny"}; char male='M',female='F'; struct person *pGrandfather,*pFather1,*pFather2,*pMother1,*pMother2,*pSon,*pDaughter,*pCousin; pGrandfather = newperson(name[0],male); pFather1 = newperson(name[3],male); pFather2 = newperson(name[4],male); pMother1 = newperson(name[1],female); pMother2 = newperson(name[2],female); pSon = newperson(name[5],male); pDaughter = newperson(name[6],female); pCousin = newperson(name[7],female); father_child(pGrandfather,pFather1); father_child(pGrandfather,pFather2); father_child(pFather1,pSon); father_child(pFather1,pDaughter); father_child(pFather2,pCousin); mate(pFather1,pMother1); mate(pFather2,pMother2); mother_child(pMother1,pSon); mother_child(pMother1,pDaughter); mother_child(pMother2,pCousin); /* 輸出各種關係 */ print_relate(pGrandfather); print_relate(pFather1); print_relate(pFather2); print_relate(pMother1); print_relate(pMother2); print_relate(pSon); print_relate(pDaughter); print_relate(pCousin); if(!brothersinlaw(pDaughter,pCousin)) printf("%s and %s are not brothers (sisters) in law.\n",pDaughter->name,pCousin->name); else printf("%s and %s are brothers (sisters) in law.\n",pDaughter->name,pCousin->name); if(!brothersinlaw(pSon,pCousin)) printf("%s and %s are not brothers (sisters) in law.\n",pSon->name,pCousin->name); else printf("%s and %s are brothers (sisters) in law.\n",pSon->name,pCousin->name); if(!brothersinlaw(pSon,pDaughter)) printf("%s and %s are not brothers (sisters) in law.\n",pSon->name,pDaughter->name); else printf("%s and %s are brothers (sisters) in law.\n",pSon->name,pDaughter->name); return 0;}
總體來說,該執行個體並不難,並沒有涉及到比較複雜的演算法,其中稍微有些需要考慮的地方就是在判斷兩個人是否是堂兄妹的時候,用到了一點小方法,也不是很難。
下面我們來看一下程式的運行結果:
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。