文章目录
-
-
- head.h
- family.cpp
- main.cpp
head.h
#include
#include
#include
#include
#ifndef FAMILY_H_INCLUDED
#define MAXSIZE 100
#define FAMILY_H_INCLUDED
typedef struct Infomation{
char name[20];
char birth[20];
int wedding;
char add[20];
int health;
char death_date[20];
}Info;
typedef struct node
{
Info person;
struct node *lchild,*rchild;
}Bnode,*tree;
typedef struct mode
{
tree queue[MAXSIZE];
int front,rear;
}Queue;
void init(Queue Q);
void push(Queue *Q,tree e);
void pop(Queue *Q,tree *e);
int empty(Queue Q);
int full(Queue Q);
void newleft(tree p,Info L);
void newright(tree p,Info L);
tree creat();
void output(tree p);
void level(tree p);
void shownumlevel(tree p,int n);
tree parent(tree bt,tree p);
tree searchname(tree bt,char na[]);
void searchchild(tree p,tree child[]);
tree searchbirth(tree bt,char bir[]);
int samefather(tree bt,tree p,tree q);
void relationship(tree bt);
void add(tree *bt);
void deletename(tree *bt);
void showfamily(tree bt);
void update(tree *bt);
void transport(tree bt);
void menu();
#endif
family.cpp
#include
#include
#include
#include
#define MAXSIZE 100
#include "head.h"
using namespace std;
template<class T>
T fun(T x)
{
if(x!=2)
throw x;
else
return x;
}
void init(Queue Q)
{
Q.front=Q.rear=0;
}
void push(Queue *Q,tree e)
{
Q->rear=(Q->rear+1)%MAXSIZE;
Q->queue[Q->front]=e;
}
void pop(Queue *Q,tree *e)
{
Q->front=(Q->front+1)%MAXSIZE;
*e=Q->queue[Q->front];
}
int empty(Queue Q)
{
if(Q.front==Q.rear)
return 1;
else return 0;
}
int full(Queue Q)
{
if((Q.rear+1)%MAXSIZE==Q.front)
return 1;
else return 0;
}
void newleft(tree p,Info L)
{
tree q;
q=(tree)malloc(sizeof(Bnode));
q->person=L;
q->lchild=q->rchild=NULL;
p->lchild=q;
}
void newright(tree p,Info L)
{
tree q;
q=(tree)malloc(sizeof(Bnode));
q->person=L;
q->lchild=q->rchild=NULL;
p->rchild=q;
}
tree creat()
{
FILE *fp;
int i;
Info human[11];
if ((fp=fopen("E:/family.txt","r+"))==NULL)
{
printf("不能打开家谱文件\n");
}
for(i=0;i<11;i++)
{
fscanf(fp," %s %s %d %s %d",human[i].name,human[i].birth,&human[i].wedding,human[i].add,&human[i].health);
fgets(human[i].death_date,20,fp);
printf("%-7s%-15s%-7d%-12s%-10d%-20s\n",human[i].name,human[i].birth,human[i].wedding,human[i].add,human[i].health,human[i].death_date);
}
fclose(fp);
tree bt;
bt=(tree)malloc(sizeof(Bnode));
bt->person=human[0];
bt->lchild=bt->rchild=NULL;
newleft(bt,human[1]);
newright(bt,human[2]);
newleft(bt->lchild,human[3]);
newright(bt->lchild,human[4]);
newleft(bt->rchild,human[5]);
newright(bt->rchild,human[6]);
newleft(bt->lchild->lchild,human[7]);
newright(bt->lchild->lchild,human[8]);
newleft(bt->rchild->rchild,human[9]);
newright(bt->rchild->rchild->lchild,human[10]);
return bt;
}
void output(tree p)
{
printf("%-7s%-15s%-7d%-12s%-10d%-20s\n",p->person.name,p->person.birth,p->person.wedding,p->person.add,p->person.health,p->person.death_date);
}
void level(tree p)
{
tree q[20],s;
int front=0,rear=0;
printf(" 姓名 出生日期 婚否 地址 健在否 死亡日期\n");
if(p)
{
rear++;
q[rear]=p;
while(front!=rear)
{
front=(front+1)%20;
s=q[front];
output(s);
if((rear+1)%20!=front&&s->lchild!=NULL)
{
rear=(rear+1)%20;
q[rear]=s->lchild;
}
if((rear+1)%20!=front&&s->rchild!=NULL)
{
rear=(rear+1)%20;
q[rear]=s->rchild;
}
}
}
}
void shownumlevel(tree p,int n)
{
static int d=0;
d++;
if(p)
{
if(d==n)
{
output(p);
}
shownumlevel(p->lchild,n);
shownumlevel(p->rchild,n);
}
d--;
}
tree parent(tree bt,tree p)
{
tree lresult,rresult;
if(!bt||bt==p)
return NULL;
if(bt->lchild==p||bt->rchild==p)
return bt;
else
{
lresult=parent(bt->lchild,p);
rresult=parent(bt->rchild,p);
return lresult?lresult:(rresult?rresult:NULL);
}
}
tree searchname(tree bt,char na[])
{
tree lresult,rresult;
if(!bt)
{
return NULL;
}
if(strcmp(bt->person.name,na)==0)
return bt;
else
{
lresult=searchname(bt->lchild,na);
rresult=searchname(bt->rchild,na);
return lresult?lresult:(rresult?rresult:NULL);
}
}
void searchchild(tree p,tree child[])
{
child[0]=child[1]=NULL;
if(p->lchild!=NULL)
child[0]=p->lchild;
if(p->rchild!=NULL)
child[1]=p->rchild;
}
tree searchbirth(tree bt,char bir[])
{
tree lresult,rresult;
if(!bt)
{
return NULL;
}
if(strcmp(bt->person.birth,bir)==0)
return bt;
else
{
lresult=searchbirth(bt->lchild,bir);
rresult=searchbirth(bt->rchild,bir);
return lresult?lresult:(rresult?rresult:NULL);
}
}
int samefather(tree bt,tree p,tree q)
{
tree f1,f2;
f1=parent(bt,p);
f2=parent(bt,q);
if(f1==f2)
{
return 1;
}
else return 0;
}
void relationship(tree bt)
{
char name1[20],name2[20];
tree s1,s2 ,f1,f2;
printf("请输入第一个人姓名:\n");
scanf("%s",name1);
printf("请输入第二个人姓名:\n");
scanf("%s",name2);
s1=searchname(bt,name1);
s2=searchname(bt,name2);
f1=parent(bt,s1);
f2=parent(bt,s2);
if(samefather(bt,s1,s2)==1)
printf("他们是亲兄弟\n");
else if(samefather(bt,f1,f2)==1)
printf("他们是堂兄弟\n");
else if(s1->lchild==s2||s1->rchild==s2)
printf("%s是%s的孩子\n",s2->person.name,s1->person.name);
else if(s2->lchild==s1||s2->rchild==s1)
printf("%s是%s的孩子\n",s1->person.name,s2->person.name);
else
printf("两个人关系太远了\n");
}
void add(tree *bt)
{
char na[20];
tree p;
Info newchild;
printf("请输入拥有新孩子的父母的名字:\n");
scanf("%s",na);
p=searchname(*bt,na);
if(p->lchild==NULL)
{
printf("请输入新孩子的信息:\n");
scanf("%s %s %d %s %d",newchild.name,newchild.birth,&newchild.wedding,newchild.add,&newchild.health);
gets(newchild.death_date);
newleft(p,newchild);
}
else if(p->rchild==NULL)
{
printf("请输入新孩子的信息:\n");
scanf("%s %s %d %s %d",newchild.name,newchild.birth,&newchild.wedding,newchild.add,&newchild.health);
gets(newchild.death_date);
newright(p,newchild);
}
else
{
printf("添加失败!\n");
}
}
void deletename(tree *bt)
{
char na[20];
tree p,f;
printf("请输入想删除的人的姓名,删除之后他的后代也将一并删除!\n");
scanf("%s",na);
p=searchname(*bt,na);
f=parent(*bt,p);
if(f!=NULL)
{
if(f->lchild==p)
{
f->lchild=NULL;
free(p);
}
if(f->rchild==p)
{
f->rchild=NULL;
free(p);
}
}
else
{
*bt=NULL;
}
}
void showfamily(tree bt)
{
if(bt!=NULL)
{
printf("%s",bt->person.name);
if(bt->lchild||bt->rchild)
{
printf("(");
showfamily(bt->lchild);
if(bt->rchild)
{
printf(",");
}
showfamily(bt->rchild);
printf(")");
}
}
}
void update(tree *bt)
{
char na[20];
tree p;
printf("请输入你想修改的孩子的姓名:\n");
scanf("%s",na);
p=searchname(*bt,na);
if(p==NULL)
{
printf("修改失败\n");
}
else
{
printf("请输入修改后的信息: \n");
scanf("%s%s%d%s%d",p->person.name,p->person.birth,&p->person.wedding,p->person.add,&p->person.health);
gets(p->person.death_date);
}
}
char birthday[20][20];
int x=0;
void transport(tree bt)
{
if(bt)
{
strcpy(birthday[x++],bt->person.birth);
transport(bt->lchild);
transport(bt->rchild);
}
}
void menu()
{
printf("\n1.从文件读取家族信息并显示\n");
printf("2.保存家族信息并存盘\n");
printf("3.显示家谱图\n");
printf("4.层次遍历家谱图\n");
printf("5.显示第n代人的所有信息\n");
printf("6.输入两人姓名,确定其关系\n");
printf("7.给某成员添加孩子\n");
printf("8.删除某成员(若其还有后代,则一并删除)\n");
printf("9.修改某成员信息\n");
printf("10.退出系统\n");
}
main.cpp
#include
#include
#include
#include
#define MAXSIZE 100
#include "head.h"
#include "family.cpp"
using namespace std;
int main()
{
tree bt;
int i,n;
FILE *fp;
Info human[11];
printf("欢迎来到家谱管理系统\n");
menu();
while(scanf("%d",&i)>0)
{
switch(i)
{
case 1:
{
bt=creat();
transport(bt);
menu();
printf("\n请输入你的选择:\n");
break;
}
case 2:
{
if ((fp=fopen("E:/family.txt","r+"))==NULL)
{
printf("不能打开家谱文件\n");
}
for(i=0;i<11;i++)
{
fscanf(fp," %s,%s,%d,%s,%d",human[i].name,human[i].birth,&human[i].wedding,human[i].add,&human[i].health);
fgets(human[i].death_date,20,fp);
}
fclose(fp);
menu();
printf("\n请输入你的选择:\n");
break;
}
case 3:
printf("(");
showfamily(bt);
printf(")\n");
menu();
printf("\n请输入你的选择:\n");
break;
case 4:
level(bt);
menu();
printf("\n请输入你的选择:\n");
break;
case 5:
printf("你想找第几代的信息?\n");
scanf("%d",&n);
shownumlevel(bt,n);
menu();
printf("\n请输入你的选择:\n");
break;
case 6:
relationship(bt);
menu();
printf("\n请输入你的选择:\n");
break;
case 7:
add(&bt);
printf("(");
showfamily(bt);
printf(")\n");
level(bt);
menu();
printf("\n请输入你的选择:\n");
break;
case 8:
deletename(&bt);
printf("(");
showfamily(bt);
printf(")\n");
level(bt);
menu();
printf("\n 请输入你的选择:\n");
break;
case 9:
update(&bt);
showfamily(bt);
printf(")\n");
level(bt);
menu();
printf("\n请输入你的选择:\n");
break;
case 10:
printf("感谢使用!\n");
return 0;
default:
printf("输入错误,请重新输入:\n");
menu();
printf("\n请输入你的选择:\n");
break;
}
}
return 0;
}