#include "stdio.h"
#include <string.h>
#include "stdlib.h"//通讯录结构体类型定义
typedef struct node
{
int age;
char nam[10] ,dep[10],tel[12];
struct node *lchild, *rchild;
}BTreeNode;
//按照年龄生成二叉排序树
void InsertBST (BTreeNode **T,FILE *fp)
{
BTreeNode *p=*T,*f,*q;
q=(BTreeNode *)malloc(sizeof (BTreeNode));
fscanf(fp, "%s%s%d%s\n", q->nam, q->dep,&q->age, q->tel);
q->lchild-NULL;
q->rchild=NULL;
if(*T==NULL)
{

*T=q;//记录根
return;
}
while(p!=NULL)
{
f=p;
p=(p->age>q->age)?p->lchild:p->rchild;
}
if(f->age>q->age)
f->lchild=q;
else
f->rchild=q;
}
//读取文件内容
void ReadFile (BTreeNode **T)
{
FILE *fp;
if((fp=fopen("11.txt","r"))==NULL)
{
printf(" 读文件失败!\n");
}
while(!feof(fp))
InsertBST(T,fp);
fclose(fp);
}
//先序遍历显尔所有教师的信息绘
void PreOrder(BTreeNode *T)
{
if(T!=NULL)
{

printf ("%-8s %-10s %-4d %-12s\n",T->nam,T->dep, T->age, T->tel);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//中序遍历按年龄查询教师信息
void InOrder (BTreeNode *T)
{

if(T!=NULL)
{

InOrder(T->lchild);
if(T->age<=35)
{
printf ("%-8s %-10s %-4d %-12s\n", T->nam, T->dep,T->age,T->tel);InOrder(T->rchild);
}
}
}
//后序遍历按姓名查询教师信息
void PostOrder1(BTreeNode *T ,char *p)
{
if(T!=NULL)
{
PostOrder1(T->lchild, p);
PostOrder1(T->rchild,p);
if(strcmp(T->nam,p)==0)printf("%-8s %-10s g-4d %-12s\n", T->nam, T->dep, T->age, T->tel);
}
}
//后序遍历按学院查询教师信息
void PostOrder2(BTreeNode *T,char *p)
{

if(T!=NULL)
{
PostOrder2 (T->lchild, p);
PostOrder2(T->rchild,p);
if(strcmp(T->dep,p)==0)
printf ("%-8s %-10s %-4d %-12s\n",T->nam,T->dep,T->age, T->tel);
}
}
main()
{

int select,f=1;
char nam[10],dep[10];BTreeNode *T=NULL;
printf("通讯录查询系统\n");
printf ( "----------------------\n");
printf("1,显示所有教师2.按姓名查询3.按学院查询4.输出青年教师5.退出系统\n");
printf ("----------------\n");
ReadFile(&T);//读取文件内容
while(f==1)
{

printf("请输人选项:");
scanf("%d", &select) ;
switch(select)
{

case 1: PreOrder (T);
printf("----------------------\n");break;

case 2:printf("请输入要查找的姓名:");
scanf("%s",nam) ;PostOrder1 (T,nam);
printf("--------------------\n");break;

case 3:printf("请输入要查找的学院;");
scanf ("%s",dep) ;PostOrder2 (T,dep);
printf ("---------------------\n");break;

case 4:printf("青年教帅通讯录:n");InOrder(T);
printf("----------------------\n") ;break;

case 5:f=0;
}
}
}

发表评论

您的电子邮箱地址不会被公开。