📄 bigwork.cpp
字号:
tname=NULL;
FindByOccupation(T,tname,keyword);
if(!tname)cout<<"NO such a person"<<endl;
break;
case 9:
//选9,执行搜索最高职位函数
cout<<"please input the top headship you want to inquire:";
cin>>keyword;
tname=NULL;
FindByTopHeadship(T,tname,keyword);
if(!tname)cout<<"NO such a person"<<endl;
break;
case 0:
//选0,退出
system("cls");
return;
default:cout<<"input error,please reinput"<<endl;//容错检测,如果不符合选择的要求,要重选
}
}
}
void GEnealogy::Display(person info)
{
//显示info的各项基本信息
cout<<"Name:"<<info->data.name<<endl
<<"BirthPlace:"<<info->data.birthplace<<endl
<<"BirthDate:"<<info->data.birthdate.year<<"."<<info->data.birthdate.month<<"."<<info->data.birthdate.day<<endl<<
"DeathDate:"<<info->data.deathdate.year<<"."<<info->data.deathdate.month<<"."<<info->data.deathdate.day<<endl<<
"Sex:"<<info->data.sex<<endl<<
"Height:"<<info->data.height<<endl<<
"Occupation:"<<info->data.occupation<<endl<<
"Education:"<<info->data.education<<endl<<
"Topheadship:"<<info->data.top_headship<<endl;
}
int GEnealogy::IsDateValid(Date date)
{
//检查日期是否有效
switch(date.month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(date.day<1||date.day>31)
return 0;
else
return 1;
break;
case 4:
case 6:
case 9:
case 11:
if(date.day<1||date.day>30)
return 0;
else
return 1;
break;
case 2:
if(IsLeapYear(date.year)){
if(date.day<1||date.day>29)
return 0;
else
return 1;
}
else{
if(date.day<1||date.day>28)
return 0;
else
return 1;
}
break;
default:
return 0;
}
}
int GEnealogy::CompareDate(Date date1, Date date2)
{
//比较两日期大小
//若date1比date2早,返回-1;date1比date2晚,返回1;date1与date2相等,返回0
if(date1.year>date2.year)
return 1;
else if(date1.year<date2.year)
return -1;
else if(date1.month>date2.month)
return 1;
else if(date1.month<date2.month)
return -1;
else if(date1.day>date2.day)
return 1;
else if(date1.day<date2.day)
return -1;
else
return 0;
}
bool GEnealogy::IsLeapYear(int year)
{
//判断year是否为闰年
if(year%4!=0)
return false;
else if(year%400==0)
return true;
else if(year%100==0)
return false;
else
return true;
}
void GEnealogy::InsertSibling(person& Firstchild,person insertSibling)
{
//将insertsibling结点插入到以firstchild为第一个孩子的结点的兄弟结点中去并按年龄排序
person pCur,pPre;
pCur=Firstchild; //pCur为第一个孩子
//如果pCur不是第一个孩子,则pPre为pCur的前一个兄弟,否则pPre为pCur的父母
pPre=Firstchild->parent;
insertSibling->data.Depth=Firstchild->data.Depth;//使兄弟之间的depth值相等
for(;pCur!=NULL;pCur=pCur->nextsibling)
{
if(CompareDate(pCur->data.birthdate,insertSibling->data.birthdate)>=0)//兄弟以年龄排序,年龄大在前
break;
pPre=pCur;
}
if(pCur==Firstchild)//如果insertsibling比第一个孩子年龄大,则insertsibling为第一个孩子
{
insertSibling->nextsibling=Firstchild;
pPre->firstchild=insertSibling;
}
else
{
insertSibling->nextsibling=pCur; //否则插入到相应位置
pPre->nextsibling=insertSibling;
}
}
void GEnealogy::Delete(person &rootNode)
{
//删除rootnode结点以及他的所有孩子结点
if(rootNode->parent) //rootnode不是根结点
if(rootNode->parent->firstchild==rootNode)//如果rootnode为其父结点的第一个孩子
rootNode->parent->firstchild=rootNode->nextsibling; //将rootnode的nextsibling结点变为rootnodeparent结点的firstchild结点
else
{
person p=rootNode->parent->firstchild; //否则,找到rootnode在兄弟中的位置
for(;p->nextsibling!=rootNode;p=p->nextsibling)
;
p->nextsibling=rootNode->nextsibling; //插入到兄弟中
}
PostOrderTraverse(rootNode->firstchild,DestroyNode);//调用后序遍历删除rootnode的所有孩子结点
if(rootNode==T) //删除rootnode结点
DestroyNode(T);
else
DestroyNode(rootNode);
}
void GEnealogy::Modify(person &curnode,person newnode)
{
//修改某个人的信息
strcpy(curnode->data.name,newnode->data.name);
strcpy(curnode->data.birthplace,newnode->data.birthplace);
strcpy(curnode->data.sex,newnode->data.sex);
strcpy(curnode->data.occupation,newnode->data.occupation);
strcpy(curnode->data.education,newnode->data.education);
strcpy(curnode->data.top_headship,newnode->data.top_headship);
curnode->data.height=newnode->data.height;
curnode->data.birthdate=newnode->data.birthdate;
curnode->data.deathdate=newnode->data.deathdate;
}
void GEnealogy::DisplayTree(person &T)
{
//按照depth值在屏幕上输出整个家谱,其中只显示姓名
if(T)
{
for(int i=0;i<3*T->data.Depth;i++)
cout<<ends;
cout<<T->data.name<<endl;
DisplayTree(T->firstchild);//对T的孩子递归搜索
DisplayTree(T->nextsibling);//对T的兄弟递归搜索
}
}
void GEnealogy::Add(person parent, person addNode)
{
//将addnode添加到parent作为parent的孩子结点
int n=0;
addNode->firstchild=addNode->nextsibling=NULL; //初始时firstchild同nextsibling都为空
addNode->parent=parent;
if(parent==NULL) //如果父结点空
{
if(T==NULL) //如果根结点空
{
addNode->data.Depth=n;
T=addNode; //将addnode赋给根结点
return;
}
n=T->data.Depth+1;//否则将原来的根结点成为新根结点的孩子
T->data.Depth=n; //并使原来根结点的depth值加1
addNode->firstchild=T;
T->parent=addNode;
T=addNode;
return;
}
strcpy(addNode->data.parentname,parent->data.name);
n=parent->data.Depth+1;
addNode->data.Depth=n;//将depth值加1
if(parent->firstchild==NULL) //如果parent无孩子,把addNode加入其firstchild
parent->firstchild=addNode;
else
InsertSibling(parent->firstchild,addNode); //否则插入到相应的兄弟结点中去
}
void GEnealogy::FindByRelationship(person pnode)
{
//按照亲属关系查找某人的父母,孩子,兄弟,若查找成功则显示出来
person p1,p2=pnode->firstchild;
if(pnode->parent)
{
//如果pnode的parent指针非空则它的parent存在,显示出来
cout<<pnode->data.name<<" parent is"<<pnode->parent->data.name<<endl;
for(p1=pnode->parent->firstchild;p1!=NULL;p1=p1->nextsibling)//将pnode的兄第显示出来
{
if(p1!=pnode)
cout<<pnode->data.name<<" sibling is "<<p1->data.name<<endl;
else
;
}
for(;p2!=NULL;p2=p2->nextsibling)//将pnode的孩子显示出来
cout<<pnode->data.name<<" child is "<<p2->data.name<<endl;
}
else//否则说明pnode为根结点
cout<<pnode->data.name<<" is the ancestor of the Genealogy"<<endl;
}
void GEnealogy::InputData(person &pnode)
{
//输入pnode的个人信息
cout<<"Please input the name of the person:";
cin>>pnode->data.name;
cout<<"Please input the birthplace of the person:";
cin>>pnode->data.birthplace;
cout<<"Please input the birthdate of the person"<<endl;
InputDate(pnode->data.birthdate);
cout<<"Please input the deathdate of the person"<<endl;
InputDate(pnode->data.deathdate);
while( !IsDateValid(pnode->data.birthdate) || !IsDateValid(pnode->data.deathdate)|| CompareDate(pnode->data.deathdate,pnode->data.birthdate)!=1 )//
{
//如果不符合日期要求,要重输
cout<<"input error,please reinput"<<endl;
cout<<"Please input the birthdate of the person"<<endl;
InputDate(pnode->data.birthdate);
cout<<"Please input the deathdate of the person"<<endl;
InputDate(pnode->data.deathdate);
}
cout<<"Please input the sex of the person('m' for male & 'f' for female):";
cin>>pnode->data.sex;
cout<<"Please input the height of the person:";
cin>>pnode->data.height;
cout<<"Please input the occupation of the person:";
cin>>pnode->data.occupation;
cout<<"Please input the education of the person:";
cin>>pnode->data.education;
cout<<"Please input the top headship of the person:";
cin>>pnode->data.top_headship;
}
void GEnealogy::InputDate(Date &day)
{
//输入日期
cout<<"Year:";
cin>>day.year;
cout<<"Month:";
cin>>day.month;
cout<<"Day:";
cin>>day.day;
}
int GEnealogy::Age(person pNode)
{
//返回某个人的年龄
return (pNode->data.deathdate.year-pNode->data.birthdate.year);
}
int GEnealogy::Height(person pNode)
{
//返回某个人的身高
return pNode->data.height;
}
void GEnealogy::AverageLife(person &T,int &personNums,int &age)
{
//统计整个家谱的人的平均寿命,age初值为0
if(T)
{
personNums++;age+=Age(T);
AverageLife(T->firstchild,personNums,age);
AverageLife(T->nextsibling,personNums,age);
}
}
void GEnealogy::AverageHeight(person &T, int &personNums, int &height)
{
//统计整个家谱的人的平均身高,personnum,height初值为0
if(T)
{
personNums++;height+=Height(T);
AverageHeight(T->firstchild,personNums,height);
AverageHeight(T->nextsibling,personNums,height);
}
}
void GEnealogy::MaleFemale(person&T,int &maleNum,int &femaleNum)
{
//统计整个家谱的人的男女比例,malenum,femalenum初值为0
if(T)
{
if(strcmp(T->data.sex,"m")==0)
maleNum++;
else
femaleNum++;
MaleFemale(T->firstchild,maleNum,femaleNum);
MaleFemale(T->nextsibling,maleNum,femaleNum);
}
}
int GEnealogy::FamilyNumber(person pnode)
{
//统计以某个人为parent的家庭的人口
int childnum=0;
person p=pnode->firstchild;
for(;p!=NULL;p=p->nextsibling)
childnum++;
return childnum+1;
}
void GEnealogy::TotalFamilyNumber(person &T,int &totalfamily)
{
//统计家谱中家庭人口总数,totalfamily初值为0
if(T)
{
totalfamily+=FamilyNumber(T);
TotalFamilyNumber(T->firstchild,totalfamily);
TotalFamilyNumber(T->nextsibling,totalfamily);
}
}
void GEnealogy::TotalFamily(person &T,int &total)
{
//统计家谱中家庭人数,total初值为0
if(T)
{
if(T)
total++;
TotalFamily(T->firstchild,total);
TotalFamily(T->nextsibling,total);
}
}
void GEnealogy::Statistic()
{
//统计函数,调用上述的各统计函数完成统计功能
int choice;int total=0,totalfamily=0;int male=0,female=0; int personnum1=0,height=0; int personnum2=0,age=0;
while(1)
{
cout<<"please choose statistic operation"<<endl;
cout<<"1.AverageLife"<<endl;
cout<<"2.AverageHeight"<<endl;
cout<<"3.Male&Female Proportion"<<endl;
cout<<"4.AverageFamilyNumber"<<endl;
cout<<"0.Exit"<<endl;
cin>>choice;
switch(choice)
{//选择菜单,以下要注意为完成统计功能,要将int类型转化为float类型
case 1:
AverageLife(T,personnum1,age);
cout<<"The average life of the GEnealogy is "<<float(age)/float(personnum1)<<endl;
break;
case 2:
AverageHeight(T,personnum2,height);
cout<<"The average height of the Genealogy is"<<float(height)/float(personnum2)<<endl;
break;
case 3:
MaleFemale(T,male,female);
cout<<"The Genealogy has "<<male<<" males & "<<female<<" females"<<endl;
break;
case 4:
TotalFamily(T,total);
TotalFamilyNumber(T,totalfamily);
cout<<"The average person of the family is "<<float(totalfamily)/float(total)<<endl;
break;
case 0:
system("cls");
return;
default:
cout<<"input error,reinput"<<endl;//容错检测,如果不符合选择的要求,要重选
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -