实例:
设计一个模拟社会关系的数据结构,每个人的信息用结构表示,包含名字、性别和指向父亲、母亲、配偶、子女的指针(只限两个子女)。要求编写以下函数:
(1)增加一个新人的函数
(2)建立人与人之间关系的函数:父-子、母-子、配偶等。
(3)检查两人之间是否为堂兄妹
思路解析:
能够充分的联系指针的应用。书中的代码在增加一个新人时,只为新人提供名字和性别,关于新人的其他信息通过调用其他函数建立。
书中代码如下:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define CHILDREN 2 6 7 struct person{ 8 char *name; /*名字符串指针*/ 9 char sex; /*性别:男用字符'M';女用字符'F'*/ 10 struct person *father; /*指向父亲*/ 11 struct person *mother; /*指向母亲*/ 12 struct person *mate; /*指向配偶*/ 13 struct person *childern[CHILDREN];/*指向子女*/ 14 }; 15 16 /* [函数]newperson增加新人 */ 17 struct person *newperson(char *name, char sex) 18 { 19 struct person *p; 20 int index; 21 22 p = (struct person *)malloc(sizeof(struct person)); 23 p->name = (char *)malloc(strlen(name)+1); 24 strcpy(p->name, name); 25 p->sex = sex; 26 p->father = NULL; 27 p->mother = NULL; 28 p->mate = NULL; 29 for(index=0; index<CHILDREN; index++) 30 p->childern[index] = NULL; 31 return p; 32 } 33 34 /* [函数]father_child建立父-子关系 */ 35 void father_child(struct person *father, struct person *child) 36 { 37 int index; 38 39 for(index=0; index<CHILDREN-1; index++)/*寻找一个空缺的子女指针*/ 40 if(father->childern[index]==NULL) /*若没有空缺,则填在最后*/ 41 break; 42 father->childern[index] = child; /*建立父-子关系*/ 43 child->father = father; 44 } 45 46 void mother_child(struct person *mother, struct person *child) 47 { 48 int index; 49 for(index=0; index<CHILDREN-1; index++)/*寻找一个空缺的子女指针*/ 50 if(mother->childern[index]==NULL) /*若没有空缺,则填在最后*/ 51 break; 52 mother->childern[index] = child; /*建立母-子关系*/ 53 child->mother = mother; 54 } 55 56 /* [函数]mate 建立配偶关系 */ 57 void mate(struct person *h, struct person *w) 58 { 59 h->mate = w; 60 w->mate = h; 61 } 62 63 /* [函数]brotherinlow 检查两人是否是堂兄妹 */ 64 int brothersinlaw(struct person *p1, struct person *p2) 65 { 66 struct person *f1, *f2; 67 if(p1==NULL||p2==NULL||p1==p2) 68 return 0; 69 if(p1->sex==p2->sex) return 0;/*不可能是堂兄妹*/ 70 f1 = p1->father; 71 f2 = p2->father; 72 if(f1!=NULL && f1==f2) return 0;/*是兄妹,不是堂兄妹*/ 73 while(f1!=NULL&&f2!=NULL&&f1!=f2)/*考虑远房情况*/ 74 { 75 f1 = f1->father; 76 f2 = f2->father; 77 if(f1!=NULL && f2!=NULL && f1==f2) return 1; 78 } 79 return 0; 80 } 81 82 /* 函数print_relate用于输出人物p的姓名,性别和各种关系 */ 83 void print_relate(struct person *p) 84 { 85 int index,i; 86 if(p->name == NULL) 87 return; 88 if(p->sex == 'M') 89 printf(" %s is male.", p->name); 90 else 91 printf(" %s is female.",p->name); 92 if(p->father != NULL) 93 printf(" %s's father is %s.",p->name,p->father->name); 94 if(p->mother != NULL) 95 printf(" %s's mother is %s.",p->name,p->mother->name); 96 printf(" "); 97 if(p->mate != NULL) 98 if(p->sex == 'M') 99 printf(" His wife is %s.", p->mate->name); 100 else 101 printf(" Her husband is %s.",p->mate->name); 102 if(p->childern != NULL) 103 { 104 for(index=0; index<CHILDREN-1; index++) 105 if(p->childern[index]==NULL) 106 break; 107 if(index>0) 108 printf(" Children are:"); 109 for(i=0; i<index; i++) 110 printf(" %s",p->childern[i]->name); 111 } 112 printf(" "); 113 } 114 115 int main() 116 { 117 char *name[8] = {"John","Kate","Maggie","Herry","Jason","Peter","Marry","Jenny"}; 118 char male='M', female='F'; 119 struct person *pGrandfather, *pFather1, *pFather2, *pMother1, *pMother2; 120 struct person *pSon, *pDaughter, *pCousin; 121 122 pGrandfather = newperson(name[0],male); 123 pFather1 = newperson(name[3],male); 124 pFather2 = newperson(name[4],male); 125 pMother1 = newperson(name[1],female); 126 pMother2 = newperson(name[2],female); 127 pSon = newperson(name[5],male); 128 pDaughter = newperson(name[6],female); 129 pCousin = newperson(name[7],female); 130 131 father_child(pGrandfather,pFather1); 132 father_child(pGrandfather,pFather2); 133 father_child(pFather1,pSon); 134 father_child(pFather1,pDaughter); 135 father_child(pFather2,pCousin); 136 137 mate(pFather1,pMother1); 138 mate(pFather2,pMother2); 139 mother_child(pMother1,pSon); 140 mother_child(pMother1,pDaughter); 141 mother_child(pMother2,pCousin); 142 143 print_relate(pGrandfather); 144 print_relate(pFather1); 145 print_relate(pFather2); 146 print_relate(pMother1); 147 print_relate(pMother2); 148 print_relate(pSon); 149 print_relate(pDaughter); 150 print_relate(pCousin); 151 152 153 if(!brothersinlaw(pDaughter,pCousin)) 154 printf("%s and %s are not brothers (sisters) in law. ",pDaughter->name,pCousin->name); 155 else 156 printf("%s and %s are brothers (sisters) in law. ",pDaughter->name,pCousin->name); 157 if(!brothersinlaw(pSon,pCousin)) 158 printf("%s and %s are not brothers (sisters) in law. ",pSon->name,pCousin->name); 159 else 160 printf("%s and %s are brothers (sisters) in law. ",pSon->name,pCousin->name); 161 if(!brothersinlaw(pSon,pDaughter)) 162 printf("%s and %s are not brothers (sisters) in law. ",pSon->name,pDaughter->name); 163 else 164 printf("%s and %s are brothers (sisters) in law. ",pSon->name,pDaughter->name); 165 166 //printf("Hello world! "); 167 return 0; 168 }