代码是这样,还没做完
#include
#include
#include
#include
#include
using namespace std;
bool function(char a[],char b[])
{
int n = 0;
if(strlen(a)==strlen(b))
{
while(a[n]!='\0' && b[n]!='\0')
{
if(a[n] == b[n])
n++;
else
return 0;
}
}
else return 0;
return 1;
}//对比两个字符数组是否相等的函数
struct FamilyTree
{
char name[50];
int birthday;
int dieday;
int sex;
int height;
int weight;
bool marriage;
int edu;
char prof[50];
FamilyTree *child;
FamilyTree *brother;
};//要储存的结构
FamilyTree searchtg(char name[50],FamilyTree *tg)
{
FamilyTree *n=NULL;
if(function(tg->name,name))return *tg;
else
{
if(tg->child!=NULL)*n=searchtg(name,tg->child);
if((tg->brother!=NULL)&&(n==NULL))*n=searchtg(name,tg->brother);
return *n;
}
}//在二叉树里找节点的函数
FamilyTree searchrelation(char name[50],FamilyTree *tg)
{
FamilyTree *n=tg->brother;
FamilyTree *s=tg->child;
if(function(s->name,name))return *s;
if(function(n->name,name))return *n;
if(tg->child!=NULL)*s=searchrelation(name,tg->child);
if(tg->brother!=NULL)*n=searchrelation(name,tg->brother);
if(s!=NULL)if(function(s->name,name))return *s;
if(n!=NULL)if(function(n->name,name))return *n;
n=NULL;
return *n;
}//在二叉树里找某个节点的相关节点的函数
void readinfo(FamilyTree *p)
{
cout<<"请输入姓名"<
cin>>p->name;
cout<<"请输入出生日期"<
cin>>p->birthday;
cout<<"请输入死亡日期(仍在世则输入0)"<
cin>>p->dieday;
cout<<"请输入性别(1为男0为女)"<
cin>>p->sex;
cout<<"请输入身高(cm)体重(kg)"<
cin>>p->height;
cin>>p->weight;
cout<<"请输入婚姻状况(0为未婚1为已婚)"<
cin>>p->marriage;
cout<<"请输入学历(0为未接受教育,1为中专及以下,2为大学及大专,3为研究生,4为博士生及以上"<
cin>>p->edu;
cout<<"请输入职位"<
cin>>p->prof;
} //读数据
void showinfo(FamilyTree *p)
{
cout<<"姓名:"<name<
cout
if(p->sex==1)cout<<"男"<
else cout
coutbirthday;
if(p->dieday==0)cout<<" 尚在世"<
else coutdieday<
coutheight<<" "<<"体重:"<weight<
cout
if(p->marriage)cout<<"已婚"<
else cout
cout
switch(p->edu)
{
case 0:cout<<"未接受教育"<
case 1:cout
case 2:cout
case 3:cout
case 4: cout
}
coutprof<
}//展示数据
void deleteTree()
{
char filename[100];
scanf("%s",&filename);
int i=0;
while(filename[i]!='\0')i++;
filename[i++]='.';
filename[i++]='d';
filename[i++]='a';
filename[i++]='t';
if(remove(filename)) printf("无法删除家谱文件 %s \n",filename);
else printf("家谱文件已删除 \n");
}//删除一个文件
void add(FamilyTree *th)
{
FamilyTree *s,*n;
int od;
char name[50];
s=new FamilyTree;
s->child=NULL;
s->brother=NULL;
readinfo(s);
cin>>name;
*n=searchtg(name,th);
if(n!=NULL)
{
cout<<"请输入亲缘关系(1为加入者是兄弟姐妹,2为加入者是儿子女儿)"<
cin>>od;
if(od==2)n=n->child;
while(n->brother!=NULL)n=n->brother;
n->brother=s;
}
else cout<<"错误!此家谱中没有该人"<
}//在二叉树上增加一个节点
void remove(FamilyTree *th)
{
char name[50];
cin>>name;
FamilyTree *n,*s;
*n=searchtg(name,th);
if(n==NULL)cout<<"错误!此家谱中没有该人人!"<
else
{
*s=searchrelation(n->name,th);
if(s->child==n)
{
if(n->brother!=NULL)s->child=n->brother;
else s->child=NULL;
delete n;
}
if(s->brother==n)
{
if(n->brother!=NULL)s->brother=n->brother;
else s->brother=NULL;
delete n;
}
}
}//删除二叉树上一个节点
void search(FamilyTree *th)
{
char name[50];
cin>>name;
FamilyTree *n;
*n=searchtg(name,th);
if(n==NULL)cout<<"错误!此家谱中没有该人人!"<
else showinfo(n);
}//展示某个节点的数据
void change(FamilyTree *th)
{
char name[50];
int od;
cin>>name;
FamilyTree *n;
*n=searchtg(name,th);
if(n==NULL)cout<<"错误!此家谱中没有该人人!"<
else
{
cout
while(cin>>od!=0)
switch(od)
{
case 1:{cout<<"请输入新名字"<
cin>>n->name;break;}
case 2:{cout<<"请输入新的出生日期"<
cin>>n->birthday;break;}
case 3:{cout<<"请输入死亡日期(仍在世则输入0)"<
cin>>n->dieday;break;}
case 4:{cout<<"请输入身高(cm)"<
cin>>n->height;break;}
case 5:{cout<<"请输入体重(kg)"<
cin>>n->weight;break;}
case 6:{cout<<"请输入婚姻状况(0为未婚1为已婚)"<
cin>>n->marriage;break;}
case 7:{cout<<"请输入学历(0为未接受教育,1为中专及以下,2为大学及大专,3为研究生,4为博士生及以上"<
cin>>n->edu;break;}
case 8:{cout<<"请输入职位"<
cin>>n->prof;break;}
}
}
}//修改某个节点的数据
void operateTree(FamilyTree *th)
{
int o;
cout<<"请选择操作:"<
cout
cin>>o;
switch(o)
{
case 1:add(th);break;
case 2:remove(th);break;
case 3:search(th);break;
case 4:change(th);break;
}
}//对某个树进行操作
void operate()
{
int o;
FamilyTree *n;
char ansname[50];
cout<<"请选择操作:"<
cout
cin>>o;
switch(o)
{
// case 1:addTree();break; //这个是增加一个新的树,每棵树创建一1个单独的dat文件储存,文件名为根节点的name……但是我不知道怎么做
case 2:deleteTree();break;
case 3:
{
cout<<"请输入树的祖先的名称"<
cin>>name;
n=getTree(name) //这个程序我没有写,要完成的功能是把对应名字的dat文件中的二叉树读出来,然后n为其根节点
operateTree();
break;
}
}
}
//未完成
int main()
{
operate();
}
大概就是我不知道怎么用文件储存二叉树,也不知道怎么读出来,希望大家帮帮忙