📄 familytree.h
字号:
//familytree.h
#include<string.h>
#include<iostream>
#include<fstream.h>
struct member
{
member *wife,*brother;
char *name;
int sex;
char *birth;
char *father;
int marital_status;
char *spouse;
int generation;//代
member(){wife=brother=NULL;}
member(char *n,int s,char *bi,char *f,int m,char *sp,int g,member*w=NULL,member*b=NULL);
};
member::member(char *n,int s,char *bi,char *f,int m,char *sp,int g,member*w,member*b)
{
name=n;
sex=s;
birth=bi;
father=f;//父亲或丈夫
marital_status=m;//婚姻状况
spouse=sp;//配偶
generation=g;
wife=w;
brother=b;
}
class familytree
{
public:
//***************创建族谱****************//
familytree(){count=0;countg=0;root=NULL;};
~familytree(){deletetree(root);};
bool add(member *new_member);//插入一个家族成员
void deletetree(member *sub_root);//清除家族信息
void deletefamily(){deletetree(root);}
//***************打印功能****************//
void print(member *m);//打印一个成员的信息
void printfamily(member *sub_root);//打印家族的所有成员(前序遍历)
void printf(){printfamily(root);}
//***************统计功能****************//
int getcount()const{return count;};//取家族成员人数
int getgeneration(member *sub_root);//取家族总共几代
int getcountg(){getgeneration(root);return countg;}
//***************查询功能****************//
bool findmember(char *name,member *father,member *&p);//找出相应名字的节点的指针
void showfather(char *childname);//找出相应节点的父亲或丈夫并打印出其信息来
void showwife(char *husbanddname);//找出相应节点的妻子并打印出其信息来
void showchilden(char *fatherdname);//找出相应节点的孩子并打印出其信息来
void showbrother(char *brothername);//找出相应节点的兄弟姐妹并打印出其信息来
void showoneself(char *name);//找出相应节点并打印其信息
private:
int count;//家族人员的总人数
int countg;//代
member *root;//祖先节点
};
bool familytree::findmember(char *name,member *father,member *&p)//找出相应名字的节点的指针,并赋给p
{
if(father==NULL)
{
p=NULL;
return false;
}
else if(strcmp(father->name,name)==0)//比较字符串
{
p=father;
return true;
}
else
{
if(findmember(name,father->brother,p))
{
return findmember(name,father->brother,p);
}
else{return findmember(name,father->wife,p);}
}
}
bool familytree::add(member *new_member)//插入一个家族成员节点
{
if(root==NULL)
{
root=new_member;
count++;
new_member->generation=1;//祖先为第一代
return true;
}
member *temp=NULL,*temp1=NULL,*temp2=NULL;
if(!findmember(new_member->father,root,temp)){return false;}//找不到新成员的父亲
else
{
findmember(new_member->father,root,temp);
if(strcmp(new_member->father,new_member->spouse)==0)//如果father和spouse的名字相同,则是father的妻子
{
temp->wife=new_member;
new_member->generation=temp->generation;
temp->marital_status=1;//妻子与丈夫同一代
temp->spouse=new_member->name;
return true;
}
if(temp->wife!=NULL)//是father的儿子
{
temp=temp->wife;//找出妻子节点
temp1=temp;
while(temp->brother!=NULL){temp=temp->brother;}
temp->brother=new_member;//在最后一个儿子节点后插入新成员节点
if(temp1==temp){new_member->generation=temp->generation+1;}//儿子代数=母亲代数+1
else{new_member->generation=temp->generation;}//和兄弟同一代
count++;
return true;
}
cout<<"\nERROR! "<<new_member->father<<" has no wife yet.\n\n";
return false;
}
}
void familytree::deletetree(member *sub_root)//清除家族信息
{
if(sub_root!=NULL)
{
deletetree(sub_root->brother);
deletetree(sub_root->wife);
delete sub_root;
}
}
void familytree::print(member *m)//打印一个成员的信息
{
cout<<m->name;
if(m->sex==0){cout<<"\t♀";}
else{cout<<"\t♂";}
cout<<" "<<m->birth<<" "<<m->father<<" ";
if((m->sex==0)&&(strcmp(m->father,m->spouse)!=0)) //如果是女儿的话,则不输出配偶信息
{
if(m->marital_status==0){cout<<"\tsingle"<<"\t "<<"\t "<<m->generation;}
else{cout<<"\tmarried"<<"\t "<<"\t "<<m->generation;}
cout<<endl;
}else{ //如果是其他人员的话,则输出配偶信息
if(m->marital_status==0){cout<<"\tsingle"<<"\t "<<"\t "<<m->generation;}
else{cout<<"\tmarried"<<" "<<m->spouse<<"\t "<<m->generation;}
cout<<endl;
}
}
void familytree::printfamily(member *sub_root)//打印家族的所有成员(前序遍历)
{
if(sub_root!=NULL)
{
print(sub_root);
printfamily(sub_root->wife);
printfamily(sub_root->brother);
}
}
void familytree::showfather(char *childname)//找出相应节点的父亲或丈夫并打印出其信息来
{
member *temp1=NULL,*temp2=NULL;
if(!findmember(childname,root,temp1))
{
cout<<"ERROR!The name "<<childname<<" is not found in the family!\n";
return;
}
findmember(childname,root,temp1);
if(!findmember(temp1->father,root,temp2))
{
cout<<"ERROR!Can't find "<<childname<<"'s father!\n";
return;
}
else//找到被查询的节点及其父亲节点
{
findmember(temp1->father,root,temp2);
if(strcmp(temp1->father,temp1->spouse)==0)//判断找的是丈夫还是父亲
{
cout<<"---------------------------------"<<childname<<"'s husband------------------------------------\n";
}else{cout<<"---------------------------------"<<childname<<"'s father-----------------------------------\n";}
cout<<"name"<<" sex"<<" birthday"<<" father(husband)"<<" marital status"<<" spouse"<<" generation";
print(temp2);
cout<<endl;
return;
}
}
void familytree::showwife(char *husbandname)//找出相应节点的妻子并打印出其信息来
{
member *temp=NULL;
if(!findmember(husbandname,root,temp))
{
cout<<"ERROR! The name "<<husbandname<<" is not found in the family!\n";
return;
}
findmember(husbandname,root,temp);
temp=temp->wife;
if(temp==NULL)
{
cout<<"He don't have wife yet!\n";
return;
}
else
{
cout<<"----------------------------------"<<husbandname<<"'s wife------------------------------------\n";
cout<<"name"<<" sex"<<" birthday"<<" father(husband)"<<" marital status"<<" spouse"<<" generation";
print(temp);
cout<<endl;
return;
}
}
void familytree::showchilden(char *fatherdname)//找出相应节点的孩子并打印出其信息来
{
member *temp=NULL;
if(!findmember(fatherdname,root,temp))
{
cout<<"ERROR! The name "<<fatherdname<<" is not found in the family!\n";
return ;
}
findmember(fatherdname,root,temp);
if(strcmp(temp->father,temp->spouse)==0) //若为妻子结点
{
temp=temp->brother;
}
else if(temp->sex==0) //若为除妻子结点外的女性结点
{cout<<"He doesn't have any children yet!\n"; return;}
else if(temp->marital_status==0) //若为未婚结点
{cout<<"He doesn't have any children yet!\n"; return;}
else{
temp=temp->wife;
temp=temp->brother;
}
if(temp==NULL)
{
cout<<"He doesn't have any children yet!\n";
return;
}
else
{
cout<<"-----------------------------------"<<fatherdname<<"'s children---------------------------------\n";
cout<<"name"<<" sex"<<" birthday"<<" father(husband)"<<" marital status"<<" spouse"<<" generation";
int num=0;
while(temp!=NULL)
{
num++;
print(temp);
temp=temp->brother;
}
cout<<"He has "<<num<<" children\n";
cout<<endl;
return;
}
}
void familytree::showbrother(char *brothername)//找出相应节点的兄弟姐妹并打印出其信息来
{
member *temp1,*temp2;
if(!findmember(brothername,root,temp1))
{
cout<<"ERROR! The name "<<brothername<<" is not found in the family!\n";
return;
}
else
{
findmember(brothername,root,temp1);
if(strcmp(temp1->father,temp1->spouse)==0) cout<<"ERROR! she is "<<temp1->father<<"'s wife!\n";
else
{
if(temp1==root)
{
cout<<"ERROR! "<<brothername<<" is the ancestor!\n";
return;
}
else
{
findmember(temp1->father,root,temp2);
temp2=temp2->wife;
temp2=temp2->brother;
cout<<"--------------------------------"<<brothername<<"'s brothers----------------------------------\n";
cout<<"name"<<" sex"<<" birthday"<<" father(husband)"<<" marital status"<<" spouse"<<" generation";
int num=0;
while(temp2!=NULL)
{
if(strcmp(temp2->name,brothername)!=0)//不输出被查找的成员的信息
{
num++;
print(temp2);
temp2=temp2->brother;
}
else{temp2=temp2->brother;}
}
cout<<brothername<<" has "<<num<<" brothers\n";
cout<<endl;
return;
}
}
}
}
int familytree::getgeneration(member *sub_root)//取家族总共几代
{
if(sub_root!=NULL)
{
getgeneration(sub_root->brother);
getgeneration(sub_root->wife);
if(sub_root->generation>countg){countg=sub_root->generation;}
}
return countg;
}
void familytree::showoneself(char *name)
{
member *temp=NULL;
if(!findmember(name,root,temp))
{
cout<<"ERROR! The name "<<name<<" is not found in the family!\n";
return ;
}
findmember(name,root,temp);
cout<<"--------------------------------"<<name<<"'s information----------------------------------\n";
cout<<"name"<<" sex"<<" birthday"<<" father(husband)"<<" marital status"<<" spouse"<<" generation";
print(temp);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -