


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 项目实训二 项目名称 _ 家谱管理系统_ 姓名_ _ 班级 _ _学号_ _指导教师 _ _ _ 2018.1问题描述:家谱用于记录某家族历代家族成员的情况与关系。现编制一个家谱资料管理系统,实现对一个家族所有的资料进行收集整理。实现对家庭成员信息的建立、查找、插入、修改、增加、删除、更新、统计等等功能。目的和要求:目的:1、 能根据具体问题的具体情况,结合数据结构课程中的基本理论和基本算法,分析并正确确定数据的逻辑结构,合理地选择相应的存储结构,并能设计出解决问题的有效算法。2、 提高程序设计和调试能力。通过上机学习,验证自己设计的算法的正确性。学会有效利用基本调试方法。3、 初步掌握软件
2、开发过程中问题分析、系统设计、程序编码、测试等基本方法和技能。4、 培养根据选题需要选择学习书籍,查阅文献资料的自学能力。要求: 用于记录某家族历代家族成员的情况与关系。现编制一个家谱资料管理系统,实现对一个家族所有的资料进行收集整理。支持对家谱的增加,删除,更新,统计等。软件环境:Microsoft Visual Studio 2010流程设计:开始Main函数Menu函数FamilyTree函数Getroot函数Case 1Case 2Case 3Case 4Case 5Case 6Case 7Case 8Case 9Case10defeault结束Creat函数函数递归调用Menu函数P
3、reOrder函数函数InOrder函数函数PostOrder函数函数Generation函数函数NumberOfPeople函数函数LifeNum函数函数Message函数函数AddNewPeople函数数DeletePeople函数SetNewName函数FindChild函数SaveToFile函数FileToFamilyTree函数递归调用递归调用递归调用PrintMessage函数函数SetNode函数函数函数PreFindFather函数函数PreFindBrother函数函数PreFindFather函数函数PreFindBrother函数函数PrintMessage函数函数模块
4、划分:1、 统计模块(1) 统计家族总人数、健在人数、几代人(2) 主要函数:int Generation(Node *root); /这个家族共有几代人int NumberOfPeople( ); /家族的总人数int LifeNum( ); /健在人数(3) 实现方法:静态成员变量(4) 实现结果:2、 更新模块(1) 创建家谱、增加成员、删除成员、成员改名(2) 主要函数:Node* Creat( ); /构造函数调用void AddNewPeople(Node *root,string FatherName,string NAme); /增加新的家族成员int DeletePeople
5、(Node *root,string FatherName,string Deletepeople); /删除家族成员int SetNewName(Node *root,string NAme,string NewName); /更改姓名(3) 实现方法:创建家谱和成员改名主要通过递归调用;增加成员和删除成员主要通过栈的非递归调用。(4) 实现结果:3、 查询模块(1) 查询成员详细信息、查询成员的孩子以及孩子的详细信息(2) 主要函数:int Message(Node *root,string Name); /显示该成员的基本信息int FindChild(Node *root,string
6、 NAme); /显示孩子信息(3) 实现方法:通过递归调用,找到成员,输出相应的信息(4) 实现结果:4、 显示模块(1) 前序、中序、后序遍历家谱(2) 主要函数:void PreOrder(Node *root); /前序递归遍历输出家谱void InOrder(Node *root); /中序递归遍历输出家谱void PostOrder(Node *root); /后序递归遍历输出家谱(3) 实现方法:递归遍历(4) 实现结果:5、 文件模块(1) 保存到文件、从文件读取(2) 主要函数:void SaveToFile(Node *root); /保存到文件void FileToFam
7、ilyTree( Node *root) ; /从文件中读取(3) 实现方法:文件流(4) 实现结果:数据结构实现:1、 生日结构体struct BirthDay int year;int month;int day;friend istream& operator(istream &is,BirthDay &b);friend ostream& operator重载会跳过空白字符,包括回车符。解决问题:使用cin.get()函数接收回车。9、 问题:删除成员时,删除能够成功,但会出现空指针错误。解决方案:在delete该成员时,需要将指向该成员的指针置空。10、 问题:在主函数中,通过请用户
8、输入数字,来选择相应的操作,当用户误输入的为选择以外的字符时,会结束程序运行。解决方案:与解决方案4相同。实验结果及分析1、 创建家谱2、 保存到文件3、 读取文件4、 增加成员5、 基本信息6、 查询成员信息7、 成员改名8、 遍历家谱9、 查询孩子信息10、 删除成员收获: 本次实训在我们为期两周的时间里进行,通过自己的不断学习、请教和老师的指导,完成了关于家谱资料管理的设计。前期主要是准备阶段,运用哪些技术,中期实践阶段,通过几天的上机编写代码,然后完成,后期完善阶段,对一些难点和重点再细化,和做一些数据输入时的异常处理。最后进行答辩阶段。通过这次实训的互相帮助学习的过程,自己看书学习的
9、经验,以及从网上以及其他各种途径获得信息和知识的经验。理论与实际相结合的设计,锻炼了我综合运用所学的基础知识,解决实际问题的能力,同时也提高我查阅文献资料、对程序整体的把握等其他能力水平。而且通过对整体的掌控,对局部的取舍,以及对细节的斟酌处理,都使我的能力得到了锻炼,我的各方面经验都得到了极大的丰富。附录 全部代码Familytree.h#ifndef FAMILYTREE_H#define FAMILYTREE_H#include #include #include #include #include #include using namespace std;struct BirthDay
10、 int year;int month;int day;friend istream& operator(istream &is,BirthDay &b);friend ostream& operator(istream &is,BirthDay &b)isb.yearb.monthb.day;return is;ostream& operator(ostream &os,const BirthDay &b)osb.year-b.month-root=Creat();FamilyTree:FamilyTree() /析构函数,释放链表中各结点的存储空间Release(root);Node* F
11、amilyTree:Getroot() /获取根结点return root;Node* FamilyTree:Creat()/构造函数调用Node *root;string ch;coutch; /输入名字 if(ch!=y)/异常处理if(ch!=Y)if(ch!=#)int t=1;docoutn输入不明确,请重新输入!endl;coutch;if(ch=y)|(ch=Y)|(ch=#)t=0;while(t=1);if (ch=#) root = NULL;elseroot=new Node; /申请结点内存空间SetNode(root); /设置结点内容root-child=Creat
12、( ); /该结点的孩子root-brother=Creat( ); /该结点的兄弟return root; /返回结点void FamilyTree:Release(Node *root) /析构函数调用if(root!=NULL)Release(root-child); /释放左孩子Release(root-brother); /释放右兄弟delete root;void FamilyTree:SetNode(Node *root) /设置结点信息Numberofpeople+;cout请输入家庭成员的基本信息endl;;coutroot-data.bi
13、rthPlace;coutroot-data.birthDay;if(cin.fail()cout输入有错!请重新输入生日(数字):data.birthDayi!=0;+i)if(isdigit(root-data.birthDayi)=0)coutn输入不明确,请重新输入!endl;break;*/coutroot-data.sex;cation;coutroot-data.job;coutroot-data.father;cout配偶(有多任配偶则以“,”或者“、”间隔):root-data.spouse;coutroot-data.life;if(tou
14、pper(root-data.life)!=Y) /异常处理if(toupper(root-data.life)!=N)int t=1;docoutn输入不明确,请重新输入!endl;coutroot-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+;void FamilyTree:PreOrder(Node *root) /前序递归遍历输出家谱if(root=NULL)return;elsecou
15、child);PreOrder(root-brother);void FamilyTree:InOrder(Node *root) /中序递归遍历输出家谱if(root=NULL) return;elseInOrder(root-child);brother);void FamilyTree:PostOrder(Node *root) /后序递归遍历输出家谱if(root=NULL)return;elsePostOrder(root-child);PostOrder(root-brother);child); /左孩子的r
16、eturn l+1;/int numberofpeople=0;int FamilyTree:NumberOfPeople( ) /家族的总人数if(root=NULL) /家族人数为0return 0;/*elseif(root!=NULL)numberofpeople+;NumberOfPeople(root-child);NumberOfPeople(root-brother);return numberofpeople;*/elsereturn Numberofpeople;/int count=0;int FamilyTree:LifeNum( ) /健在人数if(root=NULL
17、) /-1表示这个家族不存在return -1; /*elseif(toupper(root-data.life)=Y)count+;LifeNum(root-child);LifeNum(root-brother);return count;*/return LifePeopele;void FamilyTree:PrintMessage(Node *root ) /输出基本信息if(root=NULL)return ;elsecout姓名:;couttt性别:data.sex;couttt配偶:data.spouseendl;cout出生地:data.birthPlace
18、;couttt生日:data.birthDay;/;data.birthDay.year-data.birthDay.month-data.birthDay.dayendl;couttt父亲:data.fatherendl;cout学历:cation;couttt工作:data.job;coutdata.life)=Y)cout是endl;elsecout否=Name)message=1;PrintMessage(root );elseMessage(root-child,Name);Message(root-brother,Name);return mess
19、age;Node* FamilyTree:PreFindFather(Node *root,string FatherName) /给定元素值查找父亲结点指针位置并返回其指针,此方法采用的先序遍历 if(root=NULL)throw错误;Node *p;Node *tree20;int top=0;while(root!=NULL|top!=0)while(root!=NULL)if(=FatherName)p=root;top+;treetop=root;root=root-child;if(top!=0)root=treetop-brother;top-;re
20、turn p;Node* FamilyTree:PreFindBrother(Node *root,string FatherName) /给定元素值查找兄弟结点指针位置并返回其指针,此方法采用的先序遍历 if(root=NULL)throw错误;Node *p;Node *tree20;int top=0;while(root!=NULL|top!=0)while(root!=NULL)if(root-data.father=FatherName)p=root;top+;treetop=root;root=root-child;if(top!=0)root=treetop-brother;t
21、op-;return p;void FamilyTree:AddNewPeople(Node *root,string FatherName,string NAme) /增加新的家族成员if(root=NULL)/如果这个家族为空,直接把该结点置为根结点Creat( ) ;elseNode *p=new Node;=NAme;p-child=NULL;p-brother=NULL;Node *Brother=PreFindBrother(root,FatherName); /兄弟结点if(root-data.father=FatherName) /如果与祖先(根结点)同一
22、个父亲,则置为根结点的右兄弟Node *q=root;while(q-brother!=NULL) /寻找根结点的右兄弟结点为空的结点q=q-brother;if(q-brother=NULL) q-brother=p;SetNode(p);Node *Father=PreFindFather(root,FatherName); /父亲结点/Node *Brother=PreFindBrother(root,FatherName); /兄弟结点if(Father-child=NULL) /如果父亲结点的孩子结点为空Father-child=p;SetNode(p);else /如果父亲结点的孩
23、子结点不为空if(Brother-brother=NULL) /最后一个兄弟结点Brother-brother=p;SetNode(p);int FamilyTree:DeletePeople(Node *root,string FatherName,string Deletepeople) /删除家族成员int t=0;Numberofpeople-;if(root=NULL)return t;elseif(=Deletepeople) /如果要删除的为祖先,则调用Release()函数t=1;root-brother=NULL;root-child=NULL;R
24、elease(root);elseNode *Father=PreFindFather(root,FatherName);Node *Brother=PreFindBrother(root,FatherName); /兄弟结点Node *p;Node *tree20;int top=0;while(root!=NULL|top!=0)while(root!=NULL)if(=Deletepeople)p=root;/break;top+;treetop=root;root=root-child;if(top!=0)root=treetop-brother;top-;
25、if(toupper(p-data.life)=Y) /健在人数减一LifePeopele-;if(F=)/Deletepeople)Father-child=NULL;elseBrother-brother=NULL;/p-brother=NULL;/p-child=NULL;t=1;delete p;return t;int flag=0; /标记int FamilyTree:SetNewName(Node *root,string NAme,string NewName) /更改姓名if(root=NULL)return f
26、lag;elseif(=NAme)flag=1;=NewName;elseSetNewName(root-child,NAme,NewName);SetNewName(root-brother,NAme,NewName);return flag;int FamilyTree:FindChild(Node *root,string NAme) /显示孩子信息int flag=0;if(root=NULL)return flag ;elseif(=NAme)if(root-child=NULL)return fla
27、g;elseflag=1;child;tree0=p;int top=0;while(p-brother!=NULL)p=p-brother;top+;treetop=p; ;coutch;if(toupper(ch)=Y)Node *q=treetop;while(top-1)PrintMessage(q);coutn-endl;top-;q=treetop;return flag;void FamilyTree:SaveToFile(Node *root) /保存到文件if(root=NULL)cout家谱为空endl;ret
28、urn ;ofstream ofile;cout 请输入要保存文件的路径: endl;char c = cin.get(); /接收回车char dir40;cin.getline(dir, 40); ofile.open(dir,ios:app); if (_access(dir, 0) = -1) /系统自带功能判断路径是否有效cout 输入路径不存在! endl;return;else/d:test.txtcout 已经保存全部信息。 endl;ofile 姓名 setw(10) 性别 setw(10) 配偶 setw(10) 出生地 setw(10) 生日 setw(10) 学历 se
29、tw(7) 工作 setw(7) 父亲 setw(7) 健在 endl;Node *tree20;int top=0;while(root!=NULL|top!=0)while(root!=NULL)setw(10) data.sexsetw(10) data.spouse setw(10)data.birthPlacesetw(10) data.birthDaysetw(10) cationsetw(9) etw(9) data.fathersetw(9) data.lifechild;if(top!=0)root=treetop
30、-brother;top-;ofile.close();void FamilyTree:FileToFamilyTree(Node *root ) /从文件中读取cout 请输入要读取文件的路径: 重载会跳过空白字符,包括回车字符,所以无法使用直接读入回车符cin.get(),该成员函数功能为,从cin读入一个字符,并返回cin.getline(dir, 40); / cin.get(),该成员函数功能为,从cin读入一个字符,并返回ifile.open(dir); /if (_access(dir, 0) = -1)cout 输入路径不存在! endl;return;string ch;getline(ifile, ch);cout ch root-data.sexroot-data.spouseroot-data.birthPlaceroot-data.birthDcationroot-data.jobroot-data.fatherroot-data.life;treei=root;i+;i-;fo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- ppp水利项目合同范例
- 人保车损险合同范例
- 内部转让门店合同范例
- 代理区域转让合同范例
- 中英投资合同范例
- 2025年中国增强菱镁光缆盖板市场调查研究报告
- 2025年中国压力真空校验仪市场调查研究报告
- 2025年中国前连接器市场调查研究报告
- 农村玉米销售合同范例
- 共用店铺合作合同范本
- GB/T 19413-2024数据中心和通信机房用空气调节机组
- 汽车租赁项目投标书
- 《税收负担对我国制造业资本金融化的影响研究》
- 2024年四川省公务员录用考试《行测》真题及答案解析
- 卷扬机使用施工方案
- 现代家政导论-课件 5.1.3家庭管家
- 市政管网工程施工方案
- 医院感染暴发及处理课件
- 中国古典园林史复习
- 32个管理学经典理论
- 高压线下作业安全技术交底
评论
0/150
提交评论