实例:

  设计一个模拟社会关系的数据结构,每个人的信息用结构表示,包含名字、性别和指向父亲、母亲、配偶、子女的指针(只限两个子女)。要求编写以下函数:

    (1)增加一个新人的函数

    (2)建立人与人之间关系的函数:父-子、母-子、配偶等。

    (3)检查两人之间是否为堂兄妹

思路解析:

  能够充分的联系指针的应用。书中的代码在增加一个新人时,只为新人提供名字和性别,关于新人的其他信息通过调用其他函数建立。

书中代码如下:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define CHILDREN 2 struct person{
char *name; /*名字符串指针*/
char sex; /*性别:男用字符'M';女用字符'F'*/
struct person *father; /*指向父亲*/
struct person *mother; /*指向母亲*/
struct person *mate; /*指向配偶*/
struct person *childern[CHILDREN];/*指向子女*/
}; /* [函数]newperson增加新人 */
struct person *newperson(char *name, char sex)
{
struct person *p;
int index; p = (struct person *)malloc(sizeof(struct person));
p->name = (char *)malloc(strlen(name)+);
strcpy(p->name, name);
p->sex = sex;
p->father = NULL;
p->mother = NULL;
p->mate = NULL;
for(index=; index<CHILDREN; index++)
p->childern[index] = NULL;
return p;
} /* [函数]father_child建立父-子关系 */
void father_child(struct person *father, struct person *child)
{
int index; for(index=; index<CHILDREN-; index++)/*寻找一个空缺的子女指针*/
if(father->childern[index]==NULL) /*若没有空缺,则填在最后*/
break;
father->childern[index] = child; /*建立父-子关系*/
child->father = father;
} void mother_child(struct person *mother, struct person *child)
{
int index;
for(index=; index<CHILDREN-; index++)/*寻找一个空缺的子女指针*/
if(mother->childern[index]==NULL) /*若没有空缺,则填在最后*/
break;
mother->childern[index] = child; /*建立母-子关系*/
child->mother = mother;
} /* [函数]mate 建立配偶关系 */
void mate(struct person *h, struct person *w)
{
h->mate = w;
w->mate = h;
} /* [函数]brotherinlow 检查两人是否是堂兄妹 */
int brothersinlaw(struct person *p1, struct person *p2)
{
struct person *f1, *f2;
if(p1==NULL||p2==NULL||p1==p2)
return ;
if(p1->sex==p2->sex) return ;/*不可能是堂兄妹*/
f1 = p1->father;
f2 = p2->father;
if(f1!=NULL && f1==f2) return ;/*是兄妹,不是堂兄妹*/
while(f1!=NULL&&f2!=NULL&&f1!=f2)/*考虑远房情况*/
{
f1 = f1->father;
f2 = f2->father;
if(f1!=NULL && f2!=NULL && f1==f2) return ;
}
return ;
} /* 函数print_relate用于输出人物p的姓名,性别和各种关系 */
void print_relate(struct person *p)
{
int index,i;
if(p->name == NULL)
return;
if(p->sex == 'M')
printf(" %s is male.", p->name);
else
printf(" %s is female.",p->name);
if(p->father != NULL)
printf(" %s's father is %s.",p->name,p->father->name);
if(p->mother != NULL)
printf(" %s's mother is %s.",p->name,p->mother->name);
printf("\n");
if(p->mate != NULL)
if(p->sex == 'M')
printf(" His wife is %s.", p->mate->name);
else
printf(" Her husband is %s.",p->mate->name);
if(p->childern != NULL)
{
for(index=; index<CHILDREN-; index++)
if(p->childern[index]==NULL)
break;
if(index>)
printf(" Children are:");
for(i=; i<index; i++)
printf(" %s",p->childern[i]->name);
}
printf("\n");
} int main()
{
char *name[] = {"John","Kate","Maggie","Herry","Jason","Peter","Marry","Jenny"};
char male='M', female='F';
struct person *pGrandfather, *pFather1, *pFather2, *pMother1, *pMother2;
struct person *pSon, *pDaughter, *pCousin; pGrandfather = newperson(name[],male);
pFather1 = newperson(name[],male);
pFather2 = newperson(name[],male);
pMother1 = newperson(name[],female);
pMother2 = newperson(name[],female);
pSon = newperson(name[],male);
pDaughter = newperson(name[],female);
pCousin = newperson(name[],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); //printf("Hello world!\n");
return ;
}

C语言实例解析精粹学习笔记——36(模拟社会关系)的更多相关文章

  1. C语言实例解析精粹学习笔记——18

    <C语言实例解析精粹>中编译环境采用的是Turbo C 2.0.但是这个编译器年代久远,较新的编译器对书中的某些例子支持不好,在学习的时候同时做一些笔记. 实例18:将一个无符号整数转换为 ...

  2. C语言实例解析精粹学习笔记——35(报数游戏)

    实例35: 设由n个人站成一圈,分别被编号1,2,3,4,……,n.第一个人从1开始报数,每报数位m的人被从圈中推测,其后的人再次从1开始报数,重复上述过程,直至所有人都从圈中退出. 实例解析: 用链 ...

  3. C语言实例解析精粹学习笔记——42(插入排序)

    实例说明: 将一个整数数组按从小到大的顺序进行排序.(主要学习基本的插入排序和改进的冒泡排序的算法和应用) 思路1: 从第一个数据开始,分别比较其后的数据,若比它小,则将这两个数的位置交换:从第一个数 ...

  4. C语言实例解析精粹学习笔记——32

    实例32: 编制一个包含姓名.地址.邮编和电话的通讯录输入和输出函数. 思路解析: 1.用结构体来完成姓名.地址.邮编和电话的组合. 2.结构体指针的使用. 3.malloc的使用 4.scanf函数 ...

  5. C语言实例解析精粹学习笔记——31

    实例31: 判断字符串是否是回文 思路解析: 引入两个指针变量(head和tail),开始时,两指针分别指向字符串的首末字符,当两指针所指字符相等时,两指针分别向后和向前移动一个字符位置,并继续比较, ...

  6. C语言实例解析精粹学习笔记——30

    实例30: 用已知字符串s中的字符,生成由其中n个字符组成的所有字符排列.设n小于字符串s的字符个数,其中s中的字符在每个排列中最多出现一次.例如,对于s[]="abc",n=2, ...

  7. C语言实例解析精粹学习笔记——28

    实例28:从键盘读入实数 题目要求: 编制一个从键盘读入实数的函数readreal(double *rp).函数将读入的实数字符列转换成实数后,利用指针参数rp,将实数存于指针所指向的变量*rp. 思 ...

  8. C语言实例解析精粹学习笔记——19

    实例19:判断正整数n的d进制表示形式是否是回文数(顺着看和倒着看相同的数). 主要思路: 一种方法:将正整数n数转换成d进制的数,逐个比较首尾对应数字,判断是否为回文数. 另一种方法:将正整数n数转 ...

  9. C语言实例解析精粹学习笔记——43(希尔排序)

    实例说明: 用希尔排序方法对数组进行排序.由于书中更关注的实例,对于原理来说有一定的解释,但是对于第一次接触的人来说可能略微有些简略.自己在草稿纸上画了好久,后来发现网上有好多很漂亮的原理图. 下面将 ...

随机推荐

  1. Linux学习笔记之Linux第一课-基本介绍

    Linux简介 Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux是一套免费使用和自由传播的类Unix操作系统,是一个基 ...

  2. python3的学习经验

    网上资料非常多,颇有些“乱花渐欲迷人眼”的意味,个人看了不少,拖之前从事前端的福,发现廖雪峰大神的网站里有.学了2天之后发觉获益良多,网址:https://www.liaoxuefeng.com/wi ...

  3. WIN10安装VS2013出现兼容性问题解决

    在WIN10安装VS2013时,会提示“windows程序兼容模式已打开”,通过搜索引擎搜索的常见方案为: 1.使用命令行安装,进入vs_ultimate文件所在目录,输入:vs_ultimate / ...

  4. scrum和团队合作

    一. 队名及宣言 队名 the better for you 宣言Change our lives with code 二. 队员及分工 a.承担软件工程的角色 姓名 学号 角色 张美庆 B20150 ...

  5. Android(java)学习笔记24:自定义异常类

    1. 自定义异常: 考试成绩必须在0-100之间 很明显java没有对应的异常,需要我们自己来做一个异常 自定义异常 继承自Exception 继承自RuntimeException 下面是一个代码示 ...

  6. 【[AHOI2012]树屋阶梯】

    卡特兰数! 至于为什么是卡特兰数,就稍微说那么一两句吧 对于一个高度为\(i\)的阶梯,我们可以在左上角填一个高度为\(k\)的阶梯,右下角填一个高度为\(i-1-k\)的阶梯剩下的我们用一个大的长方 ...

  7. Graphics Card Notes | 烧卡日记(显卡常识笔记)

    [ a comparison of several popular Graphics cards ]

  8. 【字符串】跳来跳去的KMP匹配

    原理: 不给予证明啦(懒得一批 但是代码中有给还算详细的注释 参考:https://www.cnblogs.com/yjiyjige/p/3263858.html 模板题: 洛谷P3375: http ...

  9. Kinect骨架数据

  10. 菜鸟笔记 -- Chapter 6 面向对象

    在Java语言中经常被提到的两个词汇是类与对象,实质上可以将类看作是对象的载体,它定义了对象所具有的功能.学习Java语言必须要掌握类与对象,这样可以从深层次去理解Java这种面向对象语言的开发理念, ...