1、#用二叉树来实现学生健康情况管理系统【综设实验题目】 实现学生健康情况管理的几个操作功能(新建、插入、删除、从文件读取、写入文件和查询、屏幕输出等功能)。健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。实验内容1利用二叉树来实现2系统的菜单功能项如下:1-新建学生健康表2-向学生健康表插入学生信息3-在健康表删除学生信息4-从文件中读取健康表信息5-向文件写入学生健康表信息6-在健康表中查询学生信息(按学生学号来进行查找)7-在屏幕中输出全部学生信息8-退出【中文摘要】 这次实验主要用二叉树来实现简单的学生健康管理系统,为了方便查找,进一步使用排序二叉树来实现。系统的功能包括:向学
2、生健康表插入学生信息,在健康表删除学生信息,从文件中读取健康表信息,向文件写入学生健康表信息,在健康表中查询学生信息(按学生学号来进行查找),在屏幕中输出全部学生信息等。健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。【关键词 】 排序二叉树 学生健康管理系统 学生信息【前言】本次实验是为了进一步熟悉和掌握VC环境下的编译、调试和执行的方法及步骤,熟悉二叉树存储的实现方式及其使用。【实验设计】 以排序二叉树为储存机制,可以方便的实现插入或删除学生信息。每个学生的信息储存在一个结构体Sstudent中,并且这个结构体带有输出学生信息的函数ouput()。然后以这个结构体作为二叉树节点
3、的数据类型,这样就实现了学生信息的储存。在创建二叉树对象时将已存储在文件中的学生信息写入二叉树,在析构函数里实现将学生信息写入文件。【实验实现】 软件平台:VC+ 6.0硬件平台:32位机器主要功能模块分析:1、储存一个学生的信息:/*Sstudent.h 文件*/#ifndef _Sstudent_h_#define _Sstudent_h_#include using namespace std;struct birthday /出生日期 unsigned short day; unsigned short month; unsigned short year; ;struct Sstud
4、ent /一个学生的基本信息 char number12; /学号 char name12; /名字 struct birthday bd; /出生日期 char gender4; /性别 char healthcase10; /健康情况 Sstudent() void input(); /输入学生的基本信息 void output(); /输出学生的基本信息 void operator =(Sstudent s); bool operator (Sstudent &s); ;void Sstudent:input() /输入一个学生的信息 cout请输入学生信息:endl; coutnumb
5、er; coutname; coutgender; coutbd.yearbd.monthbd.day; couthealthcase; coutendl;void Sstudent:output() /输出一个学生的信息 cout学号: numberendl 姓名: nameendl 性别: genderendl 生日: bd.year/bd.month/bd.dayendl 健康情况: healthcaseendlendl;void Sstudent:operator =(Sstudent s) strcpy(number,s.number); strcpy(name,s.name); s
6、trcpy(gender,s.gender); bd.year=s.bd.year; bd.month=s.bd.month; bd.day=s.bd.day; strcpy(healthcase,s.healthcase);bool Sstudent:operator (Sstudent &s) if(strcmp(number,s.number) = 1) /若number 大于 s.number return true; else return false;#endif2、将文件中的学生健康信息写进排序二叉树BSTree:BSTree() root=NULL; BTreeNode *b=
7、NULL; Sstudent s; fstream file; file.open(student.dat,ios:in|ios:binary); if(!file) cerrstudent.dat cant open!data); /调用插入函数建二叉树 file.close();3、将排序二叉树中的学生信息写进文件思想:在退出程序调用析构函数时,通过前序遍历,将访问到的节点信息依次写进文件。BSTree:BSTree() fstream file; file.open(student.dat,ios:out|ios:binary); if(!file) cerrstudent.dat ca
8、nt open!=0) while(p!=NULL) write1(file,p); stacktop+=p; p=p-lchild; top-; p=stacktop; if( p!=NULL) p=p-rchild; coutendl;void write1(fstream &outfile, BTreeNode *p) /将节点p写进文件流 if(p=NULL) coutp=NULLdata), sizeof(p-data) );4、向学生健康表插入学生信息/具有插入功能的查找(递归)void BSTree:inst(BTreeNode *p,BTreeNode *&bst) if (b
9、st=NULL) bst=p; else if (p-data data ) inst(p,bst-lchild); else inst(p,bst-rchild);/*功能:在当前的排序二叉树上查找关键字等于给定值el的结点,当查找成功时输出相应的信息,否则建立一个新结点并插入在当前排序二叉树的适当位置中。*/void BSTree:inst(Sstudent el) BTreeNode *p; p = sear(el.number); if (p != NULL ) cout已找到 data.number); if(k = -1) pfather=p; p=p-lchild; else i
10、f(k = 1) pfather=p; p=p-rchild; else break; if (p = NULL ) cout找不到该同学信息,删除失败!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 = pf
11、ather-lchild) /p是左子节点 pfather-lchild = p-rchild; else pfather-rchild = p-rchild; else if(p-rchild = NULL) if(p = pfather-lchild) /p是左子节点 pfather-lchild = p-lchild; else pfather-rchild = p-lchild; p-data.output(); return p;/实例函数BTreeNode * BSTree:deleteNode(char num) BTreeNode *p=root; return deleteN
12、ode(p,num);6、在屏幕中输出全部学生信息/采用中序遍历的思想输出节点信息,则在屏幕上根据学生学号的升序输出。void BTree:prnt(BTreeNode *p) /递归函数 if (p!=NULL ) prnt(p-lchild); p-data.output(); prnt(p-rchild); /实例函数void BTree:prnt() prnt(root);7、用户界面主函数文件:int main() BSTree t; /新建一个二叉树:t int choice=10; /将choice初始化,使其不为04 Sstudent s; BTreeNode *p=NULL;
13、 char num12; while(choice) cout请输入您要进行的操作:endl; cout1-向学生健康表插入学生信息endl 2-在健康表删除学生信息endl 3-在健康表中查询学生信息(按学生学号来进行查找)endl 4-在屏幕中输出全部学生信息endl 0-退出choice; switch(choice) case 1: s.input(); /输入学生信息 t.inst(s); break; case 2: coutnum; p=t.deleteNode(num); if (p!=NULL) cout删除成功!删除学生的信息为:getdata().output(); break; case 3: coutnum; p=t.sear(num); if (p!=NULL) p-getdata().output(); else cout找不到该学生信息!endl; break; case 4: t.prnt(); break; case 0: break; default: cout输入错误!请重新输入。endl; coutendl; return 0;【结果及结果分析】1、插入学生信息:插入后的二叉树为:2、删除学生信息:3、按学号查询学生信息:4、输出二叉树中所有的学生信息:【参考文献】、数据结构C+描述 朱振元 朱承 编著
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1