📄 family.h
字号:
#ifndef FAMILY_CLASS
#define FAMILY_CLASS
#include<ctype.h>
#include<fstream>
#include<vector>
#include"Person.h"
#include"d_myBirthHeap.h"
#include"d_myNameHeap.h"
using namespace std;
extern int memberNum;
extern int isInt(char s[]);
class Family
{
public:
FILE *fp;
void title();
void menu1();
void menu2();
void Persontitle();
void buildFamily(vector<Person>&v);//自己创建一个家谱
bool readFamily(vector<Person>&v);//文件导入一个家谱
bool writeFamily(vector<Person>&v);//一个家谱写入文件
void outputgeneration(vector<Person>&v);//显示第n 代所有人的信息
void outputbyName(vector<Person>&v);//按照姓名查询,输出成员信息
void outputbyBirth(vector<Person>&v);//按照出生日期查询成员名单
void relationship(vector<Person>&v);//输入两人姓名,确定其关系
void addchild(vector<Person>&v);//某成员添加孩子
void birthsort(vector<Person>&v);//按出生日期对家谱中所有人排序
void updatemember(vector<Person>&v);//修改某成员信息
void deletemember(vector<Person>&v);//删除某成员
int searchbyName(vector<Person>&v);//按姓名查询
int searchbyBirth(vector<Person>&v);//按出生日期查询
int binSearchName(vector<Person>&v,int first, int last, char target[]);//姓名的二分查找函数
int binSearchBirth(vector<Person>&v,int first, int last);//出生日期的二分查找函数
// void ttt(vector<Person>&v);用于调试
};
void Family::relationship(vector<Person>&v)
{
int n=v.size(),i,j;
char name1[20];
char name2[20];
cout<<"输入第一个人的姓名:";
cin>>name1;
cout<<"输入第二个人的姓名:";
cin>>name2;
i=binSearchName(v,0,n,name1);
j=binSearchName(v,0,n,name2);
if(i==v.size())
cout<<name1<<" 不是家族成员"<<endl;
else if(j==v.size())
cout<<name2<<" 不是家族成员"<<endl;
else
{
int k=v[i].generation,m=v[j].generation;
if(k>m)//v[j]是v[i]的长辈
{
int p=v[i].parentID;
while(1)
{
if(v[p].generation<=m)
break;
p=v[p].parentID;
}
if(v[p].generation==m&&p==j)
{
cout<<v[j].name <<" 是 "<<v[i].name<<"长"<<k-m<<"辈,而且是直系亲属"<<endl;
}
else
{
cout<<v[j].name <<" 比 "<<v[i].name<<"长"<<k-m<<"辈,但不是直系亲属"<<endl;
}
}
else if(k<m)//v[i]是v[j]的长辈
{
int p=v[j].parentID;
while(1)
{
if(v[p].generation<=k)
break;
p=v[p].parentID;
}
if(v[p].generation==k&&p==i)
{
cout<<v[i].name <<" 是 "<<v[j].name<<"长"<<m-k<<"辈,而且是直系亲属"<<endl;
}
else
{
cout<<v[i].name <<" 比 "<<v[j].name<<"长"<<m-k<<"辈,但不是直系亲属"<<endl;
}
}
else
{
if(v[i].sex==v[j].sex)
{
if(v[i].sex=='M')
cout<<v[i].name <<" 与 "<<v[j].name<<"是兄弟"<<endl;
else
cout<<v[i].name <<" 与 "<<v[j].name<<"是姐妹"<<endl;
}
else
cout<<v[i].name <<" 与 "<<v[j].name<<"是兄妹"<<endl;
}
}
}
void Family::outputbyBirth(vector<Person>&v)
{
int n=v.size();
int i=searchbyBirth(v);
if(i!=n)
{
Persontitle();
v[i].outputPerson();
}
else
cout<<"无此人信息"<<endl;
}
void Family::deletemember(vector<Person>&v)
{
char choice;
void delet(vector<Person>&v,int k);//声明递归删除函数
cout<<"1:按姓名查询删除成员"<<endl;
cout<<"2:按出生日期查询删除成员"<<endl;
cout<<"请选择:";
bool flag=true;
while(flag)
{
choice=getch();
cout<<choice<<endl;
if(choice=='1'||choice=='2')
flag=false;
else
{cout<<"输入错误!!!"<<endl;cout<<"重新输入:";}
}
int n=v.size(),i;
switch(choice)
{
case '1': cout<<"请输入姓名:";char target[20];cin>>target;i=binSearchName(v,0,n,target);break;
case '2': i=binSearchBirth(v,0,n);break;
}
if(i!=n)
{
v[i].parentID=-2;
memberNum--;
delet(v,i);
vector<Person>temp;
for(int j=0;j<v.size();j++)
if(v[j].parentID!=-2)
{
temp.push_back(v[j]);
for(int k=0;k<v.size();k++)
if(v[k].parentID==j)
v[k].parentID=temp.size()-1;
}
v.resize(0);
for(j=0;j<temp.size();j++)
v.push_back(temp[j]);
}
else
cout<<"无此人信息"<<endl;
}
void delet(vector<Person>&v,int k)
{
for(int i=0;i<v.size()&&v[i].parentID!=k;i++)
;
if (i==v.size())
return;
else
{
for(int j=0;j<v.size();j++)
{
if(v[j].parentID==k)
{
v[j].parentID=-2;//父亲为-2,说明此项已删除
memberNum--;
delet(v,j);
}
}
}
}
void Family::updatemember(vector<Person>&v)
{
char choice;
cout<<"1:按姓名查询修改成员"<<endl;
cout<<"2:按出生日期查询修改成员"<<endl;
cout<<"请选择:";
bool flag=true;
while(flag)
{
choice=getch();
cout<<choice<<endl;
if(choice=='1'||choice=='2')
flag=false;
else
{cout<<"输入错误!!!"<<endl;cout<<"重新输入:";}
}
int n=v.size(),i;
switch(choice)
{
case '1': cout<<"请输入姓名:";char target[20];cin>>target;i=binSearchName(v,0,n,target);break;
case '2': i=binSearchBirth(v,0,n);break;
}
if(i!=n)
{
Persontitle();
v[i].outputPerson();
cout<<setiosflags(ios::left)<<setw(15)<<"1:出生日期"<<setw(10)<<"2:姓名"<<setw(10)<<"3:第几代\n"
<<setw(10)<<"4:性别"<<setw(10)<<"5:健在否"<<setw(7)<<"6:婚否"
<<setw(10)<<"7:死亡日期"<<"8:地址"<<endl;
cout<<"请选择修改项:";
char c;
flag=true;
while(flag)
{
c=getch();
if(c>='1'&&c<='8')
flag=false;
else
{cout<<"输入错误!!!"<<endl;cout<<"重新输入:";}
}
switch(c)
{
case '1':
{
cout<<"出生日期:"<<endl;
flag=false;
char s[50];
cout<<"请输入出生年份:";
bool t=true;
while(t)
{
cin>>s;
if(isInt(s)!=-1)
{
v[i].year=isInt(s);
t=false;
}
else
cout<<"输入错误!!!\n请请重新输入:";
}
cout<<"请输入出生月份:";
t=true;
while(t)
{
cin>>s;
if(isInt(s)!=-1)
{
v[i].month=isInt(s);
t=false;
}
else
cout<<"输入错误!!!\n请请重新输入:";
}
cout<<"请输入出生日子:";
t=true;
while(t)
{
cin>>s;
if(isInt(s)!=-1)
{
v[i].day=isInt(s);
t=false;
}
else
cout<<"输入错误!!!\n请请重新输入:";
}
}
break;
case '2':cout<<"姓名:";cin>>v[i].name;break;
case '3':
{
cout<<"第几代:";
char s[50];
bool flag=true;
int gn;
while(flag)
{
cin>>s;
gn=isInt(s);
if(gn!=-1)
flag=false;
else
cout<<"输入错误!!!\n请请重新输入:";
}
v[i].generation=gn;
}break;
case '4':
{
cout<<"性别(M/F):";
char c;
bool flag=false;
do{
c=getch();
c=toupper(c);
cout<<c<<endl;
if(c!='M'&&c!='F')
cout<<"输入错误!!!\n请重新输入:";
else flag=true;
}while(!flag);
v[i].sex=c;
}break;
case '5':
{
cout<<"健在否(Y/N):";
char c;
bool flag=false;
do{
c=getch();
c=toupper(c);
cout<<c<<endl;
if(c!='Y'&&c!='N')
cout<<"输入错误!!!\n请重新输入:";
else flag=true;
}while(!flag);
v[i].alive=c;
}break;
case '6':
{
cout<<"婚否(Y/N):";
char c;
bool flag=false;
do{
c=getch();
c=toupper(c);
cout<<c<<endl;
if(c!='Y'&&c!='N')
cout<<"输入错误!!!\n请重新输入:";
else flag=true;
}while(!flag);
v[i].marriage=c;
}break;
case '7':cout<<"死亡日期:";cin>>v[i].deathdate;break;
case '8':cout<<"地址:";cin>>v[i].addr;break;
}
cout<<"修改后:"<<endl;
v[i].outputPerson();
}
else
cout<<"无此人信息"<<endl;
}
int Family::binSearchBirth( vector<Person>&v,int first, int last)
{
int mid; // index of the midpoint
int midvalue; // object that is assigned v[mid]
int origLast = last; // save original value of last
char choice;
cout<<"1:按出生年份查询"<<endl;
cout<<"2:按出生月份查询"<<endl;
cout<<"请选择:";
bool flag=true;
while(flag)
{
choice=getch();
cout<<choice<<endl;
if(choice=='1'||choice=='2')
flag=false;
else
{cout<<"输入错误!!!"<<endl;cout<<"请重新输入:";}
}
int target;
char s[50];
switch(choice)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -