模拟社会关系

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

  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;
}

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

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

这里写图片描述

posted @ 2015-07-11 10:41  陈洪波  阅读(208)  评论(0编辑  收藏  举报