#define N 50
typedef struct student
{
char name[N];
char studentID[N];
char brithday[N];
char tele[N];
}TreeData;
typedef struct treenode
{
TreeData * stuinfo;
struct treenode * leftchild;
struct treenode * rightchild;
}TREENODE;
#include
#include
#include
extern TREENODE * mycreatetree();
extern void myinsertnode(TREENODE *,TreeData *);
extern void mycopy(TreeData *,char *,char *,char *,char *);
extern void myeva(TreeData *);
extern void preorder(TREENODE *);
extern TREENODE *treeinit(TREENODE *);
extern TREENODE * mysearch(TREENODE *,char *);
extern int isfind(TREENODE *,char *);
extern int mycorrect(TREENODE *,char *);
extern TREENODE * findleaf(TREENODE *);
extern TREENODE * findparent(TREENODE *,TREENODE *);
extern void myremove(TREENODE *,char *);
void myeva(TreeData *dest)
{
printf("请输入你要添加的姓名:");
scanf("%s",dest->name);
while(1)
{
printf("请输入你要添加的学号(20209XXXX):");
scanf("%s",dest->studentID);
if((strlen(dest->studentID) == 9) &&(dest->studentID[0] == '2')&& (dest->studentID[1] =='0')&& (dest->studentID[2] == '2')&&(dest->studentID[3] == '0')&& (dest->studentID[4] =='9')&&((dest->studentID[5] <='9')&&(dest->studentID[5] >='0'))&&((dest->studentID[6] <='9')&&(dest->studentID[6] >='0'))&&((dest->studentID[7] <='9')&&(dest->studentID[7] >='0'))&&((dest->studentID[8] <='9')&&(dest->studentID[8] >= '0')))
{
break;
}
else
{
printf("输入有误!\n");
printf("您输入的数据必须为20209XXXX的格式(XXXX为可变数)\n");
}
}
while(1)
{
printf("请输入你要添加的生日(XXXX-XX-XX):");
scanf("%s",dest->brithday);
if((strlen(dest->brithday) == 10) &&((dest->brithday[0] <= '9')&&(dest->brithday[0]>= '0'))&& ((dest->brithday[1] <='9')&&(dest->brithday[1] >= '0'))&&((dest->brithday[2] <= '9')&&(dest->brithday[2]>= '0'))&& ((dest->brithday[3] <='9')&&(dest->brithday[3] >= '0'))&&(dest->brithday[4] == '-')&&((dest->brithday[5] <='1')&&(dest->brithday[5] >='0'))&&((dest->brithday[6] <='9')&&(dest->brithday[6] >='0'))&&(dest->brithday[7] =='-')&&((dest->brithday[8] <='3')&&(dest->brithday[8] >='0'))&&((dest->brithday[9] <='9')&&(dest->brithday[9] >= '0')))
{
if(dest->brithday[5] == '1')
{
if(dest->brithday[6] <= '2')
{
;
}
else
{
goto loop;
}
}
if(dest->brithday[8] == '3')
{
if(dest->brithday[9] <= '1')
{
;
}
else
{
goto loop;
}
}
break;
}
else
{
loop:
printf("输入有误!\n");
printf("您输入的数据必须为XXXX-XX-XX的格式\n");
}
}
while(1)
{
printf("请输入你要添加的电话:");
scanf("%s",dest->tele);
if((strlen(dest->tele) == 11)&&((dest->tele[0] <='9')&&(dest->tele[0] >='0'))&&((dest->tele[1] <='9')&&(dest->tele[1] >='0'))&&((dest->tele[2] <='9')&&(dest->tele[2] >='0'))&&((dest->tele[3] <='9')&&(dest->tele[3] >='0'))&&((dest->tele[4] <='9')&&(dest->tele[4] >='0'))&&((dest->tele[5] <='9')&&(dest->tele[5] >='0'))&&((dest->tele[6] <'9')&&(dest->tele[6] >='0'))&&((dest->tele[7] <='9')&&(dest->tele[7] >='0'))&&((dest->tele[8] <='9')&&(dest->tele[8] >='0'))&&((dest->tele[9] <='9')&&(dest->tele[9] >='0'))&&((dest->tele[10] <='9')&&(dest->tele[10] >= '0')))
{
break;
}
else
{
printf("输入有误!\n");
printf("您输入的数据必须为11位数字!\n");
}
}
}
void mycopy(TreeData *dest,char *name,char *studentID,char*brithday,char *tele)
{
strcpy(dest->name,name);
strcpy(dest->studentID,studentID);
strcpy(dest->brithday,brithday);
strcpy(dest->tele,tele);
}
TREENODE * findleaf(TREENODE *root)
{
static TREENODE * leaf = NULL;
if(root != NULL)
{
if((root->leftchild == NULL) && (root->rightchild ==NULL))
{
leaf = root;
return leaf;
}
findleaf(root->leftchild);
findleaf(root->rightchild);
}
return leaf;
}
TREENODE *findparent(TREENODE *root,TREENODE *leaf)
{
static TREENODE *parent = NULL;
if(root->leftchild != NULL)
{
if(root->leftchild == leaf)
{
parent = root;
}
else
{
findparent(root->leftchild,leaf);
}
}
if(root->rightchild != NULL)
{
if(root->rightchild == leaf)
{
parent = root;
}
else
{
findparent(root->rightchild,leaf);
}
}
return parent;
}
void myremove(TREENODE *root,char *info)
{
TREENODE *find = (TREENODE *)malloc(sizeof(TREENODE));
TREENODE *leaf = (TREENODE *)malloc(sizeof(TREENODE));
TREENODE *parent = (TREENODE *)malloc(sizeof(TREENODE));
int ret = isfind(root,info);
if(ret == 0)
{
printf("没有此人!\n");
}
else
{
find = mysearch(root,info);
printf("确定要删除此学生:\n");
//printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",find->stuinfo->name,find->stuinfo->studentID,find->stuinfo->brithday,find->stuinfo->tele);
printf("确定/取消?(Y/N):");
char yn;
setbuf(stdin,NULL);
scanf("%c",&yn);
if(yn == 'Y')
{
leaf = findleaf(root);
parent = findparent(root,leaf);
//parent = (TREENODE *)malloc(sizeof(TREENODE));
if(leaf == find)
{
free(find);
if(parent->leftchild == leaf)
{
parent->leftchild = NULL;
}
else
{
parent->rightchild = NULL;
}
find = NULL;
}
else
{
strcpy(find->stuinfo->name,leaf->stuinfo->name);
strcpy(find->stuinfo->studentID,leaf->stuinfo->studentID);
strcpy(find->stuinfo->brithday,leaf->stuinfo->brithday);
strcpy(find->stuinfo->tele,leaf->stuinfo->tele);
free(leaf);
if(parent->leftchild == leaf)
{
parent->leftchild = NULL;
}
else
{
parent->rightchild = NULL;
}
leaf = NULL;
}
}
}
}
TREENODE * mycreatetree()
{
TREENODE *root = NULL;
return root;
}
void myinsertnode(TREENODE *root,TreeData *data)
{
//root = (TREENODE *)malloc(sizeof(TREENODE));
if(strcmp(root->stuinfo->studentID,data->studentID) <0)
{
if(root->leftchild == NULL)
{
TREENODE *new = (TREENODE *)malloc(sizeof(TREENODE));
new->stuinfo = data;
new->leftchild = NULL;
new->rightchild = NULL;
root->leftchild = new;
}
else
{
myinsertnode(root->leftchild,data);
}
}
else
{
if(root->rightchild == NULL)
{
TREENODE *new = (TREENODE *)malloc(sizeof(TREENODE));
new->stuinfo = data;
new->leftchild = NULL;
new->rightchild = NULL;
root->rightchild = new;
}
else
{
myinsertnode(root->rightchild,data);
}
}
}
void preorder(TREENODE *root)
{
if(root != NULL)
{
printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
preorder(root->leftchild);
preorder(root->rightchild);
}
}
int isfind(TREENODE *root,char *info)
{
return ((root != NULL) &&((strcmp(root->stuinfo->name,info) == 0) ||(isfind(root->leftchild,info) ||isfind(root->rightchild,info))));
}
TREENODE * mysearch(TREENODE *root,char *info)
{
int ret = isfind(root,info);
static int flt = 0;
static TREENODE *find = NULL;
if((ret == 0) && (flt == 0))
{
printf("系统为找到此人!\n");
return;
}
else
{
flt = 1;
//TreeData * find = (TreeData *)malloc(sizeof(TreeData));
if(root != NULL)
{
if(strcmp(root->stuinfo->name,info) == 0)
{
//TREENODE * find = NULL;//(TreeData*)malloc(sizeof(TreeData));
//find = root->stuinfo;
find = root;
printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
return find;
}
mysearch(root->leftchild,info);
mysearch(root->rightchild,info);
}
}
return find;
}
int mycorrect(TREENODE *root,char *info)
{
int ret = isfind(root,info);
static int flt = 0;
if((ret == 0) && (flt == 0))
{
printf("系统为找到此人!\n");
printf("请按Enter键继续...");
getchar();
getchar();
return;
}
else
{
flt = 1;
if(root != NULL)
{
if(strcmp(root->stuinfo->name,info) == 0)
{
printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
int choose;
char yn;
char *buffer = (char *)malloc(sizeof(char));
printf("请输入你想修改的内容或按5取消修改:");
scanf("%d",&choose);
while((choose > 5) || (choose < 1))
{
printf("输入有误!\n");
printf("请重新输入要修改的内容或按5取消:");
scanf("%d",&choose);
}
switch(choose)
{
case 1:
{
printf("请输入要新的姓名:");
scanf("%s",buffer);
printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->name,buffer);
setbuf(stdin,NULL);
scanf("%c",&yn);
if(yn == 'Y')
{
strcpy(root->stuinfo->name,buffer);
printf("修改成功\n");
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
getchar();
}
else
{
printf("数据没有修改!\n");
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
getchar();
}
break;
}
case 2:
{
printf("请输入要新的学号:");
scanf("%s",buffer);
printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->studentID,buffer);
setbuf(stdin,NULL);
scanf("%c",&yn);
if(yn == 'Y')
{
strcpy(root->stuinfo->studentID,buffer);
printf("修改成功\n");
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
getchar();
}
else
{
printf("数据没有修改!\n");
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
getchar();
}
break;
}
case 3:
{
printf("请输入要新的生日:");
scanf("%s",buffer);
printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->brithday,buffer);
setbuf(stdin,NULL);
scanf("%c",&yn);
if(yn == 'Y')
{
strcpy(root->stuinfo->brithday,buffer);
printf("修改成功\n");
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
getchar();
}
else
{
printf("数据没有修改!\n");
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
getchar();
}
break;
}
case 4:
{
printf("请输入要新的电话:");
scanf("%s",buffer);
printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->tele,buffer);
setbuf(stdin,NULL);
scanf("%c",&yn);
if(yn == 'Y')
{
strcpy(root->stuinfo->tele,buffer);
printf("修改成功\n");
printf("请按Enter键继续...");
//setbuf(stdin,NULL);
getchar();
getchar();
}
else
{
printf("数据没有修改!\n");
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
getchar();
}
break;
}
case 5:
{
break;
}
}//switend
goto end;
} //if==end
mycorrect(root->leftchild,info);
mycorrect(root->rightchild,info);
} // if==NULLend
} //else end
end:
return;
}
void display(TREENODE *root)
{
int choose;
system("clear");
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t\t欢迎使用通讯录管理系统!\n");
printf("\n\n\n\n\n\n\n\n\n\n\n");
sleep(2);
system("clear");
while(1)
{
system("clear");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("请输入您的选择(1-6):");
scanf("%d",&choose);
if((choose > 6) || (choose < 1))
{
printf("输入有误,您的选择必须在1-5之间!\n");
printf("请重新输入:");
scanf("%d",&choose);
}
switch(choose)
{
case 1:
{
system("clear");
preorder(root);
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
break;
}
case 2:
{
system("clear");
char *info = (char *)malloc(sizeof(char));
printf("请输入要修改的人的姓名:");
scanf("%s",info);
mycorrect(root,info);
break;
}
case 3:
{
system("clear");
TreeData *info = (TreeData *)malloc(sizeof(TreeData));
myeva(info);
printf("您确定要添加学生:\n");
printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",info->name,info->studentID,info->brithday,info->tele);
printf("确定/取消(Y/N)?");
char yn;
setbuf(stdin,NULL);
scanf("%c",&yn);
if(yn == 'Y')
{
if(root == NULL)
{
root = (TREENODE *)malloc(sizeof(TREENODE));
root->stuinfo = info;
root->leftchild = NULL;
root->rightchild = NULL;
}
else
{
myinsertnode(root,info);
}
printf("添加成功!\n");
}
else
{
printf("取消添加!\n");
}
printf("请按Enter键继续...");
//setbuf(stdin,NULL);
getchar();
getchar();
break;
}
case 4:
{
system("clear");
char *info = (char *)malloc(sizeof(char));
printf("请输入要查找的人的姓名:");
scanf("%s",info);
mysearch(root,info);
printf("请按Enter键继续...");
setbuf(stdin,NULL);
getchar();
break;
}
case 5:
{
system("clear");
char *info = (char *)malloc(sizeof(char));
printf("请输入要删除的人的姓名:");
scanf("%s",info);
myremove(root,info);
printf("请按Enter键继续...");
getchar();
getchar();
break;
}
case 6:
{
system("clear");
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t\t\t欢迎再次使用!\n");
printf("\n\n\n\n\n\n\n\n\n\n\n");
sleep(2);
system("clear");
exit(0);
}
default:
{
;
}
}
}
}
TREENODE * treeinit(TREENODE *root)
{
TreeData * info[4];
int i;
for(i = 0; i < 4; i++)
{
if((info[i] = (TreeData *)malloc(sizeof(TreeData))) == NULL)
{
printf("分配空间失败!\n");
}
}
mycopy(info[0],"张一","202090116","1990-10-11","15298377000");
mycopy(info[1],"张二","202090114","1990-10-12","15298377111");
mycopy(info[2],"张三","202090115","1990-10-13","15298377222");
mycopy(info[3],"张四","202090118","1990-10-14","15298377333");
root = (TREENODE *)malloc(sizeof(TREENODE));
root->stuinfo = info[0];
root->leftchild = NULL;
root->rightchild = NULL;
myinsertnode(root,info[1]);
myinsertnode(root,info[2]);
myinsertnode(root,info[3]);
return root;
}
int main()
{
TREENODE * root = NULL;
root = mycreatetree();
root = treeinit(root);
display(root);
return 0;
}