实例:

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

    (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. Sharepoint学习笔记—修改SharePoint的Timeouts (Execution Timeout)

    有时在Sharepoin中有些执行任务可能会超过Sharepoint环境默认的Timout限制,这种情况下系统会报"Request Timed out"错误.对此我们可以在两个层次 ...

  2. Anaconda中spyder 安装tensorflow

    关于Anaconda的安装就不介绍了,本文主要介绍spyder中安装 tensorflow.废话少说 直接重点: 1.安装好Anaconda之后,找到spyder图标 点击install,等待安装完成 ...

  3. Selenium2学习(十五)-- 单选框和复选框(radiobox、checkbox)

    本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...

  4. 2.安装 Android SDK

    安装Android SDK Android SDK(Software Development Kit,软件开发工具包)提供了 Android API 库和开发工具构建,测试和调试应用程序.简单来讲,A ...

  5. PWM----调节LED亮度

    - - --调节两个LED灯亮度 module led_pwm ( clk, rst, //cnt1_pwm, out1, out2, out3, out4 ); input clk, rst; // ...

  6. 团队合作之Scrum

    CCSU小助手 一:开发团队简介 队名:瓜队 组员:钟文兴.周畅.吉刘磊.唐仲勋 宣言:We are a team at any time! 团队项目描述: 内容:“生活在长大”: 目标:为了方便对学 ...

  7. Android数字签名解析(一)

     一.数字签名概述 所谓"数字签名"就是通过某种password运算生成一系列符号及代码组成电子password进行签名,来取代书写签名或印章. 数字签名有两种功效:一是能确定消息 ...

  8. cftool拟合&函数逼近

    cftool拟合&函数逼近 cftool 真是神奇,之前我们搞的一些线性拟合解方程,多项式拟合,函数拟合求参数啊,等等. 已经超级多了,为啥还得搞一个cftool拟合啊?而且毫无数学理论. 如 ...

  9. java实现权重随机算法

    权重随机算法在抽奖,资源调度等系统中应用还是比较广泛的,一个简单的按照权重来随机的实现,权重为几个随机对象(分类)的命中的比例,权重设置越高命中越容易,之和可以不等于100: 简单实现代码如下: im ...

  10. MYSQL添加外键关联

    SELECT * from stu st,course co,score sc where st.sid = sc.sid and sc.cid = co.cid 如果我们要给 sid 做一个约束,即 ...