C语言实例解析精粹学习笔记——36(模拟社会关系)
实例:
设计一个模拟社会关系的数据结构,每个人的信息用结构表示,包含名字、性别和指向父亲、母亲、配偶、子女的指针(只限两个子女)。要求编写以下函数:
(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(模拟社会关系)的更多相关文章
- C语言实例解析精粹学习笔记——18
<C语言实例解析精粹>中编译环境采用的是Turbo C 2.0.但是这个编译器年代久远,较新的编译器对书中的某些例子支持不好,在学习的时候同时做一些笔记. 实例18:将一个无符号整数转换为 ...
- C语言实例解析精粹学习笔记——35(报数游戏)
实例35: 设由n个人站成一圈,分别被编号1,2,3,4,……,n.第一个人从1开始报数,每报数位m的人被从圈中推测,其后的人再次从1开始报数,重复上述过程,直至所有人都从圈中退出. 实例解析: 用链 ...
- C语言实例解析精粹学习笔记——42(插入排序)
实例说明: 将一个整数数组按从小到大的顺序进行排序.(主要学习基本的插入排序和改进的冒泡排序的算法和应用) 思路1: 从第一个数据开始,分别比较其后的数据,若比它小,则将这两个数的位置交换:从第一个数 ...
- C语言实例解析精粹学习笔记——32
实例32: 编制一个包含姓名.地址.邮编和电话的通讯录输入和输出函数. 思路解析: 1.用结构体来完成姓名.地址.邮编和电话的组合. 2.结构体指针的使用. 3.malloc的使用 4.scanf函数 ...
- C语言实例解析精粹学习笔记——31
实例31: 判断字符串是否是回文 思路解析: 引入两个指针变量(head和tail),开始时,两指针分别指向字符串的首末字符,当两指针所指字符相等时,两指针分别向后和向前移动一个字符位置,并继续比较, ...
- C语言实例解析精粹学习笔记——30
实例30: 用已知字符串s中的字符,生成由其中n个字符组成的所有字符排列.设n小于字符串s的字符个数,其中s中的字符在每个排列中最多出现一次.例如,对于s[]="abc",n=2, ...
- C语言实例解析精粹学习笔记——28
实例28:从键盘读入实数 题目要求: 编制一个从键盘读入实数的函数readreal(double *rp).函数将读入的实数字符列转换成实数后,利用指针参数rp,将实数存于指针所指向的变量*rp. 思 ...
- C语言实例解析精粹学习笔记——19
实例19:判断正整数n的d进制表示形式是否是回文数(顺着看和倒着看相同的数). 主要思路: 一种方法:将正整数n数转换成d进制的数,逐个比较首尾对应数字,判断是否为回文数. 另一种方法:将正整数n数转 ...
- C语言实例解析精粹学习笔记——43(希尔排序)
实例说明: 用希尔排序方法对数组进行排序.由于书中更关注的实例,对于原理来说有一定的解释,但是对于第一次接触的人来说可能略微有些简略.自己在草稿纸上画了好久,后来发现网上有好多很漂亮的原理图. 下面将 ...
随机推荐
- 常见WEB开发安全漏洞 原因分析及解决
目 录 1 会话标识未更新 3 1.1 原因 3 1.2 解决 3 2 SQL注入 3 2.1 原因 3 2.2 解决 5 3 XSS跨站脚本编制 5 3.1 原因 5 3.2 解决 5 4 XSRF ...
- C# 轻松读取、改变文件的创建、修改、访问时间 z
// 读取文件的创建.修改.访问时间FileInfo fi = new FileInfo("C://test.txt");Console.WriteLine(fi.Creation ...
- 【Leetcode】【Easy】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是不是回文整数(例12321).不能使 ...
- PHP:使用php,循环html中的select标签与Php数据
select标签,我们都知道是下拉列表,这里,我们使用foreach循环,将select中的数据进行输出 例子: 1.数据表:mimi_article,表中有个字段,为1或0,表示着是或否 2.通过p ...
- February 24 2017 Week 8 Friday
If you fail, don't forget to learn your lesson. 如果你失败了,千万别忘了汲取教训. Frankly speaking, it is easy to ta ...
- 通过describe命令学习Kubernetes的pod属性详解
我们可以首先使用kubectl get pods命令得到pod列表,比如我们想研究pod nginx-storage-pod的明细: 使用命令kubectl describe pod nginx-st ...
- 另一种方式实现事务码SE16里的结果集修改
注: 这种方法不同于网上流传的在调试器里修改fcode的那种解决方案. 使用场景:我们需要直接在开发系统的事务码SE16里修改某些结果集的值,但是在SE16的工具栏里看不见修改按钮: 解决方案 使用/ ...
- mysql数据库 BETWEEN 语法的用法和边界值解析
between用法: 用于where表达式中,选取两个值之间的数据,如: SELECT id FROM user WHERE id BETWEEN value1 AND value2; 当betwee ...
- CodeForces 30C Shooting Gallery 简单dp
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/36944227 题目链接:点击打开链接 给定 ...
- TSP 遗传算法
GA——遗传算法 同模拟退火算法一样,都是现代优化算法之一.模拟退火是在一定接受程度的情况下仍然接受一个比较差的解. 遗传算法,是真真正正的和大自然的遗传进化有着非常紧密的联系的,当然遗传进化的只是在 ...