实例:

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

    (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. 视频监控——从其他浏览器打开低版本IE方案

    1. 方案背景 由于低版本IE浏览器并不支持很多新的页面技术,导致部分页面效果难以实现;另一方面IE浏览器版本与操作系统绑定,难以统一,不同版本IE间的不兼容导致多种兼容性问题,因此本项目暂定采用Ch ...

  2. C#实现Stream与byte[]之间的转换实例教程

    一.二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...

  3. SpringMvc-view

    1.view视图:及springmvc返回到前端的页面,前面的所有跳转都是view的列子在此就不在举例了 2.在view界面中如何实现国际化? 2.1实现国际化有首先需要配置国际化资源文件:例如 英文 ...

  4. 【Spring实战】—— 15 Spring JDBC模板使用

    前一篇通过对传统的JDBC的使用操作,可以体会到使用的繁琐与复杂,套句话说,是用了20%作了真正的工作,80%作了重复的工作. 那么通过本篇,可以了解如下的内容: 1 如何配置数据源 2 如何在spr ...

  5. (转)sizeof()和_countof()

    (转)sizeof()和_countof()   先看程序: #include <iostream> using namespace std; int main(int argc, cha ...

  6. SVN cleanup 反复失败解决办法

    svn cleanup cleaning up 操作反复失败,svn提示的问题是版本需要更新,更新成最新的版本之后,依旧反复失败,陷入死循环.还好找一个blog上的方法试了一下,成功了. 先说故障环境 ...

  7. pinyin4j工具类

    <!-- 导入pinyin4j --> <dependency> <groupId>com.belerweb</groupId> <artifac ...

  8. ueditor1.2.6图片被压缩的解决办法

    修改文件路径: ueditor\dialogs\image\image.html 修改数值:

  9. SignalR中的依赖注入

    什么是依赖注入? 如果你已经熟悉依赖注入可以跳过此节. 依赖注入 (DI) 模式下,对象并不为自身的依赖负责. 下边的例子是一个主动 DI. 假设你有个对象需要消息日志.你可能定义了一个日志接口: C ...

  10. C#中的代码书写规范以及命名规范

    C#代码书写规则: 1. 尽量使用接口,然后使用类实现接口,以提高程序的灵活性. 2.一行不要超过80个字符 3.尽量不要手动更改计算机生成的代码 4.关键的语句写注释 5.建议局部变量在最接近使用它 ...