本實例有求設計一個模擬社會關系的數據結構,每個人的信息用結構表示,包含名字,性別和指向父親,母親,配偶,子女的指針(設只限兩個子女)。要求編寫以下函數:
1. 增加一個新人的函數
1. 建立人與人之間關系的函數,父子 、母子、配偶等
1. 檢查某兩人之間是否是堂兄妹
該實例的主要目的是聯系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;
}
~~~
總體來說,該實例并不難,并沒有涉及到比較復雜的算法,其中稍微有些需要考慮的地方就是在判斷兩個人是否是堂兄妹的時候,用到了一點小方法,也不是很難。
下面我們來看一下程序的運行結果:

- 前言
- 實例一:HelloWorld
- scanf函數學習
- 實數比較
- sizeof()保留字獲取類型的大小
- 自增/自減學習
- C學習if條件判斷和for循環
- C實現的九九乘法表
- C實現一個比較簡單的猜數游戲
- 使用C模擬ATM練習switch..case用法
- 記錄一個班級的成績練習一維數組
- C數組實現矩陣的轉置
- C二維數組練習
- 利用數組求前n個質數
- C實現萬年歷
- C實現數組中元素的排序
- C實現任意進制數的轉化
- C判斷一個正整數n的d進制數是否是回文數
- C使用遞歸實現前N個元素的和
- 鋼材切割問題
- 使用指針比較整型數據的大小
- 指向數組的指針
- 尋找指定元素
- 尋找相同元素的指針
- 整數轉換成羅馬數字
- 字符替換
- 從鍵盤讀入實數
- C實現字符行排版
- C實現字符排列
- C實例--判斷一個字符串是否是回文數
- 通訊錄的輸入輸出
- 撲克牌的結構定義
- 使用“結構”統計學生成績
- 報數游戲
- 模擬社會關系
- 統計文件中字符個數
- C實現兩個文件的內容輸出到同一個屏幕