#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <math.h>
#include <process.h>
#include <windows.h>
#define MAXNUM 20
char family[100][20]; /* 全局变量*/
struct TriTree /* 树的三叉链表存储结构*/
{
char data[MAXNUM];
struct Node *parent;
struct Node *lchild;
struct Node *rchild;
};
struct Node /* 队列的结点*/
{
struct TriTree *info;
struct Node *next;
};
struct LinkQueue /* 链接队列类型*/
{
struct Node *front;
struct Node *rear;
};
//链队的基本操作
//建立一个空队列
struct LinkQueue *LQueueCreateEmpty( )
{
struct LinkQueue *plqu;
plqu=(struct LinkQueue *)malloc(sizeof(struct LinkQueue));
if (plqu!=NULL)
plqu->front=plqu->rear=NULL;
else
return NULL;
return plqu;
}
//进队列
void LQueueEnQueue(struct LinkQueue *plqu,struct TriTree *x)
{
struct Node *p;
p=(struct Node *)malloc(sizeof(struct Node));
p->info=x;
p->next=NULL;
if(plqu->front==NULL)/* 原来为空队*/
plqu->front=p;
else
plqu->rear->next=p;
plqu->rear=p;
}
//出队列
void LQueueDeQueue(struct LinkQueue *plqu,struct TriTree *x)
{
struct Node *p;
if(plqu->front==NULL)
return;
else
{
p=plqu->front;
x=p->info;
plqu->front=plqu->front->next;
free(p);
return;
}
}
//求队头元素
struct TriTree *LQueueGetFront(struct LinkQueue *plqu)
{
return(plqu->front->info);
}
/* 建立家族关系树*/
struct TriTree *TriTreeCreate()
{
int i=0,flag=0,start=0;
char str[MAXNUM];
struct TriTree *t,*x=NULL,*tree,*root=NULL;
struct LinkQueue *q; /* 建立一个空队列*/
q=LQueueCreateEmpty();
strcpy(str,family[i]);
i++; /* family数组下标*/
while(str[0]!='#')
{
while(str[0]!='@')
{
if(root==NULL) /* 空树*/
{
root=(struct TriTree *)malloc(sizeof(struct TriTree));
strcpy(root->data,str);
root->parent=NULL;
root->lchild=NULL;
root->rchild=NULL;
LQueueEnQueue(q,root); /* 将root入队*/
tree=root;
}
else /* 不为空树*/
{
t=(struct TriTree *)malloc(sizeof(struct TriTree));
strcpy(t->data,str);
t->lchild=NULL;
t->rchild=NULL;
t->parent=LQueueGetFront(q); /* 当前结点的双亲为队头元素*/
LQueueEnQueue(q,t); /* 入队*/
if(flag==0) /* flag为0,当前结点没有左孩子*/
root->lchild=t;
else /* flag为1,当前结点已有左孩子*/
root->rchild=t;
root=t;
}
flag=1; /* 标记当前结点已有左孩子*/
strcpy(str,family[i]);
i++;
}
if(start!=0) /* 标记不是第一次出现“@”*/
{
LQueueDeQueue(q,x); /* 出队*/
if(q->front!=NULL)
root=LQueueGetFront(q); /* 队头元素*/
}
start=1; /* 标记已出现过“@”*/
flag=0; /* “@”后面的结点一定为左孩子*/
strcpy(str,family[i]);
i++;
}
return tree;
}
//建立家族关系并存入文件
struct TriTree *Create()
{
struct TriTree *t;
char* str0="李一一"; char* str1= "@"; char* str2="李二一"; char* str3="李二二";
char* str4="李二三"; char* str5="@"; char* str6="李三一"; char* str7="李三二";
char* str8="李三三"; char* str9="@"; char* str10="李三四"; char* str11="李三五";
char* str12="@"; char* str13="李三六"; char* str14="李三七"; char* str15="@";
char* str16="李四一"; char* str17="李四二"; char* str18="@"; char* str19="李四三";
char* str20="@"; char* str21="李四四"; char* str22="李四五"; char* str23="@" ;
char* str24="李四六" ; char* str25="李四七"; char* str26="@"; char* str27="李四八" ;
char* str28="李四九"; char* str29="@"; char* str30="李四一零"; char* str31="李四一一";
char* str32="@"; char* str33="李四一二"; char* str34="@"; char* str35="@";
char* str36="@"; char* str37="李五一"; char* str38="李五二"; char* str39="@";
char* str40="@"; char* str41="李五三"; char* str42="@"; char* str43="李五四";
char* str44="李五五"; char* str45="@"; char* str46="@"; char* str47="@";
char* str48="@"; char* str49="李五六"; char* str50="@"; char* str51="李五七";
char* str52="@"; char* str53="#";
FILE *fp;
fp=fopen("family.txt","w");
fputs(str0,fp); fputc('\n',fp); strcpy(family[0],str0); fputs(str1,fp); fputc('\n',fp); strcpy(family[1],str1);
fputs(str2,fp); fputc('\n',fp); strcpy(family[2],str2); fputs(str3,fp); fputc('\n',fp); strcpy(family[3],str3);
fputs(str4,fp); fputc('\n',fp); strcpy(family[4],str4); fputs(str5,fp); fputc('\n',fp); strcpy(family[5],str5);
fputs(str6,fp); fputc('\n',fp); strcpy(family[6],str6); fputs(str7,fp); fputc('\n',fp); strcpy(family[7],str7);
fputs(str8,fp); fputc('\n',fp); strcpy(family[8],str8); fputs(str9,fp); fputc('\n',fp); strcpy(family[9],str9);
fputs(str10,fp); fputc('\n',fp); strcpy(family[10],str10); fputs(str11,fp); fputc('\n',fp); strcpy(family[11],str11);
fputs(str12,fp); fputc('\n',fp); strcpy(family[12],str12); fputs(str13,fp); fputc('\n',fp); strcpy(family[13],str13);
fputs(str14,fp); fputc('\n',fp); strcpy(family[14],str14); fputs(str15,fp); fputc('\n',fp); strcpy(family[15],str15);
fputs(str16,fp); fputc('\n',fp); strcpy(family[16],str16); fputs(str17,fp); fputc('\n',fp); strcpy(family[17],str17);
fputs(str18,fp); fputc('\n',fp); strcpy(family[18],str18); fputs(str19,fp); fputc('\n',fp); strcpy(family[19],str19);
fputs(str20,fp); fputc('\n',fp); strcpy(family[20],str20); fputs(str21,fp); fputc('\n',fp); strcpy(family[21],str21);
fputs(str22,fp); fputc('\n',fp); strcpy(family[22],str22); fputs(str23,fp); fputc('\n',fp); strcpy(family[23],str23);
fputs(str24,fp); fputc('\n',fp); strcpy(family[24],str24); fputs(str25,fp); fputc('\n',fp); strcpy(family[25],str25);
fputs(str26,fp); fputc('\n',fp); strcpy(family[26],str26); fputs(str27,fp); fputc('\n',fp); strcpy(family[27],str27);
fputs(str28,fp); fputc('\n',fp); strcpy(family[28],str28); fputs(str29,fp); fputc('\n',fp); strcpy(family[29],str29);
fputs(str30,fp); fputc('\n',fp); strcpy(family[30],str30); fputs(str31,fp); fputc('\n',fp); strcpy(family[31],str31);
fputs(str32,fp); fputc('\n',fp); strcpy(family[32],str32); fputs(str33,fp); fputc('\n',fp); strcpy(family[33],str33);
fputs(str34,fp); fputc('\n',fp); strcpy(family[34],str34); fputs(str35,fp); fputc('\n',fp); strcpy(family[35],str35);
fputs(str36,fp); fputc('\n',fp); strcpy(family[36],str36); fputs(str37,fp); fputc('\n',fp); strcpy(family[37],str37);
fputs(str38,fp); fputc('\n',fp); strcpy(family[38],str38); fputs(str39,fp); fputc('\n',fp); strcpy(family[39],str39);
fputs(str40,fp); fputc('\n',fp); strcpy(family[40],str40); fputs(str41,fp); fputc('\n',fp); strcpy(family[41],str41);
fputs(str42,fp); fputc('\n',fp); strcpy(family[42],str42); fputs(str43,fp); fputc('\n',fp); strcpy(family[43],str43);
fputs(str44,fp); fputc('\n',fp); strcpy(family[44],str44); fputs(str45,fp); fputc('\n',fp); strcpy(family[45],str45);
fputs(str46,fp); fputc('\n',fp); strcpy(family[46],str46); fputs(str47,fp); fputc('\n',fp); strcpy(family[47],str47);
fputs(str48,fp); fputc('\n',fp); strcpy(family[48],str48); fputs(str49,fp); fputc('\n',fp); strcpy(family[49],str49);
fputs(str50,fp); fputc('\n',fp); strcpy(family[50],str50); fputs(str51,fp); fputc('\n',fp); strcpy(family[51],str51);
fputs(str52,fp); fputc('\n',fp); strcpy(family[52],str52); fputs(str53,fp); fputc('\n',fp); strcpy(family[53],str53);
fclose(fp);
t=TriTreeCreate(); /* 创建树*/
return t;
}
//家族成员查找
struct TriTree *Search(struct TriTree *t,char name[])
{
struct TriTree *temp;
if(t==NULL)
return NULL;
else if(strcmp(t->data,name)==0)
return t;
else
{
temp=Search(t->lchild,name);
if(temp)
return Search(t->lchild,name);
else
return Search(t->rchild,name);
}
}
//家族成员添加
void AddSon(struct