实例:

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

    (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("\n");
 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("\n");
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.\n",pDaughter->name,pCousin->name);
155     else
156         printf("%s and %s are brothers (sisters) in law.\n",pDaughter->name,pCousin->name);
157     if(!brothersinlaw(pSon,pCousin))
158         printf("%s and %s are not brothers (sisters) in law.\n",pSon->name,pCousin->name);
159     else
160         printf("%s and %s are brothers (sisters) in law.\n",pSon->name,pCousin->name);
161     if(!brothersinlaw(pSon,pDaughter))
162         printf("%s and %s are not brothers (sisters) in law.\n",pSon->name,pDaughter->name);
163     else
164         printf("%s and %s are brothers (sisters) in law.\n",pSon->name,pDaughter->name);
165
166     //printf("Hello world!\n");
167     return 0;
168 }

转载于:https://www.cnblogs.com/llccbb1/p/9800169.html

C语言实例解析精粹学习笔记——36(模拟社会关系)相关推荐

  1. c语言编程实例解析精粹,C语言实例解析精粹学习笔记——35(报数游戏)

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

  2. c语言求三个整数的积,反汇编学习-C语言实例解析精粹-实例3求整数之积

    序言 为了提高可读性,我添加了这一段,另外由于我用的是VS2017,会出现一些奇怪的错误,也一并在这里解决. 例如本次出现了这个错误(安全检查错误):错误 C4996 'scanf': This fu ...

  3. c语言/c++转Java学习笔记---基础问题

    c语言/c++转Java学习笔记---基础问题 1.java注释 2.数组的定义和使用 定义 使用 3.类 4.this 的使用 5.继承 6.super的使用 7.包 8.修饰符 成员的访问控制符( ...

  4. R语言与函数估计学习笔记(函数模型的参数估计)

    R语言与函数估计学习笔记 毫无疑问,函数估计是一个比参数估计要复杂得多的问题,当然也是一个有趣的多的问题.这个问题在模型未知的实验设计的建模中十分的常见,也是我正在学习的内容的一部分. 关于函数估计我 ...

  5. R语言与抽样技术学习笔记(Jackknife)

    R语言与抽样技术学习笔记(Randomize,Jackknife,bootstrap) Jackknife算法 Jackknife的想法在我很早的一篇博客<R语言与点估计学习笔记(刀切法与最小二 ...

  6. python爬虫学习笔记2模拟登录与数据库

    前言 为了加入学校里面一个技术小组,我接受了写一个爬取学校网站通知公告的任务.这个任务比以前写的爬虫更难的地方在于,需要模拟登录才能获得页面,以及将得到的数据存入数据库. 本文按照日期来记录我完成任务 ...

  7. [原创]java WEB学习笔记36:Java Bean 概述,及在JSP 中的使用,原理

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. linux软件包管理解析,linux学习笔记_09_软件包管理解析.doc

    linux学习笔记_09_软件包管理解析 软件包管理 软件包分类 源码包(C语言编写的源代码) linux主要由C语言来写. 源码包可以用写字板打开 脚本安装包:源码包进行再开发的源码包(提供安装界面 ...

  9. C语言五子棋人人对弈学习笔记

    C语言编写五子棋人人对弈学习笔记 1.头文件#include <conio.h> #include <conio.h>是一个控制输出的头文件. 包含以下函数:textbackg ...

  10. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

最新文章

  1. PubChem分子库
  2. 关于C语言中的预处理器的简单笔记
  3. leetcode 448. Find All Numbers Disappeared in an Array
  4. 【Android TV 开发】焦点处理 ( 父容器与子组件焦点获取关系处理 | 不同电视设备上的兼容问题 | 触摸获取焦点 | 按键获取焦点 )
  5. 【PC工具】chrome插件: Github 项目代码树形格式展示工具octotree
  6. 字典树实现_反怼面试官系列之 字典树
  7. linux c语言 readline,Linux C代码实现读取配置文件示例
  8. employee setup in Organization unit
  9. 在ACCESS中使用Group By语句
  10. jdk1.8 base64注意事项
  11. 关于JPA方法名创建自动查询
  12. React的组件模式 1
  13. linux之rpm命令
  14. 1.material组件的安装及其使用
  15. 数据结构:实验一 线性表的基本功能实现
  16. golang echo框架案例
  17. php教程phpmeng,李炎恢PHP培训视频教程
  18. 苹果4怎么越狱_它的维生素C含量是苹果的4倍,是我国第4大主粮,土豆怎么种植的...
  19. 人体疲劳程度检测,生理信号处理
  20. 信息学奥赛一本通:1134:合法C标识符查

热门文章

  1. SpringBoot @Value 读取配置,太强大了!
  2. 掌握了这30道MySQL基础面试题,我成了面霸
  3. 疑似 B 站后台源码泄露,ikun 潜入?
  4. Android 11 Meetup 上海站!来了!
  5. ios时间相差多少天_iOS 时间戳和时间互换,计算两日期相隔天数
  6. python协程调度方式_python 3.x 学习笔记17(协程以及I/O模式)
  7. mysql基础之数据库备份和恢复的基础知识
  8. 微信小程序wx.navigateTo无法跳转
  9. 反向传播与梯度消失梯度爆炸
  10. 如何避免 $_SERVER[PHP_SELF] 被利用?