#include<iostream.h>#include<string.h>#include<fstream.h>#include<stdlib.h>struct per{int data;char name[20];char brith[20];char marry;char address[20];char live;char dietime[20];char parent[20];int generation;int child[10];int numberchild;per(){for(int i=0;i<20;i++)name[i]='\0';for(i=0;i<20;i++)brith[i]='\0';for(i=0;i<20;i++)address[i]='\0';for(i=0;i<20;i++)dietime[i]='\0';for(i=0;i<10;i++)child[i]=0;for(i=0;i<20;i++)parent[i]='\0';live='\0';marry='\0';data=-10;generation=0;numberchild=0;}};void setupinfo(per person[]);void display(per person[],int&);void displayhome(per person[],int&);void displayinfo(perperson[],int&);void namesearch(per person[],int&);void birtfdaysearch(per person[],int&);void addchild(per person[],int&);void editmember(per person[],int&);void sortbirthday(per person[],int&);void main(){per person[36];int count=0;fstreaminputFile("person.txt",ios::in|ios::o ut|ios::binary);if(!inputFile){cout<<"文件不存在!"<<endl;exit(0);}inputFile.read((char*)&person[co unt],sizeof(person[count]));//从文件中读取指定大小的字节函数read(),读取字节函数while(!inputFile.eof()) //当文件没有读取完毕,进行循环{if(person[count].name[0]!='\0')//当名字不为空时{count++; //统计人数inputFile.read((char*)(person+count ),sizeof(*person));}}inputFile.close();//关闭文件if(count<1) //如果家谱图里面没人{cout<<" 家谱不存在,请你建立一个家谱!!"<<endl;setupinfo(person);}display(person,count);}void setupinfo(per person[36]) //创建信息{int i,j,k;fstreamoutputfile("person.txt",ios::in|ios::out|ios::binary|ios::app); //打开文件for(i=0;i<5;i++){cout<<" 姓名 : ";cin.getline(person[i].name,20);cout<<" 生日(日期样式为xxxx/xx/xx) : ";cin.getline(person[i].brith,20);cout<<" 婚否(输入Y或y或N或n) :";cin>>person[i].marry;while(toupper(person[i].marry)!='Y' &&toupper(person[i].marry)!='N'){cout<<" 你输入错误,请你重新输入!!"<<endl;cout<<" 婚否(输入Y或y或N或n) :";cin>>person[i].marry;}cin.ignore();cout<<" 地址 : ";cin.getline(person[i].address,20);cout<<" 是否健在(输入Y或y或N或n) :";cin>>person[i].live; //是否死亡while(toupper(person[i].live)!='Y'& &toupper(person[i].live)!='N'){cout<<" 你输入错误,请你重新输入!!"<<endl;cout<<" 婚否(输入Y或y或N 或n) :";cin>>person[i].live;}cin.ignore();if(toupper(person[i].live)=='N'){cout<<" 死亡日期(日期样式为xxxx/xx/xx) :";cin.getline(person[i].dietime,20);/ / 输入死亡日期}cout<<" 属于第几代 : ";cin>>person[i].generation;cin.ignore();if(person[i].generation!=1) //不是第一代{cout<<" 他(她)的父亲 : ";cin.getline(person[i].parent,20); //cin.getline是读取字符串函数 20是读取字符串的个数}cout<<endl;}for(j=0;j<i;j++){if(person[j].generation==1)person[j].data=-1;//****************************}for(j=0;j<i;j++){for(k=0;k<i;k++){if(strcmp(person[j].parent,person[k].name)==0) //比较2个字符串是否相同,实质是找某个人的父亲person[j].data=k;}}for(j=0;j<i;j++){for(k=0;k<i;k++){if(strcmp(person[j].parent,person[k].name)==0){person[k].child[person[k].numberchild]=j;person[k].numberchild++; //找某个人的孩子}}}for(j=0;j<i;j++)outputfile.write((char*)(person+j), sizeof(*person)); //写文件outputfile.close(); //关闭文件夹system("cls");}void display(per person[36],int &n1){int choice;while(1) //循环式一直进行的{cout<<endl<<endl<<endl;cout<<"\t\t 家族关系查询系统"<<endl;cout<<"\t\t1. 显示家谱"<<endl;cout<<"\t\t2. 显示第n代人的所有信息"<<endl;cout<<"\t\t3. 按照姓名查询某成员的信息"<<endl;cout<<"\t\t4. 按照出生日期查询成员名单"<<endl;cout<<"\t\t5. 某成员添加孩子"<<endl;cout<<"\t\t6. 修改某成员信息"<<endl;cout<<"\t\t7. 按生日日期对家谱中的所有人进行排序"<<endl;cout<<"\t\t8. 退出系统"<<endl;cout<<"\t\t 输入选择:"<<endl;cout<<"\t\t 请输入1-8之间的数"<<endl;cin>>choice;while(choice!=1&&choice!=2&&choice!=3&&cho ice!=4&&choice!=5&&choice!=6&&choice!=7&&choice!=8){cout<<"you enter wrong!"<<endl;cout<<"please enter again!"<<endl;cin>>choice;}cin.ignore(1,'\n'); //跳过一个字符,含义是忽略一行system("cls");switch(choice){case 1: displayhome(person,n1); break;case 2: displayinfo(person,n1); break;case 3: namesearch(person,n1); break;case 4: birtfdaysearch(person,n1); break;case 5: addchild(person,n1); break;case 6: editmember(person,n1); break;case 7: sortbirthday(person,n1); break;case 8: exit(0);}}}void displayhome(per person[36],int &n2) //调用函数的参数传递的是一个参数地址{int i,j,max;char again;cout<<endl<<endl;for(i=0;i<n2-1;i++){max=person[i].generation;for(j=i+1;j<n2;j++){if(person[i].generation<person[j].g eneration)max=person[j].generation; //找出最大的代数}}for(i=1;i<=max;i++){cout<<" 第"<<i<<"代的成员有 : ";for(j=0;j<n2;j++){if(person[j].generation==i)cout<<person[j].name<<" ";//输出每一代的人}cout<<endl;}cout<<endl<<endl;cout<<" 你是否想继续查看?如果继续请输入Y或y,否则输入N或n!"<<endl;cin>>again;if(toupper(again)=='Y') //就是判断输入得是否为y或Y{system("cls");return;}if(toupper(again)=='N')exit(0); //终止程序的执行}void displayinfo(per person[36],int &n2) //显示代数{int i,gen,j=0;char again;cout<<endl<<endl;cout<<" 请你输入你要查询的代数 : ";cin>>gen;cout<<endl; //换行for(i=0;i<n2;i++){if(person[i].generation!=gen)j++;}if(j==n2) //判断是否找到cout<<" 你要查的代数还没有!!!"<<endl<<endl;for(i=0;i<n2;i++){if(person[i].generation==gen){cout<<" 姓名: "<<person[i].name<<endl<<" 出生日期: "<<person[i].brith<<" 第几代: "<<person[i].generation<<endl<<" 婚否: "<<person[i].marry<<" 地址 :"<<person[i].address<<endl<<" 是否健在: "<<person[i].live<<endl;if(person[i].live=='N'||person[i].liv e=='n')cout<<" 死亡日期: "<<person[i].dietime<<endl;cout<<endl<<endl;}}cout<<" 你是否想继续查看?如果继续请输入Y或y,否则输入N或n!"<<endl;cin>>again;if(toupper(again)=='Y'){system("cls");return;}if(toupper(again)=='N')exit(0);}void namesearch(per person[36],int &n2){int i,j=0,k;char again,name[20];cout<<endl<<endl;cout<<" 请你输入你想要查询的人的姓名 : ";cin.getline(name,20);for(i=0;i<n2;i++){if(strcmp(person[i].name,name)!=0)j++;}cout<<endl<<endl;if(j==n2)cout<<" 你要查询的人没有!!!"<<endl<<endl;for(i=0;i<n2;i++){if(strcmp(person[i].name,name)==0){cout<<" 姓名: "<<person[i].name<<endl<<" 出生日期: "<<person[i].brith<<" 第几代: "<<person[i].generation<<endl<<" 婚否: "<<person[i].marry<<" 地址 :"<<person[i].address<<endl<<" 是否健在: "<<person[i].live<<endl;if(person[i].live=='N'||person[i].liv e=='n')cout<<" 死亡日期:"<<person[i].dietime<<endl;cout<<endl<<endl;if(person[i].generation==1)cout<<" 他是这个家的根,没有父亲!!!!"<<endl<<endl;else{cout<<" 他(她)父亲的信息 : "<<endl;cout<<" 姓名: "<<person[person[i].data].name<<endl<<" 出生日期: "<<person[person[i].data].brith<<" 第几代: "<<person[person[i].data].generation< <endl<<" 婚否: "<<person[person[i].data].marry<<" 地址:"<<person[person[i].data].address <<endl<<" 是否健在: "<<person[person[i].data].live<<endl;if(person[person[i].data].live=='N' ||person[person[i].data].live=='n')cout<<" 死亡日期: "<<person[person[i].data].dietime<<en dl;cout<<endl<<endl;}if(person[i].numberchild==0)cout<<" 他没有孩子!!!"<<endl<<endl;else{cout<<" 他有"<<person[i].numberchild<<"个孩子,他们的信息 :"<<endl<<endl;for(k=0;k<person[i].numberchild;k++ ){cout<<" 姓名: "<<person[person[i].child[k]].name<<e ndl<<" 出生日期: "<<person[person[i].child[k]].brith<<" 第几代: "<<person[person[i].child[k]].generat ion<<endl<<" 婚否: "<<person[person[i].child[k]].marry<<" 地址:"<<person[person[i].child[k]].add ress<<endl<<" 是否健在: "<<person[person[i].child[k]].live;if(person[person[i].child[k]].live= ='N'||person[person[i].child[k]].live =='n')cout<<" 死亡日期 : "<<person[person[i].child[k]].dietime<<endl;cout<<endl<<endl;}}}}cout<<" 你是否想继续查看?如果继续请输入Y或y,否则输入N或n!"<<endl;cin>>again;if(toupper(again)=='Y'){system("cls");return;}if(toupper(again)=='N')exit(0);}void birtfdaysearch(perperson[36],int &n2){charbegbir[20],endbir[20],again;int i,flag=0;cout<<endl<<endl;cout<<" 请你输入两个时间,格式为****/**/**!"<<endl;cout<<" 起始时间是 : ";cin.getline(begbir,20);cout<<" 终止时间是 : ";cin.getline(endbir,20);cout<<endl<<endl;for(i=0;i<n2;i++){if(strcmp(person[i].brith,begbir)>= 0&&strcmp(person[i].brith,endbir)<=0) {cout<<" 姓名: "<<person[i].name;cout<<" 生日:"<<person[i].brith;flag=1;cout<<endl;}}cout<<endl<<endl;if(flag==0)cout<<" 在这段时间内没有人生日!!!"<<endl<<endl;cout<<" 你是否想继续查看?如果继续请输入Y或y,否则输入N或n!"<<endl;cin>>again;if(toupper(again)=='Y'){system("cls");return;}if(toupper(again)=='N')exit(0);}void addchild(per person[36],int &n2){int i;char again;fstreamfile("person.txt",ios::in|ios::out|io s::binary|ios::app);cout<<endl<<endl;cout<<" 请输入孩子的信息!!!"<<endl<<endl<<endl;cout<<" 姓名 : ";cin.getline(person[n2].name,20);cout<<" 生日(日期样式为xxxx/xx/xx) : ";cin.getline(person[n2].brith,20)cout<<" 婚否(输入Y或y或N或n) :";cin>>person[n2].marry;while(toupper(person[n2].marry)! ='Y'&&toupper(person[n2].marry)!='N') {cout<<" 你输入错误,请你重新输入!!"<<endl;cout<<" 婚否(输入Y或y或N或n) :";cin>>person[n2].marry;}cin.ignore();cout<<" 地址 : ";cin.getline(person[n2].address,2 0);cout<<" 是否健在(输入Y或y或N或n) :";cin>>person[n2].live;while(toupper(person[n2].live)!= 'Y'&&toupper(person[n2].live)!='N') {cout<<" 你输入错误,请你重新输入!!"<<endl;cout<<" 婚否(输入Y或y或N或n) :";cin>>person[n2].live;}cin.ignore();if(toupper(person[n2].live)=='N' ){cout<<" 死亡日期(日期样式为xxxx/xx/xx) :";cin.getline(person[n2].dietime,20);}cout<<" 属于第几代 : ";cin>>person[n2].generation;cin.ignore();if(person[n2].generation!=1){cout<<" 他(她)的父亲 : ";cin.getline(person[n2].parent,20);}cout<<endl;for(i=0;i<n2;i++){if(strcmp(person[n2].parent,person[ i].name)==0){person[n2].data=i;break;}}file.write((char*)&person[n2],si zeof(person[n2]));file.close();file.open("person.txt",ios::in|i os::out|ios::binary);file.seekp(i*sizeof(per),ios::be g);person[i].child[person[i].number child]=n2;person[i].numberchild++;file.write((char*)&person[i],siz eof(per));file.close();n2++;cout<<" 你是否想继续查看?如果继续请输入Y或y,否则输入N或n!"<<endl;cin>>again;if(toupper(again)=='Y'){system("cls");return;}if(toupper(again)=='N')exit(0);}void editmember(per person[36],int &n2){int i,flag=0,choice;char name[20],again;fstreamfile("person.txt",ios::in|ios::out|io s::binary);cout<<endl<<endl;cout<<" 请你输入你想修改的成员的名字 : ";cin.getline(name,20);cout<<endl<<endl;for(i=0;i<n2;i++){if(strcmp(person[i].name,name)==0){flag=1;break;}}if(flag==0)cout<<" 家谱中没有这个人!!"<<endl<<endl<<endl;if(flag==1){cout<<"\t\t请你选择你要修改的项!!"<<endl;cout<<"\t\t 1. 姓名"<<endl;cout<<"\t\t 2. 地址"<<endl;cout<<"\t\t 3. 婚否"<<endl;cout<<"\t\t 4. 生日"<<endl;cout<<"\t\t 5. 是否死亡"<<endl;cout<<"\t\t 6. 死亡日期"<<endl;cout<<"\t\t 请输入1-6之间的数"<<endl;cout<<"\t\t 输入选择 : ";cin>>choice;while(choice!=1&&choice!=2&&choice!=3&&cho ice!=4&&choice!=5&&choice!=6){cout<<"you enter wrong!"<<endl;cout<<"please enter again!"<<endl;cin>>choice;}cin.ignore(1,'\n');switch(choice){case 1: cout<<endl<<" 请你输入新名字 : ";cin.getline(person[i].name,20);file.seekp(i*sizeof(per),ios::beg);//file.write((char*)&person[i],sizeof(p er));break;case 2: cout<<endl<<" 请输入新的地址 : ";cin.getline(person[i].address,20);//file.seekp(i*sizeof(per),ios::beg);//file.write((char*)&person[i],sizeof(p er));break;case 3: cout<<endl<<" 请你输入新的是否结婚 : ";cin>>person[i].marry;//file.seekp(i*sizeof(per),ios::beg);file.write((char*)&person[i],sizeof(p er));break;case 4: cout<<endl<<" 请你输入新的生日 : ";cin.getline(person[i].brith,20);//file.seekp(i*sizeof(per),ios::beg);//file.write((char*)&person[i],sizeof(p er));break;case 5: cout<<endl<<" 请你输入新的是否死亡 : ";cin>>person[i].live;//file.seekp(i*sizeof(per),ios::beg);//file.write((char*)&person[i],sizeof(p er));break;case 6: cout<<endl<<" 请你输入新的死亡日期 : ";cin.getline(person[i].dietime,20);//file.seekp(i*sizeof(per),ios::beg);//file.write((char*)&person[i],sizeof(p er));break;}for(int j=0;j<n2;j++)file.write((char*)&person[j],sizeof (per));}cout<<endl<<endl;cout<<" 你是否想继续查看?如果继续请输入Y或y,否则输入N或n!"<<endl;cin>>again;if(toupper(again)=='Y'){system("cls");return;}if(toupper(again)=='N')exit(0);}void sortbirthday(per person[36],int &n2){per person1;int i,j;char again;cout<<endl<<endl;for(i=0;i<n2-1;i++)for(j=i+1;j<n2;j++){if(strcmp(person[i].brith,person[j].brith)>=0){person1=person[i];person[i]=person[j];person[j]=person1;}}for(i=0;i<n2;i++)cout<<" 姓名: "<<person[i].name<<" 生日: "<<person[i].brith<<endl;cout<<endl<<endl;cout<<" 你是否想继续查看?如果继续请输入Y或y,否则输入N或n!"<<endl;cin>>again;if(toupper(again)=='Y'){system("cls");return;}if(toupper(again)=='N')exit(0);}。