模拟社会关系

简介: 版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/46840015 本实例有求设计一个模拟社会关系的数据结构,每个人的信息用结构表示,包含名字,性别和指向父亲,母亲,配偶,子女的指针(设只限两个子女)。
版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/46840015

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

  1. 增加一个新人的函数
  2. 建立人与人之间关系的函数,父子 、母子、配偶等
  3. 检查某两人之间是否是堂兄妹

该实例的主要目的是联系C中结构体的使用,下面是函数的实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CHILDREN 2

/**
 * 设计一个模拟社会关系的数据结构,每个人的信息用结构表示,包含名字,
 * 性别和指向父亲,母亲,配偶,子女的指针(设只限两个子女)。要求编写
 * 以下函数:(1)增加一个新人的函数 (2)建立人与人之间关系的函数,父子
 * 、母子、配偶等 (3)检查某两人之间是否是堂兄妹
 */

struct person{
    char *name; /* 人的姓名 */
    char sex;   /* 性别,'M'表示男性,'F'表示女性 */
    struct person *father; /* 该人的父亲 */
    struct person *mother; /* 该人的母亲 */
    struct person *mate;  /* 该人的配偶 */
    struct person *childs[CHILDREN]; /* 该人的孩子 */
};

/* [函数] 添加一个新人 */
struct person * newperson(char *name,char sex){
    struct person *p = (struct person *)malloc(sizeof(struct person));
    p->name = (char *)malloc(sizeof(name)+1);
    strcpy(p->name,name);

    p->sex = sex;

    p->father = NULL;
    p->mother = NULL;
    p->mate = NULL;

    int i = 0;
    for(i = 0;i < CHILDREN;i++){
        p->childs[i] = NULL;
    }

    return p;
};

/* [函数] 建立父子关系 */
void father_child(struct person *father,struct person *child){

    int index;

    for(index = 0;index < CHILDREN-1;index++) /* 寻找一个空缺的位置 */
        if(father->childs[index] == NULL)       /* 如果没有,则放到最后 */
            break;

    father->childs[index] = child;
    child->father = father;
}

/* [函数] 建立母子关系 */
void mother_child(struct person *mother,struct person *child){
    int index;

    for(index = 0;index < CHILDREN-1;index++) /* 寻找一个空缺的位置 */
        if(mother->childs[index] == NULL)       /* 如果没有,则放到最后 */
            break;

    mother->childs[index] = child;
    child->mother = mother;
}

/* [函数] mate 建立配偶关系 */
void mate(struct person *h,struct person *w){

    /* 建立配偶关系 */
    h->mate = w;
    w->mate = h;
}

/** [函数] 判断是否为堂兄妹
 *  params:
 *      struct person *p1:被判断的人
 *      struct person *p2:被判断的人
 * return:
 *      0:不是堂兄妹关系
 *      1:是堂兄妹关系
 */

 int brothersinlaw(struct person *p1,struct person *p2)
 {
     struct person *f1,*f2;

     if(p1 == NULL || p2 == NULL || p1 == p2) return 0;

     if(p1->sex == p2->sex) return 0; /* 不可能是堂兄妹*/

     f1 = p1->father;
     f2 = p2->father;

     if(f1 != NULL &&f1 == f1)
        return 0; /* 是兄妹,不是堂兄妹 */

     while(f1 != NULL && f2 != NULL && f1 != f2) /* 远亲 */
     {
        f1 = f1->father;
        f2 = f2->father;

        if(f1 != NULL && f2 != NULL && f1 == f2) return 1;
     }

     return 0;
 }

/* [函数] 输出人物关系 */
void print_relate(struct person *p)
{
    int index,i;

    if(p->name == NULL)
        return;

    if(p->sex == 'M')
        printf("%s is male.\n",p->name);
    else
        printf("%s is female.\n",p->name);

    if(p->father != NULL)
        printf("%s's father is %s.\n",p->name,p->father->name);
    if(p->mother != NULL)
        printf("%s's mother is %s.\n",p->name,p->mother->name);

    if(p->mate != NULL)
        if(p->sex == 'M')
            printf("His wife is %s.\n",p->mate->name);
        else
            printf("Her husband is %s.\n",p->mate->name);

    if(p->childs != NULL){
        for(index = 0;index <CHILDREN-1;index++)
            if(p->childs[index] == NULL)
                break;

        if(index > 0)
            printf(" Children are : ");
        for(i = 0;i < index;i++)
            printf("%s\t",p->childs[i]->name);
    }

    printf("\n");
}

int main()
{
    char *name[8]={"John","Kate","Maggie","Herry","Jason","Peter","Marry","Jenny"};
    char male='M',female='F';
    struct person *pGrandfather,*pFather1,*pFather2,*pMother1,*pMother2,*pSon,*pDaughter,*pCousin;

    pGrandfather = newperson(name[0],male);
    pFather1 = newperson(name[3],male);
    pFather2 = newperson(name[4],male);
    pMother1 = newperson(name[1],female);
    pMother2 = newperson(name[2],female);
    pSon = newperson(name[5],male);
    pDaughter = newperson(name[6],female);
    pCousin = newperson(name[7],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);

    return 0;
}

总体来说,该实例并不难,并没有涉及到比较复杂的算法,其中稍微有些需要考虑的地方就是在判断两个人是否是堂兄妹的时候,用到了一点小方法,也不是很难。

下面我们来看一下程序的运行结果:

这里写图片描述

目录
相关文章
|
4月前
免费图片在线压缩工具
在线图片压缩,快速减小图片大小,不损失太多画质
789 0
|
安全 数据建模 网络安全
2024阿里云双11,WoSign SSL证书优惠券使用攻略
2024阿里云“11.11金秋云创季”活动主会场,阿里云用户通过完成个人或企业实名认证,可以领取不同额度的满减优惠券,叠加折扣优惠。用户购买WoSign SSL证书,如何叠加才能更加优惠呢?
1224 3
|
6月前
|
人工智能 算法 安全
AI智能体热潮下,打工人如何抢占未来职场先机?
当AI成为生活与工作的基础设施,生成式人工智能认证(GAI认证)为职场人提供了应对变革的关键工具。文章从AI智能体浪潮引发的职业革命出发,分析了技能需求重构、职业边界模糊及伦理责任升级的趋势,强调GAI认证通过系统性知识框架、全球认可的权威性和技术伦理教育,帮助个人从“工具使用者”转型为“规则制定者”。无论是传统行业从业者还是技术专家,GAI认证都能提升其在AI时代的竞争力,成为职业发展的护城河与未来入场券。掌握AI不是选择,而是必然,而GAI认证正是通向未来的桥梁。
|
11月前
|
运维 监控 安全
天财商龙:云上卓越架构治理实践
天财商龙成立于1998年,专注于为餐饮企业提供信息化解决方案,涵盖点餐、收银、供应链和会员系统等。自2013年起逐步实现业务上云,与阿里云合作至今已十年。通过采用阿里云的WA体系,公司在账号管理、安全保障、监控体系和成本管控等方面进行了全面优化,提升了业务稳定性与安全性,并实现了显著的成本节约。未来,公司将持续探索智能化和全球化发展,进一步提升餐饮行业的数字化水平。
|
12月前
|
监控 安全
网络传输介质
本文介绍了有线传输介质和无线传输介质。有线传输介质包括双绞线、同轴电缆和光纤,其中双绞线因其成本低、安装便捷而广泛应用;同轴电缆适合长距离传输视频信号;光纤则具有高速、抗干扰等优势。无线传输介质涵盖无线电波、微波、红外线和蓝牙,适用于不同场景下的无线通信需求。
511 1
网络传输介质
|
JavaScript API 开发者
掌握ArkTS,打造HarmonyOS应用新视界:从“Hello World”到状态管理,揭秘鸿蒙UI开发的高效秘诀
【10月更文挑战第19天】ArkTS(ArkUI TypeScript)是华为鸿蒙系统中用于开发用户界面的声明式编程语言,结合了TypeScript和HarmonyOS的UI框架。本文介绍ArkTS的基本语法,包括组件结构、模板和脚本部分,并通过“Hello World”和计数器示例展示其使用方法。
642 1
|
缓存 监控 数据库
性能优化:解锁应用与系统的极致速度
【10月更文挑战第20天】性能优化:解锁应用与系统的极致速度
316 0
|
索引 Windows
软件设计师软考题目解析16 --每日五题
这篇文章提供了软件设计师软考的每日五题解析,涵盖了文件系统崩溃影响、多级索引结构、位示图大小计算、文件关联以及位示图大小需求等知识点。
259 0
|
物联网 人机交互 语音技术
计算机中输入输出设备
【7月更文挑战第28天】
358 1