#用二叉树来实现学生健康情况管理系统.docx

上传人:b****6 文档编号:7835972 上传时间:2023-01-26 格式:DOCX 页数:17 大小:230.04KB
下载 相关 举报
#用二叉树来实现学生健康情况管理系统.docx_第1页
第1页 / 共17页
#用二叉树来实现学生健康情况管理系统.docx_第2页
第2页 / 共17页
#用二叉树来实现学生健康情况管理系统.docx_第3页
第3页 / 共17页
#用二叉树来实现学生健康情况管理系统.docx_第4页
第4页 / 共17页
#用二叉树来实现学生健康情况管理系统.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

#用二叉树来实现学生健康情况管理系统.docx

《#用二叉树来实现学生健康情况管理系统.docx》由会员分享,可在线阅读,更多相关《#用二叉树来实现学生健康情况管理系统.docx(17页珍藏版)》请在冰豆网上搜索。

#用二叉树来实现学生健康情况管理系统.docx

#用二叉树来实现学生健康情况管理系统

【综设实验题目】

实现学生健康情况管理的几个操作功能(新建、插入、删除、从文件读取、写入文件和查询、屏幕输出等功能)。

健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。

实验内容

1.利用二叉树来实现

2.系统的菜单功能项如下:

1------新建学生健康表

2------向学生健康表插入学生信息

3------在健康表删除学生信息

4------从文件中读取健康表信息

5------向文件写入学生健康表信息

6------在健康表中查询学生信息(按学生学号来进行查找)

7------在屏幕中输出全部学生信息

8-----退出

【中文摘要】这次实验主要用二叉树来实现简单的学生健康管理系统,为了方便查找,进一步使用排序二叉树来实现。

系统的功能包括:

向学生健康表插入学生信息,在健康表删除学生信息,从文件中读取健康表信息,向文件写入学生健康表信息,在健康表中查询学生信息(按学生学号来进行查找),在屏幕中输出全部学生信息等。

健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。

【关键词】排序二叉树学生健康管理系统学生信息

【前言】

本次实验是为了进一步熟悉和掌握VC环境下的编译、调试和执行的方法及步骤,熟悉二叉树存储的实现方式及其使用。

【实验设计】

以排序二叉树为储存机制,可以方便的实现插入或删除学生信息。

每个学生的信息储存在一个结构体Sstudent中,并且这个结构体带有输出学生信息的函数ouput()。

然后以这个结构体作为二叉树节点的数据类型,这样就实现了学生信息的储存。

在创建二叉树对象时将已存储在文件中的学生信息写入二叉树,在析构函数里实现将学生信息写入文件。

【实验实现】

软件平台:

VC++6.0

硬件平台:

32位机器

主要功能模块分析:

1、储存一个学生的信息:

/*******************************************************************

Sstudent.h文件

*******************************************************************/

#ifndef_Sstudent_h_

#define_Sstudent_h_

#include

usingnamespacestd;

structbirthday//出生日期

{

unsignedshortday;

unsignedshortmonth;

unsignedshortyear;

};

structSstudent//一个学生的基本信息

{

charnumber[12];//学号

charname[12];//名字

structbirthdaybd;//出生日期

chargender[4];//性别

charhealthcase[10];//健康情况

Sstudent(){}

voidinput();//输入学生的基本信息

voidoutput();//输出学生的基本信息

voidoperator=(Sstudents);

booloperator<(Sstudent&s);

booloperator==(Sstudents);

booloperator>(Sstudent&s);

};

voidSstudent:

:

input()//输入一个学生的信息

{

cout<<"请输入学生信息:

"<

cout<<"请输入学生的学号:

";

cin>>number;

cout<<"请输入学生的名字:

";

cin>>name;

cout<<"请输入学生的性别:

";

cin>>gender;

cout<<"请输入学生生日的日期(年、月、日):

";

cin>>bd.year>>bd.month>>bd.day;

cout<<"请输入学生的健康情况(良好或差):

";

cin>>healthcase;

cout<

}

voidSstudent:

:

output()//输出一个学生的信息

{

cout<<"学号:

"<

<<"姓名:

"<

<<"性别:

"<

<<"生日:

"<

<<"健康情况:

"<

}

voidSstudent:

:

operator=(Sstudents)

{

strcpy(number,s.number);

strcpy(name,s.name);

strcpy(gender,s.gender);

bd.year=s.bd.year;

bd.month=s.bd.month;

bd.day=s.bd.day;

strcpy(healthcase,s.healthcase);

}

boolSstudent:

:

operator<(Sstudent&s)

{

if(strcmp(number,s.number)==-1)//若number小于s.number

returntrue;

else

returnfalse;

}

boolSstudent:

:

operator==(Sstudents)

{

if(!

strcmp(name,s.name))//若name等于s.number

if(!

strcmp(number,s.number))//若num等于s.number

returntrue;

returnfalse;

}

boolSstudent:

:

operator>(Sstudent&s)

{

if(strcmp(number,s.number)==1)//若number大于s.number

returntrue;

else

returnfalse;

}

#endif

2、将文件中的学生健康信息写进排序二叉树

BSTree:

:

BSTree()

{

root=NULL;

BTreeNode*b=NULL;

Sstudents;

fstreamfile;

file.open("student.dat",ios:

:

in|ios:

:

binary);

if(!

file)

{

cerr<<"student.datcan'topen!

"<

abort();

}

while

(1)

{

file.read((char*)&s,sizeof(s));//从文件中读入一个学生的信息

if(!

file)

break;

b=newBTreeNode(s,NULL,NULL);

inst(b->data);//调用插入函数建二叉树

}

file.close();

}

3、将排序二叉树中的学生信息写进文件

思想:

在退出程序调用析构函数时,通过前序遍历,将访问到的节点信息依次写进文件。

BSTree:

:

~BSTree()

{

fstreamfile;

file.open("student.dat",ios:

:

out|ios:

:

binary);

if(!

file)

{

cerr<<"student.datcan'topen!

"<

abort();

}

BTreeNode*p=root;

preorder2(file,write1);//通过前序遍历,调用写函数write1

file.close();

}

VoidBSTree:

:

preorder2(fstream&file,voidwrite1(fstream&file,BTreeNode*p))

{

BTreeNode*stack[10],*p;

inttop=0;

p=root;

while(top>=0)

{

while(p!

=NULL)

{

write1(file,p);

stack[top++]=p;

p=p->lchild;

}

top--;

p=stack[top];

if(p!

=NULL)

p=p->rchild;

}

cout<

}

voidwrite1(fstream&outfile,BTreeNode*p)//将节点p写进文件流

{

if(p==NULL)

cout<<"p==NULL"<

else

outfile.write((char*)&(p->data),sizeof(p->data));

}

4、向学生健康表插入学生信息

//具有插入功能的查找(递归)

voidBSTree:

:

inst(BTreeNode*p,BTreeNode*&bst)

{

if(bst==NULL)

bst=p;

elseif(p->datadata)

inst(p,bst->lchild);

else

inst(p,bst->rchild);

}

/*

功能:

在当前的排序二叉树上查找关键字等于给定值el的结点,当查找成功时输出相应的信息,

否则建立一个新结点并插入在当前排序二叉树的适当位置中。

*/

voidBSTree:

:

inst(Sstudentel)

{

BTreeNode*p;

p=sear(el.number);

if(p!

=NULL)

cout<<"已找到"<

else

{

p=newBTreeNode(el,NULL,NULL);

inst(p,root);

}

}

5、删除学生信息

/*

思想:

首先查找该元素所在的节点p,同时记录父节点;

一、若p为空,则发出错误信号;

二、若p不为空:

分三种情况讨论:

  

若*p结点为叶子结点,即左子树和右子树均为空树。

由于删去叶子结点不破坏整棵树的结构,则只需修改其双亲结点的指针即可。

若*p结点只有左子树或右子树,此时只要令左子树或右子树直接成为其双亲结点*f的左子树(当*p是左子树)或右子树(当*p是右子树)即可,

作此修改也不破坏二叉排序树的特性。

若*p结点的左子树和右子树均不空。

在删去*p之后,为保持其它元素之间的相对位置不变,可按中序遍历保持有序进行调整,做法:

用p的右子树的最左端的节点tmp的数据域刷新p的数据域,然后递归调用删除tmp.

*/

BTreeNode*BSTree:

:

deleteNode(BTreeNode*&p,charnum[])

{

BTreeNode*pfather=root,*qfather=NULL,*q=NULL;;

intk;

//搜索该元素所在的节点,同时记录父节点

while(p!

=NULL)

{

k=strcmp(num,p->data.number);

if(k==-1)

{

pfather=p;

p=p->lchild;

}

elseif(k==1)

{

pfather=p;

p=p->rchild;

}

else

break;

}

if(p==NULL)

{

cout<<"找不到该同学信息,删除失败!

"<

returnNULL;

}

elseif(p->lchild!

=NULL&&p->rchild!

=NULL)//结点左右儿子都存在

{

BTreeNode*tmp=p->rchild;

while(tmp->lchild)

{

tmp=tmp->lchild;

}

p->data=tmp->data;

p->rchild=deleteNode(p->rchild,tmp->data.number);

}

else

{//只有一个儿子或没有儿子的情况

if(p->lchild==NULL)

{

if(p==pfather->lchild)//p是左子节点

pfather->lchild=p->rchild;

else

pfather->rchild=p->rchild;

}

elseif(p->rchild==NULL)

{

if(p==pfather->lchild)//p是左子节点

pfather->lchild=p->lchild;

else

pfather->rchild=p->lchild;

}

}

p->data.output();

returnp;

}

//实例函数

BTreeNode*BSTree:

:

deleteNode(charnum[])

{

BTreeNode*p=root;

returndeleteNode(p,num);

}

6、在屏幕中输出全部学生信息

//采用中序遍历的思想输出节点信息,则在屏幕上根据学生学号的升序输出。

voidBTree:

:

prnt(BTreeNode*p)//递归函数

{

if(p!

=NULL)

{

prnt(p->lchild);

p->data.output();

prnt(p->rchild);

}

}

//实例函数

voidBTree:

:

prnt()

{

prnt(root);

}

7、用户界面

主函数文件:

intmain()

{

BSTreet;//新建一个二叉树:

t

intchoice=10;//将choice初始化,使其不为0~4

Sstudents;

BTreeNode*p=NULL;

charnum[12];

while(choice)

{

cout<<"请输入您要进行的操作:

"<

cout<<"1------向学生健康表插入学生信息"<

<<"2------在健康表删除学生信息"<

<<"3------在健康表中查询学生信息(按学生学号来进行查找)"<

<<"4------在屏幕中输出全部学生信息"<

<<"0------退出"<

cin>>choice;

switch(choice)

{

case1:

s.input();//输入学生信息

t.inst(s);

break;

case2:

cout<<"请输入要删除学生的学号:

";

cin>>num;

p=t.deleteNode(num);

if(p!

=NULL)

{

cout<<"删除成功!

删除学生的信息为:

"<

p->getdata().output();

}

break;

case3:

cout<<"请输入要查询学生的学号:

";

cin>>num;

p=t.sear(num);

if(p!

=NULL)

p->getdata().output();

else

cout<<"找不到该学生信息!

"<

break;

case4:

t.prnt();

break;

case0:

break;

default:

cout<<"输入错误!

请重新输入。

"<

}

cout<

}

return0;

}

【结果及结果分析】

1、插入学生信息:

插入后的二叉树为:

2、删除学生信息:

3、按学号查询学生信息:

4、输出二叉树中所有的学生信息:

【参考文献】

、《数据结构—C++描述》朱振元朱承编著

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1