数据结构大作业之家谱管理系统.docx
- 文档编号:2749816
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:32
- 大小:70.34KB
数据结构大作业之家谱管理系统.docx
《数据结构大作业之家谱管理系统.docx》由会员分享,可在线阅读,更多相关《数据结构大作业之家谱管理系统.docx(32页珍藏版)》请在冰点文库上搜索。
数据结构大作业之家谱管理系统
C++实现的数据结构大作业之家谱管理系统
欢迎界面:
主菜单:
其他一些运行结果不在一一列举,一面篇幅过长。
这个家谱管理系统,支持键盘操作,所有的操作都可以用键盘完成,如选择功能、退出等。
代码的实现使用的是c++11标准,与旧标准或有不同。
类的头文件:
#include
#include
usingstd:
:
string;
usingstd:
:
ostream;
usingstd:
:
istream;
structBirthDay{//生日结构变量
intyear=0;intmonth=0;intday=0;
};
classMember{
friendclassFamilySystem;
friendostream&operator<<(ostream&out,constMember&m);//输出成员信息到控制台
friendistream&operator>>(istream&in,Member&m);//从文件中读入成员信息
public:
Member()=default;//使用默认合成构造函数
voidsetE();//通过控制台输入给成员赋值
~Member()=default;//使用默认合成析构函数
Member*pson=nullptr;//指向儿子的指针
Member*pbro=nullptr;//指向兄弟的指针
private:
//成员的各种数据
stringname="未知";
stringbirthPlace="未知";
BirthDaybirthDay;
stringsex="未知";
floatheight=0;
intage=0;
stringeducation="未知";
stringjob="未知";
stringfather="未知";
};
classFamilySystem{
public:
FamilySystem()=default;//使用默认合成构造函数
~FamilySystem(){
deleteroot;
}
boolfileInit();//从文件中读取数据
intmenu();//显示菜单(功能)
booladdMem();//增加成员
booldeleteMem();//删除成员
booldemandMem();//查询
booldemandAve();//查询家庭整体情况
boolmodifyMem();//修改成员
boolshowFamily();//展示整个家谱
boolsaveFile();//保存信息到文件中,将使用两个文件,一个保存成员信息,一个保存家谱整体信息
Member*root=nullptr;//家谱数结构的根节点指针?
private:
inttotal=0;//总人数
floataveAge=0;//平均年龄
floataveHeight=0;//平均身高
floataveMember=0;//家庭平均人数
floatratio=0;//男女比例
voidcalculate();//计算平均值等
Member*seek(string);//查找是否存在成员
voiddisplay(Member*,int);//先序遍历树,用于显示所有成员
};
externstringgets();//用于接收键盘的输入
类的函数及相关操作函数的实现
#include
#include
#include
#include
#include
#include"SystemClass.h"
usingstd:
:
cin;
usingstd:
:
cout;
usingstd:
:
endl;
usingstd:
:
setw;
usingstd:
:
vector;
usingstd:
:
ios;
usingstd:
:
ifstream;
usingstd:
:
ofstream;
usingstd:
:
istringstream;
stringgets(){//从控制台获取字符等信息
strings;
while(intn=_getch()){
if(n==27){
throw27;
break;
}
elseif(n==13){
cout< break; } elseif(n==8){ s.pop_back(); cout<<"\b\b"; } else{ cout< s.push_back(char(n)); } } returns; } //与BirthDay相关的函数 istream&operator>>(istream&in,BirthDay&b){//从文件中读取生日的信息 in>>b.year>>b.month>>b.day; returnin; } ostream&operator<<(ostream&out,BirthDay&b){ out< returnout; } //与Member相关的函数 voidMember: : setE(){ try{ istringstreamsin; cout< \n" < ";sin.str(gets());sin>>name; cout< ";sin.str(gets());sin.clear();sin>>birthPlace; cout< ";sin.str(gets()); while(true){ sin.clear(); sin>>birthDay.year>>birthDay.month>>birthDay.day; if(! sin||birthDay.year>2014||birthDay.month>12||birthDay.day>31){ cout<<"输入错误,请重新输入: "; sin.str(gets()); } elsebreak; } cout< ";sin.str(gets());sin.clear(); while(sin>>sex){ if(sex=="male"||sex=="female"||sex=="男"||sex=="女") break; else{ cout<<"性别必须是male、female、男或者女,请重新输入: "; sin.str(gets());sin.clear(); } } cout< ";sin.str(gets());sin.clear(); while(! (sin>>height)){ cout<<"输入错误,请重新输入: "; sin.str(gets());sin.clear(); } cout< ";sin.str(gets());sin.clear(); while(! (sin>>age)||age>300){ cout<<"输入错误,请重新输入: "< sin.str(gets());sin.clear(); } cout< ";sin.str(gets());sin.clear();sin>>education; cout< ";sin.str(gets());sin.clear();sin>>job; cout< ";sin.str(gets());sin.clear();sin>>father; } catch(intex){ throwex; } return; } ostream&operator<<(ostream&out,constMember&m){ out< "< < "< < "< < "< <<""< < "< < "< < "< < "< < "< < "< returnout; } istream&operator>>(istream&in,Member&m){ in>>m.name>>m.birthPlace>>m.birthDay>>m.sex >>m.height>>m.age>>m.education>>m.job>>m.father; returnin; } //FamilySystem的成员函数 intFamilySystem: : menu(){ cout<<"\n\n"; cout<<"---------------------------\n"; cout<<"||\n"; cout<<"|系统菜单|\n"; cout<<"||\n"; cout<<"|1.查询成员|\n"; cout<<"||\n"; cout<<"|2.查询家庭整体情况|\n"; cout<<"||\n"; cout<<"|3.添加成员|\n"; cout<<"||\n"; cout<<"|4.删除成员|\n"; cout<<"||\n"; cout<<"|5.修改成员信息|\n"; cout<<"||\n"; cout<<"|6.显示整个家谱|\n"; cout<<"||\n"; cout<<"|按ESC退出系统|\n"; cout<<"||\n"; cout<<"---------------------------\n"; cout<<""; intn=0; while (1){ n=_getch(); if(n==27)break; if(n>=49&&n<=54)break; cout<<"\n请按下1、2、3、4、5、6选择或按ESC键退出! "< } returnn; } boolFamilySystem: : fileInit(){ ifstreaminfile; //打开存储成员信息的文件,读入成员信息 infile.open("Members.txt",ios: : in|ios: : _Nocreate); if(! infile){ returnfalse; } vector vector Member*temp=newMember;//临时保存输入的成员信息 root=temp;//第一个赋给root genely.push_back(root); next.push_back(root); while(infile>>*temp){ //将成员放入家族树中,成员的父亲必须是这一代的成员 for(autoi=genely.begin();i! =genely.end();++i){ if((*i)->name==temp->father){ if((*i)->pson==nullptr) (*i)->pson=temp; else{ Member*p=(*i)->pson; while(p->pbro! =nullptr) p=p->pbro; p->pbro=temp; } next.push_back(temp); temp=nullptr; break; } } //若成员的父亲不是这一代的成员,则将next赋予genely,迭进下一代 if(temp! =nullptr){ genely=next;//迭进下一代 next.clear();//清空next for(autoi: genely){//将成员放入家族树中 if(i->name==temp->father){ i->pson=temp; next.push_back(temp); temp=nullptr; break; } } } //动态创建临时成员变量,用于存储输入的下一个成员的信息 temp=newMember; } infile.close();//关闭存储成员信息的文件 //若文件中没有成员信息,将root置为空 if(root->name=="未知") root=nullptr; //打开存储家庭总体信息的文件,读入信息 infile.open("Wholes.txt",ios: : in|ios: : _Nocreate); if(! infile){ returnfalse; } infile>>total>>aveAge>>aveHeight>>aveMember>>ratio; infile.close();//关闭文件 returntrue; } Member*FamilySystem: : seek(stringnam){ //若家谱树为空,返回空指针 if(root==nullptr) returnnullptr; //若root不为空,继续查找 Member*store=nullptr;//存储返回的指针 vector genely.push_back(root); vector for(;;){//循环查找 //在这一代中查找,若找到,跳到最后 for(autop: genely){ if(p->name==nam){ store=p; gotoEnd; } } //判断下一代是否为空,若为空,跳到最后 intjubge=0; for(autop: genely){ if(p->pson! =nullptr) ++jubge; } if(jubge==0) gotoEnd; //找到下一代 for(autop: genely){ Member*temp=p->pson; if(temp==nullptr)continue; else{ next.push_back(temp); while(temp->pbro! =nullptr){ next.push_back(temp->pbro); temp=temp->pbro; } } } genely=next; next.clear(); } End: returnstore; } boolFamilySystem: : addMem(){ cout<<"3.添加成员.\n"; Member*temp=newMember; try{ temp->setE();//输入成员信息 } catch(int){ returnfalse; } //检查其父亲是否存在 Member*jubge=seek(temp->father); if(root==nullptr)root=temp; elseif(jubge==nullptr){ cout<<"\n家谱中没有此人父亲的信息! 无法插入! "; cout<<"\n请确认此人是否此家族中的人! "; } else{ if(seek(temp->name)! =nullptr){ cout<<"\n\n此人已存在于家谱中! "< } else{ cout<<"按ENTER确定添加并返回菜单\n按ESC取消添加并返回菜单"; for(;;){ intj=_getch(); if(j==27){ deletetemp; break; } if(j==13){ if(jubge->pson==nullptr)jubge->pson=temp; else{ Member*p=jubge->pson; while(p->pbro! =nullptr) p=p->pbro; p->pbro=temp; } cout<<"\n保存成功! \n"; break; } } } } cout<<"\n按ESC键返回菜单"; for(;;){ if(_getch()==27) break; } returntrue; } boolFamilySystem: : deleteMem(){ cout<<"4.删除成员.\n\n"; cout<<"请输入姓名: "; stringna; try{ istringstreamsin(gets()); sin>>na; } catch(int){ gotoend; } Member*temp=seek(na); if(temp==nullptr) cout<<"\n\n家谱中没有此人! \n\n"; else{ if(temp->pson! =nullptr){ cout<<"\n\n此人有后代! 若删除此人其后代将同时被删! "; cout<<"\n\n按ENTER继续\n按ESC退出"; for(;;){ intj=_getch(); if(j==27) returnfalse; if(j==13) break; } } Member*p=seek(temp->father); if(p==nullptr)root=nullptr; elseif(temp==p->pson)p->pson=nullptr; else{ while(p->pbro! =temp){ p=p->pbro; } p->pbro=nullptr; } cout<<"\n\n删除成功! \n\n"; } cout<<"\n按ESC键返回菜单"; for(;;){ if(_getch()==27) break; } end: returntrue; } boolFamilySystem: : demandMem(){ cout<<"1.查询成员.\n\n"; cout<<"请输入姓名: "; stringna; try{ istringstreamsin(gets()); sin>>na; } catch(int){ gotoend; } Member*m=seek(na); if(m! =nullptr)cout<<*m; elsecout<<"\n\n对不起,家谱中没有此人的信息! \n\n"; cout<<"\n按ESC键返回菜单"; for(;;){ if(_getch()==27) break; } end: returntrue; } voidFamilySystem: : calcula
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 作业 家谱 管理 系统
![提示](https://static.bingdoc.com/images/bang_tan.gif)