數(shù)據(jù)結(jié)構(gòu)家譜管理系統(tǒng)_第1頁(yè)
數(shù)據(jù)結(jié)構(gòu)家譜管理系統(tǒng)_第2頁(yè)
數(shù)據(jù)結(jié)構(gòu)家譜管理系統(tǒng)_第3頁(yè)
數(shù)據(jù)結(jié)構(gòu)家譜管理系統(tǒng)_第4頁(yè)
數(shù)據(jù)結(jié)構(gòu)家譜管理系統(tǒng)_第5頁(yè)
已閱讀5頁(yè),還剩32頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

精品文檔精心整理PAGE精品文檔可編輯的精品文檔精品文檔精心整理精品文檔可編輯的精品文檔《項(xiàng)目實(shí)訓(xùn)二》項(xiàng)目名稱__家譜管理系統(tǒng)__ 姓名_____________ 班級(jí)_________________ 學(xué)號(hào)__________________ 指導(dǎo)教師______________2018.1問(wèn)題描述:家譜用于記錄某家族歷代家族成員的情況與關(guān)系?,F(xiàn)編制一個(gè)家譜資料管理系統(tǒng),實(shí)現(xiàn)對(duì)一個(gè)家族所有的資料進(jìn)行收集整理。實(shí)現(xiàn)對(duì)家庭成員信息的建立、查找、插入、修改、增加、刪除、更新、統(tǒng)計(jì)等等功能。目的和要求:目的:能根據(jù)具體問(wèn)題的具體情況,結(jié)合數(shù)據(jù)結(jié)構(gòu)課程中的基本理論和基本算法,分析并正確確定數(shù)據(jù)的邏輯結(jié)構(gòu),合理地選擇相應(yīng)的存儲(chǔ)結(jié)構(gòu),并能設(shè)計(jì)出解決問(wèn)題的有效算法。提高程序設(shè)計(jì)和調(diào)試能力。通過(guò)上機(jī)學(xué)習(xí),驗(yàn)證自己設(shè)計(jì)的算法的正確性。學(xué)會(huì)有效利用基本調(diào)試方法。初步掌握軟件開發(fā)過(guò)程中問(wèn)題分析、系統(tǒng)設(shè)計(jì)、程序編碼、測(cè)試等基本方法和技能。培養(yǎng)根據(jù)選題需要選擇學(xué)習(xí)書籍,查閱文獻(xiàn)資料的自學(xué)能力。要求:用于記錄某家族歷代家族成員的情況與關(guān)系?,F(xiàn)編制一個(gè)家譜資料管理系統(tǒng),實(shí)現(xiàn)對(duì)一個(gè)家族所有的資料進(jìn)行收集整理。支持對(duì)家譜的增加,刪除,更新,統(tǒng)計(jì)等。軟件環(huán)境:MicrosoftVisualStudio2010流程設(shè)計(jì):開始M開始Main函數(shù)Menu函數(shù)FamilyTree函數(shù)Getroot函數(shù)Case1Case2Case3Case4Case5Case6Case7Case8Case9Case10defeault結(jié)束Creat函數(shù)函數(shù)遞歸調(diào)用Menu函數(shù)PreOrder函數(shù)函數(shù)InOrder函數(shù)函數(shù)PostOrder函數(shù)函數(shù)Generation函數(shù)函數(shù)NumberOfPeople函數(shù)函數(shù)LifeNum函數(shù)函數(shù)Message函數(shù)函數(shù)AddNewPeople函數(shù)數(shù)DeletePeople函數(shù)SetNewName函數(shù)FindChild函數(shù)SaveToFile函數(shù)FileToFamilyTree函數(shù)遞歸調(diào)用遞歸調(diào)用遞歸調(diào)用PrintMessage函數(shù)函數(shù)SetNode函數(shù)函數(shù)函數(shù)PreFindFather函數(shù)函數(shù)PreFindBrother函數(shù)函數(shù)PreFindFather函數(shù)函數(shù)PreFindBrother函數(shù)函數(shù)PrintMessage函數(shù)函數(shù)模塊劃分:統(tǒng)計(jì)模塊統(tǒng)計(jì)家族總?cè)藬?shù)、健在人數(shù)、幾代人主要函數(shù):intGeneration(Node*root);//這個(gè)家族共有幾代人intNumberOfPeople();//家族的總?cè)藬?shù)intLifeNum();//健在人數(shù)實(shí)現(xiàn)方法:靜態(tài)成員變量實(shí)現(xiàn)結(jié)果:更新模塊創(chuàng)建家譜、增加成員、刪除成員、成員改名主要函數(shù):Node*Creat();//構(gòu)造函數(shù)調(diào)用voidAddNewPeople(Node*root,stringFatherName,stringNAme);//增加新的家族成員intDeletePeople(Node*root,stringFatherName,stringDeletepeople);//刪除家族成員intSetNewName(Node*root,stringNAme,stringNewName);//更改姓名實(shí)現(xiàn)方法:創(chuàng)建家譜和成員改名主要通過(guò)遞歸調(diào)用;增加成員和刪除成員主要通過(guò)棧的非遞歸調(diào)用。實(shí)現(xiàn)結(jié)果:查詢模塊查詢成員詳細(xì)信息、查詢成員的孩子以及孩子的詳細(xì)信息主要函數(shù):intMessage(Node*root,stringName);//顯示該成員的基本信息intFindChild(Node*root,stringNAme);//顯示孩子信息實(shí)現(xiàn)方法:通過(guò)遞歸調(diào)用,找到成員,輸出相應(yīng)的信息實(shí)現(xiàn)結(jié)果:顯示模塊前序、中序、后序遍歷家譜主要函數(shù):voidPreOrder(Node*root);//前序遞歸遍歷輸出家譜voidInOrder(Node*root);//中序遞歸遍歷輸出家譜voidPostOrder(Node*root);//后序遞歸遍歷輸出家譜實(shí)現(xiàn)方法:遞歸遍歷實(shí)現(xiàn)結(jié)果:文件模塊保存到文件、從文件讀取主要函數(shù):voidSaveToFile(Node*root);//保存到文件voidFileToFamilyTree(Node*root);//從文件中讀取實(shí)現(xiàn)方法:文件流實(shí)現(xiàn)結(jié)果:數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn):生日結(jié)構(gòu)體structBirthDay{ intyear; intmonth; intday; friendistream&operator>>(istream&is,BirthDay&b); friendostream&operator<<(ostream&os,constBirthDay&b);};信息結(jié)構(gòu)體(家族成員的基本信息)structInformation{ stringname;//姓名 stringbirthPlace;//出生地 BirthDaybirthDay;//生日 stringsex;//性別 stringeducation;//學(xué)歷 stringjob;//工作 stringfather;//父親 stringspouse;//配偶 charlife;//是否健在};二叉樹結(jié)點(diǎn)結(jié)構(gòu)體structNode{ Informationdata;//個(gè)人信息 Node*child;//第一個(gè)孩子 Node*brother;//兄弟};家譜類(二叉樹結(jié)構(gòu)、左孩子,右兄弟)classFamilyTree{private: Node*root; Node*Creat();//構(gòu)造函數(shù)調(diào)用 voidRelease(Node*root);//析構(gòu)函數(shù)調(diào)用 staticintNumberofpeople;//計(jì)算總?cè)藬?shù),NumberOfPeople()調(diào)用 staticintLifePeopele;//計(jì)算健在人數(shù),LifeNum()調(diào)用public: FamilyTree();//構(gòu)造函數(shù),初始化一棵樹,其前序序列由鍵盤輸入 ~FamilyTree();//析構(gòu)函數(shù),釋放鏈表中各結(jié)點(diǎn)的存儲(chǔ)空間 voidSetNode(Node*root);//設(shè)置結(jié)點(diǎn)信息 Node*Getroot();//獲取根結(jié)點(diǎn) voidPreOrder(Node*root);//前序遞歸遍歷輸出家譜 voidInOrder(Node*root);//中序遞歸遍歷輸出家譜 voidPostOrder(Node*root);//后序遞歸遍歷輸出家譜 intGeneration(Node*root);//這個(gè)家族共有幾代人 intNumberOfPeople();//家族的總?cè)藬?shù) intLifeNum();//健在人數(shù) voidPrintMessage(Node*root);//輸出基本信息 intMessage(Node*root,stringName);//顯示該成員的基本信息 Node*PreFindFather(Node*root,stringFatherName);//給定元素值查找父親結(jié)點(diǎn)指針位置并返回其指針,此方法采用的先序遍歷 Node*PreFindBrother(Node*root,stringFatherName);//給定元素值查找兄弟結(jié)點(diǎn)指針位置并返回其指針,此方法采用的先序遍歷 voidAddNewPeople(Node*root,stringFatherName,stringNAme);//增加新的家族成員 intDeletePeople(Node*root,stringFatherName,stringDeletepeople);//刪除家族成員 intSetNewName(Node*root,stringNAme,stringNewName);//更改姓名 intFindChild(Node*root,stringNAme);//顯示孩子信息 voidSaveToFile(Node*root);//保存到文件 voidFileToFamilyTree(Node*root);//從文件中讀取};調(diào)試分析:?jiǎn)栴}:在創(chuàng)建家譜時(shí),詢問(wèn)用戶是否需要繼續(xù)添加成員,只要用戶不輸入”#”就繼續(xù)添加。解決方案:增加if語(yǔ)句判斷條件,只要輸入的不是”Y”,”y”,”#”,就請(qǐng)用戶重新輸入。問(wèn)題:計(jì)算總?cè)藬?shù)和健在人數(shù),因?yàn)榇嬖谠黾雍蛣h除函數(shù),多次調(diào)用計(jì)算函數(shù)。解決方案:在家譜類中使用靜態(tài)成員變量問(wèn)題:在輸入和輸出成員信息中的生日,生日使用的是生日結(jié)構(gòu)體變量,輸入輸出包括年、月、日。解決方案:使用友元輸入輸出重載問(wèn)題:在輸入生日時(shí),輸入數(shù)字程序正常運(yùn)行,輸入其他字符,程序會(huì)出現(xiàn)死循環(huán)。解決方案:cin.fail()判斷輸入是否正確,cin.clear()為了使輸入錯(cuò)誤能重新輸入,將錯(cuò)誤標(biāo)識(shí)符改為0,cin.sync()清空流。問(wèn)題:在輸入一些信息是,詢問(wèn)用戶是否確認(rèn)一些信息時(shí),請(qǐng)用戶輸入‘y、‘n’,但是用戶有時(shí)會(huì)輸入大寫。解決方案:使用toupper()函數(shù),將用戶輸入的確認(rèn)信息轉(zhuǎn)換成大寫字母。問(wèn)題:在增加孩子時(shí),只能添加長(zhǎng)子,添加第二個(gè)孩子、第三個(gè)等等,會(huì)出現(xiàn)錯(cuò)誤。解決方案:添加一個(gè)尋找兄弟指針的函數(shù),如果要添加孩子的成員,已經(jīng)有了孩子,則通過(guò)調(diào)用兄弟指針函數(shù)來(lái)增加孩子。問(wèn)題:保存到文件和讀取文件時(shí),會(huì)出現(xiàn)輸入路徑錯(cuò)誤的情況。解決方案:通過(guò)調(diào)用_access()函數(shù),判斷輸入路徑是否正確。問(wèn)題:讀取文件時(shí),cin的>>重載會(huì)跳過(guò)空白字符,包括回車符。解決問(wèn)題:使用cin.get()函數(shù)接收回車。問(wèn)題:刪除成員時(shí),刪除能夠成功,但會(huì)出現(xiàn)空指針錯(cuò)誤。解決方案:在delete該成員時(shí),需要將指向該成員的指針置空。問(wèn)題:在主函數(shù)中,通過(guò)請(qǐng)用戶輸入數(shù)字,來(lái)選擇相應(yīng)的操作,當(dāng)用戶誤輸入的為選擇以外的字符時(shí),會(huì)結(jié)束程序運(yùn)行。解決方案:與解決方案4相同。實(shí)驗(yàn)結(jié)果及分析創(chuàng)建家譜保存到文件讀取文件增加成員基本信息查詢成員信息成員改名遍歷家譜查詢孩子信息刪除成員收獲:本次實(shí)訓(xùn)在我們?yōu)槠趦芍艿臅r(shí)間里進(jìn)行,通過(guò)自己的不斷學(xué)習(xí)、請(qǐng)教和老師的指導(dǎo),完成了關(guān)于家譜資料管理的設(shè)計(jì)。前期主要是準(zhǔn)備階段,運(yùn)用哪些技術(shù),中期實(shí)踐階段,通過(guò)幾天的上機(jī)編寫代碼,然后完成,后期完善階段,對(duì)一些難點(diǎn)和重點(diǎn)再細(xì)化,和做一些數(shù)據(jù)輸入時(shí)的異常處理。最后進(jìn)行答辯階段。通過(guò)這次實(shí)訓(xùn)的互相幫助學(xué)習(xí)的過(guò)程,自己看書學(xué)習(xí)的經(jīng)驗(yàn),以及從網(wǎng)上以及其他各種途徑獲得信息和知識(shí)的經(jīng)驗(yàn)。理論與實(shí)際相結(jié)合的設(shè)計(jì),鍛煉了我綜合運(yùn)用所學(xué)的基礎(chǔ)知識(shí),解決實(shí)際問(wèn)題的能力,同時(shí)也提高我查閱文獻(xiàn)資料、對(duì)程序整體的把握等其他能力水平。而且通過(guò)對(duì)整體的掌控,對(duì)局部的取舍,以及對(duì)細(xì)節(jié)的斟酌處理,都使我的能力得到了鍛煉,我的各方面經(jīng)驗(yàn)都得到了極大的豐富。附錄全部代碼Familytree.h#ifndefFAMILYTREE_H#defineFAMILYTREE_H#include<iostream>#include<string>#include<cctype>#include<io.h>#include<iomanip>#include<fstream>usingnamespacestd;structBirthDay{ intyear; intmonth; intday; friendistream&operator>>(istream&is,BirthDay&b); friendostream&operator<<(ostream&os,constBirthDay&b);};structInformation{ stringname; stringbirthPlace; BirthDaybirthDay; stringsex; stringeducation; stringjob; stringfather; stringspouse; charlife;};structNode{ Informationdata; Node*child; Node*brother;};classFamilyTree{private: Node*root; Node*Creat(); voidRelease(Node*root); staticintNumberofpeople; staticintLifePeopele;public: FamilyTree(); ~FamilyTree(); voidSetNode(Node*root); Node*Getroot(); voidPreOrder(Node*root); voidInOrder(Node*root); voidPostOrder(Node*root); intGeneration(Node*root); intNumberOfPeople(); intLifeNum(); voidPrintMessage(Node*root); intMessage(Node*root,stringName); Node*PreFindFather(Node*root,stringFatherName); Node*PreFindBrother(Node*root,stringFatherName); voidAddNewPeople(Node*root,stringFatherName,stringNAme); intDeletePeople(Node*root,stringFatherName,stringDeletepeople); intSetNewName(Node*root,stringNAme,stringNewName); intFindChild(Node*root,stringNAme); voidSaveToFile(Node*root); voidFileToFamilyTree(Node*root);};#endifFamilytree.cpp#include"Familytree.h"intFamilyTree::Numberofpeople=0;intFamilyTree::LifePeopele=0;//生日結(jié)構(gòu)體變量輸入輸出友元重載istream&operator>>(istream&is,BirthDay&b){ is>>b.year>>b.month>>b.day; returnis;}ostream&operator<<(ostream&os,constBirthDay&b){ os<<b.year<<"-"<<b.month<<"-"<<b.day; returnos;}FamilyTree::FamilyTree()//構(gòu)造函數(shù),初始化一棵樹,其前序序列由鍵盤輸入{ this->root=Creat();}FamilyTree::~FamilyTree()//析構(gòu)函數(shù),釋放鏈表中各結(jié)點(diǎn)的存儲(chǔ)空間{ Release(root);}Node*FamilyTree::Getroot()//獲取根結(jié)點(diǎn){ returnroot;}Node*FamilyTree::Creat()//構(gòu)造函數(shù)調(diào)用{ Node*root; stringch; cout<<"請(qǐng)問(wèn)是否創(chuàng)建(是:“y”,否:“#”):"; cin>>ch;//輸入名字 if(ch!="y")//異常處理 { if(ch!="Y") if(ch!="#") { intt=1; do { cout<<"\n輸入不明確,請(qǐng)重新輸入??!"<<endl; cout<<"請(qǐng)問(wèn)是否創(chuàng)建(是:“y”,否:“#”):"; cin>>ch; if((ch=="y")||(ch=="Y")||(ch=="#")) t=0; }while(t==1); } } if(ch=="#")root=NULL; else { root=newNode;//申請(qǐng)結(jié)點(diǎn)內(nèi)存空間 SetNode(root);//設(shè)置結(jié)點(diǎn)內(nèi)容 root->child=Creat();//該結(jié)點(diǎn)的孩子 root->brother=Creat();//該結(jié)點(diǎn)的兄弟 } returnroot;//返回結(jié)點(diǎn)}voidFamilyTree::Release(Node*root)//析構(gòu)函數(shù)調(diào)用{ if(root!=NULL) { Release(root->child);//釋放左孩子 Release(root->brother);//釋放右兄弟 deleteroot; }}voidFamilyTree::SetNode(Node*root)//設(shè)置結(jié)點(diǎn)信息{ Numberofpeople++; cout<<"請(qǐng)輸入家庭成員的基本信息"<<endl; cout<<"姓名:"; cin>>root->; cout<<"出生地:"; cin>>root->data.birthPlace; cout<<"生日(數(shù)字、年月日以空格或者回車間隔):"; while(1) { cin>>root->data.birthDay; if(cin.fail()) { cout<<"輸入有錯(cuò)??!請(qǐng)重新輸入生日(數(shù)字):"<<endl; cin.clear();//輸入錯(cuò)誤則能重新輸入 cin.sync();//清空流 } else break; } //isdigit異常處理生日輸入,若參數(shù)c為阿拉伯?dāng)?shù)字0~9,則返回非0值,否則返回NULL。 /*inti; for(i=0;root->data.birthDay[i]!=0;++i) { if(isdigit(root->data.birthDay[i])==0) { cout<<"\n輸入不明確,請(qǐng)重新輸入??!"<<endl; break; } }*/ cout<<"性別:"; cin>>root->data.sex; cout<<"學(xué)歷:"; cin>>root->cation; cout<<"工作:"; cin>>root->data.job; cout<<"父親:"; cin>>root->data.father; cout<<"配偶(有多任配偶則以“,”或者“、”間隔):"<<endl; cin>>root->data.spouse; cout<<"是否健在(y是,n否):"; cin>>root->data.life; if(toupper(root->data.life)!='Y')//異常處理 { if(toupper(root->data.life)!='N'){ intt=1; do { cout<<"\n輸入不明確,請(qǐng)重新輸入!!"<<endl; cout<<"是否健在(y是,n否):"; cin>>root->data.life; if((toupper(root->data.life)=='Y')||(toupper(root->data.life)=='N')) t=0; }while(t==1); } } if(toupper(root->data.life)=='Y') LifePeopele++;}voidFamilyTree::PreOrder(Node*root)//前序遞歸遍歷輸出家譜{ if(root==NULL) return; else { cout<<root-><<'\t'; PreOrder(root->child); PreOrder(root->brother); }}voidFamilyTree::InOrder(Node*root)//中序遞歸遍歷輸出家譜{ if(root==NULL) return; else { InOrder(root->child); cout<<root-><<'\t'; InOrder(root->brother); }}voidFamilyTree::PostOrder(Node*root)//后序遞歸遍歷輸出家譜{ if(root==NULL) return; else { PostOrder(root->child); PostOrder(root->brother); cout<<root-><<'\t'; }}intFamilyTree::Generation(Node*root)//這個(gè)家族共有幾代人{(lán) intl;//l左孩子 if(root==NULL)//這個(gè)家族為空,返回0 return0; else { l=Generation(root->child);//左孩子的 returnl+1; }}//intnumberofpeople=0;intFamilyTree::NumberOfPeople()//家族的總?cè)藬?shù){ if(root==NULL)//家族人數(shù)為0 return0; /*else { if(root!=NULL) numberofpeople++; NumberOfPeople(root->child); NumberOfPeople(root->brother); } returnnumberofpeople;*/ else returnNumberofpeople;}//intcount=0;intFamilyTree::LifeNum()//健在人數(shù){ if(root==NULL)//-1表示這個(gè)家族不存在 return-1; /*else { if(toupper(root->data.life)=='Y') count++; LifeNum(root->child); LifeNum(root->brother); } returncount;*/ returnLifePeopele;}voidFamilyTree::PrintMessage(Node*root)//輸出基本信息{ if(root==NULL) return; else { cout<<"姓名:"<<root->; cout<<"\t\t性別:"<<root->data.sex; cout<<"\t\t配偶:"<<root->data.spouse<<endl; cout<<"出生地:"<<root->data.birthPlace; cout<<"\t\t生日:"<<root->data.birthDay;//;<<root->data.birthDay.year<<"-"<<root->data.birthDay.month<<"-"<<root->data.birthDay.day<<endl; cout<<"\t\t父親:"<<root->data.father<<endl; cout<<"學(xué)歷:"<<root->cation; cout<<"\t\t工作:"<<root->data.job; cout<<"\t\t是否健在:"; if(toupper(root->data.life)=='Y') cout<<"是"<<endl; else cout<<"否"<<endl; }}intmessage=0;//判斷是否查找成功intFamilyTree::Message(Node*root,stringName)//顯示該成員的基本信息{ if(root==NULL) returnmessage; else { if(root->==Name) { message=1; PrintMessage(root); } else { Message(root->child,Name); Message(root->brother,Name); } } returnmessage;}Node*FamilyTree::PreFindFather(Node*root,stringFatherName)//給定元素值查找父親結(jié)點(diǎn)指針位置并返回其指針,此方法采用的先序遍歷{ if(root==NULL) throw"錯(cuò)誤"; Node*p; Node*tree[20]; inttop=0; while(root!=NULL||top!=0) { while(root!=NULL) { if(root->==FatherName) p=root; top++; tree[top]=root; root=root->child; } if(top!=0) { root=tree[top]->brother; top--; } } returnp;}Node*FamilyTree::PreFindBrother(Node*root,stringFatherName)//給定元素值查找兄弟結(jié)點(diǎn)指針位置并返回其指針,此方法采用的先序遍歷{ if(root==NULL) throw"錯(cuò)誤"; Node*p; Node*tree[20]; inttop=0; while(root!=NULL||top!=0) { while(root!=NULL) { if(root->data.father==FatherName) p=root; top++; tree[top]=root; root=root->child; } if(top!=0) { root=tree[top]->brother; top--; } } returnp;}voidFamilyTree::AddNewPeople(Node*root,stringFatherName,stringNAme)//增加新的家族成員{ if(root==NULL)//如果這個(gè)家族為空,直接把該結(jié)點(diǎn)置為根結(jié)點(diǎn) { Creat(); } else{ Node*p=newNode; p->=NAme; p->child=NULL; p->brother=NULL; Node*Brother=PreFindBrother(root,FatherName);//兄弟結(jié)點(diǎn) if(root->data.father==FatherName)//如果與祖先(根結(jié)點(diǎn))同一個(gè)父親,則置為根結(jié)點(diǎn)的右兄弟 { Node*q=root; while(q->brother!=NULL)//尋找根結(jié)點(diǎn)的右兄弟結(jié)點(diǎn)為空的結(jié)點(diǎn) { q=q->brother; } if(q->brother==NULL) { q->brother=p; SetNode(p); } } Node*Father=PreFindFather(root,FatherName);//父親結(jié)點(diǎn) //Node*Brother=PreFindBrother(root,FatherName);//兄弟結(jié)點(diǎn) if(Father->child==NULL)//如果父親結(jié)點(diǎn)的孩子結(jié)點(diǎn)為空 { Father->child=p; SetNode(p); } else//如果父親結(jié)點(diǎn)的孩子結(jié)點(diǎn)不為空 { if(Brother->brother==NULL)//最后一個(gè)兄弟結(jié)點(diǎn) { Brother->brother=p; SetNode(p); } } }}intFamilyTree::DeletePeople(Node*root,stringFatherName,stringDeletepeople)//刪除家族成員{ intt=0; Numberofpeople--; if(root==NULL) returnt; else { if(root->==Deletepeople)//如果要?jiǎng)h除的為祖先,則調(diào)用Release()函數(shù) { t=1; root->brother=NULL; root->child=NULL; Release(root); } else { Node*Father=PreFindFather(root,FatherName); Node*Brother=PreFindBrother(root,FatherName);//兄弟結(jié)點(diǎn) Node*p; Node*tree[20]; inttop=0; while(root!=NULL||top!=0) { while(root!=NULL) { if(root->==Deletepeople) { p=root; //break; } top++; tree[top]=root; root=root->child; } if(top!=0) { root=tree[top]->brother; top--; } } if(toupper(p->data.life)=='Y')//健在人數(shù)減一 LifePeopele--; if(Father->child->==p->)//Deletepeople) Father->child=NULL; else Brother->brother=NULL; //p->brother=NULL; //p->child=NULL; t=1; deletep; } } returnt;}intflag=0;//標(biāo)記intFamilyTree::SetNewName(Node*root,stringNAme,stringNewName)//更改姓名{ if(root==NULL) returnflag; else { if(root->==NAme) { flag=1; root->=NewName; } else { SetNewName(root->child,NAme,NewName); SetNewName(root->brother,NAme,NewName); } } returnflag;}intFamilyTree::FindChild(Node*root,stringNAme)//顯示孩子信息{ intflag=0; if(root==NULL) returnflag; else { if(root->==NAme) { if(root->child==NULL) returnflag; else { flag=1; cout<<root->child-><<""; Node*p; Node*tree[20]; p=root->child; tree[0]=p; inttop=0; while(p->brother!=NULL) { p=p->brother; top++; tree[top]=p; cout<<p-><<""; } cout<<"\n是否查看孩子的詳細(xì)信息(是:y,否:n):"; charch; cin>>ch; if(toupper(ch)=='Y') { Node*q=tree[top]; while(top>-1) { PrintMessage(q); cout<<"\n"<<endl; top--; q=tree[top]; } } } } } returnflag;}voidFamilyTree::SaveToFile(Node*root)//保存到文件{ if(root==NULL) { cout<<"家譜為空"<<endl; return; } ofstreamofile; cout<<"請(qǐng)輸入要保存文件的路徑:"<<endl; charc=cin.get();//接收回車 chardir[40]; cin.getline(dir,40); ofile.open(dir,ios::app); if(_access(dir,0)==-1)//系統(tǒng)自帶功能判斷路徑是否有效 { cout<<"輸入路徑不存在!"<<endl; return; } else { //d:\test.txt" cout<<"已經(jīng)保存全部信息。"<<endl; ofile<<"姓名"<<setw(10)<<"性別"<<setw(10)<<"配偶"<<setw(10)<<"出生地"<<setw(10)<<"生日"<<setw(10)<<"學(xué)歷"<<setw(7)<<"工作"<<setw(7)<<"父親"<<setw(7)<<"健在"<<endl; Node*tree[20]; inttop=0; while(root!=NULL||top!=0) { while(root!=NULL) { ofile<<root-><<setw(10)<<root->data.sex<<setw(10)<<root->data.spouse<<setw(10)<<root->data.birthPlace<<setw(10)<<root->data.birthDay<<setw(10)<<root->cation<<setw(9)<<root->data.job<<setw(9)<<root->data.father<<setw(9)<<root->data.life<<endl; top++; tree[top]; tree[top]=root; root=root->child; } if(top!=0) { root=tree[top]->brother; top--; } } ofile.close(); }}voidFamilyTree::FileToFamilyTree(Node*root)//從文件中讀取{ cout<<"請(qǐng)輸入要讀取文件的路徑:"<<endl; ifstreamifile; charc=cin.get();//讀入任意一個(gè)字符,包括回車符 chardir[40];//因?yàn)閏in的>>重載會(huì)跳過(guò)空白字符,包括回車字符,所以無(wú)法使用>>直接讀入回車符cin.get(),該成員函數(shù)功能為,從cin讀入一個(gè)字符,并返回 cin.getline(dir,40);//cin.get(),該成員函數(shù)功能為,從cin讀入一個(gè)字符,并返回 ifile.open(dir);// if(_access(dir,0)==-1) { cout<<"輸入路徑不存在!"<<endl; return; } stringch; getline(ifile,ch); cout<<ch<<endl; Node*tree[20]; inti=0; while(!ifile.eof())//eof文件末尾 {//姓名性別配偶出生地生日學(xué)歷工作父親健在 ifile>>root->>>root->data.sex>>root->data.spouse>>root->data.birthPlace>>root->data.birthDay>>root->cation>>root->data.job>>root->data.father>>root->data.life; tree[i]=root; i++; } i--; for(intj=0;j<i;j++) cout<<tree[j]-><<""<<tree[j]->data.sex<<""<<tree[j]->data.spouse<<""<<tree[j]->data.birthPlace<<""<<tree[j]->data.birthDay<<""<<tree[j]->cation<<""<<tree[j]->data.job<<""<<tree[j]->data.father<<""<<tree[j]->data.life<<endl;}Family.cpp#include<iostream>#include<process.h>#include"Familytree.h"#include<string>usingnamespacestd;voidMenu();intmain(){ Menu(); cout<<"\n創(chuàng)建家譜,輸入順序?yàn)榍靶蛐蛄校蠛⒆?,右兄?<<endl; FamilyTreeft; system("cls"); Menu(); Node*root=ft.Getroot(); while(1) { intselect; cout<<"\n請(qǐng)輸入選擇操作的序號(hào):"; //cin>>select; while(1) { cin>>select; if(cin.fail()) { cout<<"輸入有錯(cuò)??!請(qǐng)重新輸入選擇(數(shù)字):"; cin.clear();//輸入錯(cuò)誤則能重新輸入將錯(cuò)誤標(biāo)識(shí)符改為0 cin.sync();//清空數(shù)據(jù)流 } else break; } switch(select) { case1:system("cls"),Menu();break; case2: { cout<<"\n前序遍歷"<<endl; ft.PreOrder(root); cout<<"\n\n中序遍歷"<<endl; ft.InOrder(root); cout<<"\n\n后序遍歷"<<endl; ft.PostOrder(root); cout<<endl; }break; case3: { cout<<"\n\n這個(gè)家族共有"<<ft.Generation(root)<<"代人"<<endl; cout<<"\n\n這個(gè)家族共有"<<ft.NumberOfPeople()<<"人"<<endl; cout<<"\n\n這個(gè)家族健在人數(shù)為:"<<ft.LifeNum()<<endl; }break; case4: { cout<<"\n\n請(qǐng)輸入要查詢的人的姓名:"; stringName; cin>>Name; cout<<"查找結(jié)果:"<<endl; intmessage=ft.Message(root,Name); if(message==0) cout<<"查找失敗??!"<<endl; }break; case5: { cout<<"\n\n請(qǐng)輸入要增加人員的父親姓名:"; stringFatherName; cin>>FatherName; cout<<"請(qǐng)輸入要增加人員的姓名:"; stringNAme; cin>>NAme; ft.AddNewPeople(root,FatherName,NAme); }break; case6: { cout<<"\n\n請(qǐng)輸入要?jiǎng)h除人員的姓名:"; stringDeletepeople; cin>>Deletepeople; cout<<"請(qǐng)輸入該成員的父親:"; stringFname; cin>>Fname; intDelName=ft.DeletePeople

溫馨提示

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

評(píng)論

0/150

提交評(píng)論