族谱管理
族谱(或称家谱)是一种以表谱形式,记载一个以血缘关系为主体的家族世系繁衍和重要人物事迹的特殊图书体裁。族谱是中国特有的文化遗产,是中华民族的三大文献(国史,地志,族谱)之一,属珍贵的人文资料,对于历史学、民俗学、人口学、社会学和经济学的深入研究,均有其不可替代的独特功能。本题对族谱管理进行简单的模拟,以实现查看祖先和子孙个人信息、插入家族成员、删除家族成员、修改家族成员信息、按事迹查找家族成员、列出全部家族成员及关系等功能。家族成员至少包括姓名、性别、生卒年月、配偶、简介等信息,数据结构可自行设计。
/*族谱管理
实现查看祖先和子孙个人信息、插入家族成员、删除家族成员、修改家族成员信息、
按事迹查找家族成员、列出全部家族成员及关系等功能。
家族成员至少包括姓名、性别、生卒年月、配偶、简介等信息,数据结构可自行设计。*/
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef struct Tree{
char name[99];
char sex[10];
char brith[99];
char wife[99];
char other[99];
int ranking;
struct Tree *parent;
struct Tree *brother;
struct Tree *child;
}Tree;
char Member[99],Parent[99];
Tree *CreateGenealogy(Tree *&g)//创建族谱
{
Tree *t;
t = new Tree;
t->brother=NULL;
t->child=NULL;
t->parent=NULL;
cout<<"请输入祖先的姓名"<<endl;
cin>>t->name;
cout<<"请输入祖先的性别"<<endl;
cin>>t->sex;
cout<<"请输入祖先的生日"<<endl;
cin>>t->brith;
cout<<"请输入祖先的配偶"<<endl;
cin>>t->wife;
cout<<"请输入祖先的个人简介"<<endl;
cin>>t->other;
t->ranking=1;
g=t;
return g;
}
Tree *Find(Tree *b,char cname[])
{
Tree *p;
if(b==NULL)
{
//cout<<"NULL"<<endl;
return NULL;
}
else if(strcmp(b->name,cname)==0)
{
return b;
}
else
{
p=Find(b->child,cname);
if(p!=NULL)
{
return p;
}
else
{
return Find(b->brother,cname);
}
}
}
void addMember(Tree *&g,char Parent[],char Member[])
{
Tree *p,*q;
p=Find(g,Parent);
if(p==NULL)
{
cout<<"NULL"<<endl;
return ;
}
//else
q = new Tree;
strcpy(q->name,Member);
cout<<"请输入插入人的性别"<<endl;
cin>>q->sex;
cout<<"请输入插入人的生日"<<endl;
cin>>q->brith;
cout<<"请输入插入人的配偶"<<endl;
cin>>q->wife;
cout<<"请输入插入人的个人简介"<<endl;
cin>>q->other;
q->ranking=p->ranking+1;
q->child=NULL;
q->brother=NULL;
q->parent=p;
if(p->child==NULL)
{
p->child=q;
}
else
{
p=p->child;
while(p->brother!=NULL)
{
p=p->brother;
}
p->brother = q;
}
}
void deleteMember(Tree *&g)
{
Tree *p,*s;
char ch[99];
cout<<"输入你想要删除的人姓名"<<endl;
cin>>ch;
p = Find(g,ch);
if(p==NULL)
{
cout<<"查无此人,无法删除!"<<endl;
return ;
}
/*Tree *q;
s=p;
q=p->parent;
else if(q->child==p)
{
if(p->brother!=NULL)
{
q->child=p->brother;
}
delete s;
}*/
else
{
p->ranking=0;
}
}
void ChangeMsg(Tree *&g)
{
char xm[99];
cout<<"输入你想要修改的人姓名"<<endl;
cin>>xm;
Tree *p,*q;
p = Find(g,xm);
if(p==NULL)
{
cout<<"查无此人,无法修改!"<<endl;
}
else
{
cout<<"请重新输入你想要修改的个人信息"<<endl;
cout<<"请输入姓名"<<endl;
cin>>p->name;
cout<<"请输入性别"<<endl;
cin>>p->sex;
cout<<"请输入生日"<<endl;
cin>>p->brith;
cout<<"请输入其配偶"<<endl;
cin>>p->wife;
cout<<"请输入个人简介"<<endl;
cin>>p->other;
}
}
void LookGenealogy(Tree *b)
{
if(b!=NULL)//此处忘加导致bug
{
for(int i=0;i<b->ranking;i++)
cout<<" ";
cout<<"辈分 "<<b->ranking<<"; ";
cout<<"姓名: "<<b->name<<"; ";
cout<<"性别: "<<b->sex<<"; ";
cout<<"出生年月: "<<b->brith<<"; ";
cout<<"配偶: "<<b->wife<<"; ";
cout<<"个人简介: "<<b->other<<"; "<<endl;
cout<<endl;
if(b->brother!=NULL||b->child!=NULL)
{
LookGenealogy(b->child);
LookGenealogy(b->brother);
}
}
}
void InfoSearch(Tree *b,string str)
{
if(b!=NULL)
{
string s;
s=b->other;
if(s.find(str) < s.length())
{
cout<<"姓名: "<<b->name<<"; "<<endl;
cout<<"个人简介: "<<b->other<<"; "<<endl;
}
else if (b->child!=NULL || b->brother!=NULL)
{
InfoSearch(b->child,str);
InfoSearch(b->brother,str);
}
}
}
void menu()
{
printf("\n");
printf("--------------------------家谱系统---------------------------\n");
printf("\n");
printf(" 1.添加家族成员 \n");
printf("\n");
printf(" 2.删除家族成员 \n");
printf("\n");
printf(" 3.查询个人信息 \n");
printf("\n");
printf(" 4.修改个人信息 \n");
printf("\n");
printf(" 5.查看详细族谱 \n");
printf("\n");
printf(" 6.根据事迹查询 \n");
printf("\n");
printf(" 7.创建族谱 \n");
printf("\n");
printf(" 0.退出系统 \n");
printf("\n");
printf("-------------------------------------------------------------\n");
printf(" 请输入(0-7)进行操作:\n");
printf("-------------------------------------------------------------\n");
printf("\n");
}
int main()
{
int n = 7,m;
Tree *g;
char cname[99];
menu();
while(1&&n)
{
cin>>n;
switch(n)
{
case 5: LookGenealogy(g); break;
case 4: ChangeMsg(g); break;
case 2: deleteMember(g); break;
case 1: cout<<"请输入需要插入人员的长辈姓名"<<endl;
cin>>Parent;
cout<<"请输入需要插入人员的姓名"<<endl;
cin>>Member;
addMember(g,Parent,Member);
break;
case 7: CreateGenealogy(g); break;
case 3: cout<<"请输入需要查询人的姓名"<<endl;
cin>>cname;
//cout<<"..."<<g->name<<endl;why????????????
Tree *t ;
t = Find(g,cname);
if(t)
{
cout<<"姓名 "<<t->name<<endl;
cout<<"性别 "<<t->sex<<endl;
cout<<"出生年月 "<<t->brith<<endl;
cout<<"配偶 "<<t->wife<<endl;
cout<<"个人简介 "<<t->other<<endl;
cout<<"辈分 "<<t->ranking<<endl;
if(t->parent==NULL)
{
cout<<"此人为祖先"<<endl;
}
else
{
cout<<t->name<<"的爸爸是 "<<t->parent->name<<endl;
}
}
else
{
cout<<"查无此人"<<endl;
}
break;
case 6: string str;
cout<<"请输入个人简历关键字"<<endl;
cin>>str;
InfoSearch(g,str);
break;
//case 0: break;
}
}
cout<<"感谢使用!"<<endl;
return 0;
}