实例:

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

    (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. Azure 10月新公布

    Azure 10月新发布:F 系列计算优化实例,认知服务,媒体服务流式处理单元更名,Azure 镜像市场,FreeBSD 适用于Azure 虚拟机的全新 F 系列计算优化实例 Azure 虚拟机的全新 ...

  2. 3.GlusterFS 企业分布式存储的搭建

    3.1 硬件要求 一般选择 2U 机型,磁盘 SATA 盘 4TB,如果 IO 要求比较高,可以采购 SSD 固态硬盘.为了充分保证系统的稳定性和性能,要求所有 glusterfs 服务器硬件配置尽量 ...

  3. May 22nd 2017 Week 21st Monday

    The biggest adventure you can take is to live the life of your dreams. 你能经历的最大的冒险,就是过上你梦想的生活. Just l ...

  4. leetcode 322零钱兑换

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  5. 一点一点学写Makefile(5)-获取文件所在路径

    我们在开发一套代码时,应该保证工程放到任何一个目录中均可以编译成功,但是有时候链接库的时候会造成编译错误,本次就会告诉大家如何动态的获得工程所在的绝对路径 代码下载目录 选择Makefile-5 // ...

  6. nautilus命令

    nautilus 是图形程式效果是以当前用户打开图形界面所以如果想以root打开图形界面使用时记得先切为root,sudo没有用的

  7. POJ 2531 深搜剪枝

    题意:全局最大割. 分析:有相应的算法,数据量很小,可以枚举源点,汇点,最大流. 这里用DFS,状态定义:分成两个集合,刚开始S集合全部点,然后一个一个放,这是一个回溯的过程. 没剪枝也过了. 剪枝技 ...

  8. Springmvc+Mybatis+Velocity实现小demo(Maven项目)

    转:https://blog.csdn.net/FoolishAndStupid/article/details/52005934 Velocity只是充当一个展示层,和JSP的功能类似,利用myba ...

  9. pooling、relu、convolution的反向传播

    1.pooling的反向传播: https://blog.csdn.net/qq_21190081/article/details/72871704 pooling反向传播的原则:pooling的值和 ...

  10. asp.net 过滤器

    asp.net 制作过滤器原理:重写ASP.net管道事件 1.通过HttpApplicationFactory创建一个HttpApplication对象,负责处理整个请求. 2.调用ProcessR ...