<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                本實例有求設計一個模擬社會關系的數據結構,每個人的信息用結構表示,包含名字,性別和指向父親,母親,配偶,子女的指針(設只限兩個子女)。要求編寫以下函數: 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; } ~~~ 總體來說,該實例并不難,并沒有涉及到比較復雜的算法,其中稍微有些需要考慮的地方就是在判斷兩個人是否是堂兄妹的時候,用到了一點小方法,也不是很難。 下面我們來看一下程序的運行結果: ![這里寫圖片描述](https://box.kancloud.cn/2016-05-24_5743c0acd746d.jpg "")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看