模擬社會關系_第1頁
模擬社會關系_第2頁
模擬社會關系_第3頁
模擬社會關系_第4頁
模擬社會關系_第5頁
已閱讀5頁,還剩19頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

模擬社會關系C程序設計模擬社會關系設計一個模擬社會關系的數(shù)據(jù)結構,每個人的信息用結構體表示,包含姓名、性別和指向父親、母親、配偶、子女的指針(設只限兩個子女)。要求編寫以下函數(shù):(1)增加一個新人的函數(shù);(2)建立人與人之間關系的函數(shù):父-子、母-子、配偶等;(3)檢查某兩人之間是否為堂兄妹。CONTENTS01編程思路分析基礎知識點具體實現(xiàn)總結與拓展030204編程思路分析1.編程思路分析定義structperson結構體類型用于描述個人信息。1定義函數(shù)newperson()用于往社會關系中增加一個新人。2定義函數(shù)father_child()、mother_child()、mate()分別用于建立父子關系、母子關系以及配偶關系。

3定義函數(shù)brothersinlaw()用于判斷兩人之間是否為堂兄妹;定義函數(shù)print_relate()用于輸出人物關系。4在主函數(shù)中調(diào)用以上各函數(shù)用于實現(xiàn)程序的功能。5基礎知識點2.基礎知識點struct[結構體類型名]{

數(shù)據(jù)類型名1成員名1;數(shù)據(jù)類型名2成員名2;

……

數(shù)據(jù)類型名n成員名n;};結構體類型的定義形式為:structperson{charname[20];/*人的姓名*/charsex;/*性別,'M'表示男性,'F'表示女性*/structperson*father;/*該人的父親*/structperson*mother;/*該人的母親*/structperson*mate;/*該人的配偶*/structperson*childs[CHILDREN];/*該人的孩子*/};定義個人信息的結構體類型:2.基礎知識點結構體類型的數(shù)據(jù)在函數(shù)間的傳遞(3)用指向結構體變量(或結構體數(shù)組)的指針作實參,將結構體變量(或數(shù)組)的地址傳遞給形參,屬于“傳址”方式。這種方式下,實參和形參具有相同的地址,對形參的操作即是對實參的操作。這是一種最常使用的方法。要將一個結構體變量的值傳遞給另一個函數(shù),有三種方法:(1)用結構體變量的成員作參數(shù)。用法和用普通變量作實參是一樣的,屬于“值傳遞”方式。(2)用結構體變量作參數(shù)。用結構體變量作實參時,采取的也是“值傳遞”方式,將結構體變量所占的內(nèi)存單元的內(nèi)容全部按順序傳遞給形參,形參也必須是同類型的結構體變量。由于采用的是“值傳遞”的方式,如果在執(zhí)行被調(diào)函數(shù)期間改變了形參的值,該值不能返回給主調(diào)函數(shù),造成使用上的不便。具體實現(xiàn)3.具體實現(xiàn)定義structperson結構體類型用于描述個人信息。1#defineCHILDREN2structperson{charname[20];/*人的姓名*/charsex;/*性別,'M'表示男性,'F'表示女性*/structperson*father;/*該人的父親*/structperson*mother;/*該人的母親*/structperson*mate;/*該人的配偶*/structperson*childs[CHILDREN];/*該人的孩子*/};3.具體實現(xiàn)定義函數(shù)newperson()用于往社會關系中增加一個新人。2/*[函數(shù)]添加一個新人*/structperson*newperson(char*name,charsex){inti;structperson*p=(structperson*)malloc(sizeof(structperson));strcpy(p->name,name);p->sex=sex;p->father=NULL;p->mother=NULL;p->mate=NULL;for(i=0;i<CHILDREN;i++) p->childs[i]=NULL;returnp;};3.具體實現(xiàn)定義函數(shù)father_child()、mother_child()、mate()分別用于建立父子關系、母子關系以及配偶關系。

3/*[函數(shù)]建立父子關系*/voidfather_child(structperson*father,structperson*child){intindex;for(index=0;index<CHILDREN-1;index++)/*尋找一個空缺的位置*/if(father->childs[index]==NULL)/*如果沒有,則放到最后*/break;father->childs[index]=child;child->father=father;}3.具體實現(xiàn)定義函數(shù)father_child()、mother_child()、mate()分別用于建立父子關系、母子關系以及配偶關系。

3/*[函數(shù)]建立母子關系*/voidmother_child(structperson*mother,structperson*child){intindex;for(index=0;index<CHILDREN-1;index++)/*尋找一個空缺的位置*/if(mother->childs[index]==NULL)/*如果沒有,則放到最后*/break;mother->childs[index]=child;child->mother=mother;}3.具體實現(xiàn)定義函數(shù)father_child()、mother_child()、mate()分別用于建立父子關系、母子關系以及配偶關系。

3/*[函數(shù)]mate建立配偶關系*/voidmate(structperson*h,structperson*w){h->mate=w;w->mate=h;}3.具體實現(xiàn)定義函數(shù)brothersinlaw()用于判斷兩人之間是否為堂兄妹;定義函數(shù)print_relate()用于輸出人物關系。4/*[函數(shù)]判斷是否為堂兄妹*/intbrothersinlaw(structperson*p1,structperson*p2){structperson*f1,*f2;if(p1==NULL||p2==NULL||p1==p2)return0;if(p1->sex==p2->sex)return0;/*不可能是堂兄妹*/f1=p1->father;f2=p2->father;if(f1!=NULL&&f1==f2)return0;/*是兄妹,不是堂兄妹*/while(f1!=NULL&&f2!=NULL&&f1!=f2)/*遠親*/{f1=f1->father;f2=f2->father;if(f1!=NULL&&f2!=NULL&&f1==f2)return1;}return0;}3.具體實現(xiàn)/*[函數(shù)]輸出人物關系*/voidprint_relate(structperson*p){intindex,i;

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

if(p->sex=='M')printf("%sismale.\n",p->name);elseprintf("%sisfemale.\n",p->name);

if(p->father!=NULL)printf("%s'sfatheris%s.\n",p->name,p->father->name);if(p->mother!=NULL)printf("%s'smotheris%s.\n",p->name,p->mother->name);定義函數(shù)brothersinlaw()用于判斷兩人之間是否為堂兄妹;定義函數(shù)print_relate()用于輸出人物關系。43.具體實現(xiàn)/*續(xù)函數(shù)print_relate()*/if(p->mate!=NULL)if(p->sex=='M')printf("Hiswifeis%s.\n",p->mate->name);elseprintf("Herhusbandis%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("Childrenare:");for(i=0;i<index;i++)printf("%s\t",p->childs[i]->name);}printf("\n");}定義函數(shù)brothersinlaw()用于判斷兩人之間是否為堂兄妹;定義函數(shù)print_relate()用于輸出人物關系。43.具體實現(xiàn)在主函數(shù)中調(diào)用以上各函數(shù)用于實現(xiàn)程序的功能。5John(M)Herry(M)Jason(M)Kate(F)Peter(M)Maggie(F)Jenny(M)Marry(F)pGrandfatherpFather1pMother1pSonpfather2pMother2pDaughterpCousin父子父子父子父子父子配偶配偶母子母子母子char*name[8]={"John","Kate","Maggie","Herry","Jason","Peter","Marry","Jenny"};charmale='M',female='F';structperson*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);3.具體實現(xiàn)在主函數(shù)中調(diào)用以上各函數(shù)用于實現(xiàn)程序的功能。5/*輸出各種關系*/print_relate(pGrandfather);print_relate(pFather1);print_relate(pFather2);print_relate(pMother1);print_relate(pMother2);print_relate(pSon);print_relate(pDaughter);print_relate(pCousin);3.具體實現(xiàn)if(!brothersinlaw(pDaughter,pCousin))printf("%sand%sarenotbrothers(sisters)inlaw.\n",pDaughter->name,pCousin->name);elseprintf("%sand%sarebrothers(sisters)inlaw.\n",pDaughter->name,pCousin->name);if(!brothersinlaw(pSon,pCousin))printf("%sand%sarenotbrothers(sisters)inlaw.\n",pSon->name,pCousin->name);elseprintf("%sand%sarebrothers(sisters)inlaw.\n",pSon->name,pCousin->name

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論