收藏 分销(赏)

顺序表建立学生健康表.doc

上传人:xrp****65 文档编号:7433188 上传时间:2025-01-04 格式:DOC 页数:10 大小:83KB 下载积分:10 金币
下载 相关 举报
顺序表建立学生健康表.doc_第1页
第1页 / 共10页
顺序表建立学生健康表.doc_第2页
第2页 / 共10页


点击查看更多>>
资源描述
题目 学生健康情况管理系统 【问题描述】 实现学生健康情况管理的几个操作功能(新建、插入、删除、从文件读取、写入文件和查询、屏幕输出等功能)。健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。 【实验内容】 必做内容 利用顺序存储结构来实现 系统的菜单功能项如下: 1------新建学生健康表 2------向学生健康表插入学生信息 3------在健康表删除学生信息 4------从文件中读取健康表信息 5------向文件写入学生健康表信息 6------在健康表中查询学生信息(按学生学号来进行查找) 7------在屏幕中输出全部学生信息 8-----退出 程序如下: #include <iostream> #include <fstream> using namespace std; /* * 学生结点的设置,包含学号stuID,姓名name,出生日期BirthOfDate, 性别sex(B、G),健康状况(A,B,C) */ struct birthday //出生日期 { unsigned short day; unsigned short month; unsigned short year; }; struct Sstudent //一个学生的基本信息 { char stuID[12]; //学号 char name[12]; //名字 struct birthday bd; //出生日期 char sex[4]; //性别 char healthcase[10]; //健康情况 Sstudent(){} void input(); //输入学生的基本信息 void output(); //输出学生的基本信息 void operator =(Sstudent s); bool operator < (Sstudent &s); bool operator == (Sstudent s); bool operator > (Sstudent &s); }; void Sstudent::input() //输入一个学生的信息 { cout<<"请输入学生信息:"<<endl; cout<<"请输入学生的学号:"; cin>>stuID; cout<<"请输入学生的名字:"; cin>>name; cout<<"请输入学生的性别:"; cin>>sex; cout<<"请输入学生生日的日期(年、月、日):"; cin>>bd.year>>bd.month>>bd.day; cout<<"请输入学生的健康情况(良好或差):"; cin>>healthcase; cout<<endl; } void Sstudent::output() //输出一个学生的信息 { cout<<"学号: "<<stuID<<endl <<"姓名: "<<name<<endl <<"性别: "<<sex<<endl <<"生日: "<<bd.year<<"/"<<bd.month<<"/"<<bd.day<<endl <<"健康情况: "<<healthcase<<endl<<endl; } void Sstudent::operator =(Sstudent s) { strcpy(stuID,s.stuID); strcpy(name,s.name); strcpy(sex,s.sex); 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(stuID,s.stuID) == -1) //若number 小于 s.number return true; else return false; } bool Sstudent::operator == (Sstudent s) { if( !strcmp(name,s.name) ) //若name 等于 s.number if( !strcmp(stuID,s.stuID) ) //若num 等于 s.number return true; return false; } bool Sstudent::operator > (Sstudent &s) { if(strcmp(stuID,s.stuID) == 1) //若number 大于 s.number return true; else return false; } class Student { public: Student(); Student(int sz); //构造函数 ~Student(){}; //析构函数 void makeEmpty(); //将数组置为空 void SetData(); //初步建立一个线性表 void Search(char ID[12]); //在健康表中查询学生信息(按学生学号来进行查找) bool Insert(int i,Sstudent s); //向学生健康表插入学生信息 void Remove(int i, Sstudent& s); //在健康表删除学生信息 void Print(); //在屏幕中输出全部学生信息 void Write(); void ReadInsert(Sstudent s); Student Read(); private: Sstudent *stu; int last; int maxSize; }; /* * 构造函数,初始化数组 */ Student::Student() { maxSize = 50; last = -1; stu = new Sstudent[maxSize]; if (stu == NULL) { cerr<<"存储分配错误"<<endl; exit(1); } } Student::Student(int sz ) { if (sz > 0) { maxSize = sz; last = -1; stu = new Sstudent[maxSize]; if (stu == NULL) { cerr<<"存储分配错误"<<endl; exit(1); } } else { cerr<<"初始化数组错误!!!"<<endl; } } /* * 将数组置为空 无返回值 */ void Student::makeEmpty() { last = -1; } /* * 在健康表中查询学生信息(按学生学号来进行查找) 在表中搜索学号为ID的学生信息,搜索成功返回该学生结点的地址,不成功返回NULL; */ void Student::Search(char ID[12]) { for (int i = 0; i <= last; i++) { if (strcmp(ID,stu[i].stuID)==0) { stu[i].output(); return; } } cout<<" 不存在此学生。"<<endl; } /* * 前插法初步建立一个学生线性表 无返回值 */ void Student::SetData() { int count; cout<<" 输入学生的个数count = "; cin>>count; for (int i = 0; i < count; i++) { stu[i].input(); last++; } } /* * 向学生健康表插入学生信息 将新结点s插入在数组的第i个结点。 */ bool Student::Insert(int i,Sstudent s) { if (last == maxSize -1) { cout<<" 表满!不能插入"<<endl; return false; //插入不成功 } if (i < 0 || i > last + 1) { cerr<<"参数i不合理,不能插入"<<endl; return false; } s.input(); for (int j = last; j >= i; j--) { stu[j + 1] = stu[j]; } stu[i] = s; last++; //最后的位置加1 return true; //插入成功 } /* * 在健康表中删除学生信息 */ void Student::Remove(int i, Sstudent& s) { if (last == -1) { cerr<<"健康表为空,不能删除!!!"<<endl; return ; } if (i < 1 || i > last + 1) { cerr<<"参数i不合理,不能插入"<<endl; return ; } s = stu[i - 1]; cout<<"被删除结点的数据是:"<<endl; s.output(); for (int j = i; j <= last; j++) { stu[j - 1] = stu[j]; } last--; } /* * 将健康表中所有的学生信息输出 */ void Student::Print() { for (int i = 0; i <= last; i++) { stu[i].output(); } } /* * 向文件写入学生健康表信息 */ void Student::Write() { ofstream fop; //以二进制方式打开stu.txt文件 fop.open("stu.txt",ios::out|ios::binary|ios::trunc); if(fop.fail()) //文件打开失败 { cout<<"文件打开失败!"<<endl; return; } for(int i=0;i < last; i++) //把各字符信息写入文件 { fop.write((char*)&stu[i],sizeof(stu[i])); flush(cout); } cout<<"文件写入成功"<<endl; } /* * 从文件中读取健康表信息 */ Student Student::Read() { ifstream fip; //以二进制方式打开stu.txt文件 fip.open("stu.txt",ios::binary|ios::in); if(fip.fail()) //文件打开失败 { cout<<"文件打开失败!"<<endl; return NULL; //结束本函数 } Sstudent s; Student student; int i = 0; while(1) { fip.read((char*)&s,sizeof(s)); //从文件中读入一个学生的信息 if(!fip) { break; } student.ReadInsert(s); } fip.close(); cout<<"读写成功"<<endl; return student; } void Student::ReadInsert(Sstudent s) { int i = 0; stu[i] = s; last++; //最后的位置加1 } /* * 菜单函数 无返回值 */ void menu() { cout<<" 1------新建学生健康表--------------------------------"<<endl; cout<<" 2------向学生健康表插入学生信息----------------------"<<endl; cout<<" 3------在健康表删除学生信息--------------------------"<<endl; cout<<" 4------从文件中读取健康表信息------------------------"<<endl; cout<<" 5------向文件写入学生健康表信息----------------------"<<endl; cout<<" 6------在健康表中查询学生信息(按学生学号来进行查找)"<<endl; cout<<" 7------在屏幕中输出全部学生信息----------------------"<<endl; cout<<" 8----- 退出------------------------------------------"<<endl; } void main() { int i,choose; //i为删除的位置,choose是选择的操作 char ID[12]; //所删除学生的学号 Sstudent s; Student student,temp; //初始化线性表 do { menu(); cout<<" 请输入你的选择(1~8):"; cin>>choose; switch (choose) { case 1://新建学生健康表 student.SetData(); student.Print(); break; case 2://向学生健康表插入学生信息 cout<<"在哪一个位置插入学生信息 i=?"; cin>>i; student.Insert(i,s); break; case 3://在健康表删除学生信息 cout<<"删除第i个元素 i=? "; cin>>i; student.Remove(i,s); break; case 4://从文件中读取健康表信息 student = student.Read(); break; case 5://向文件写入学生健康表信息 student.Write(); break; case 6://在健康表中查询学生信息(按学生学号来进行查找) cout<<"请输入查询学生的学号: "; cin>>ID; student.Search(ID); break; case 7://在屏幕中输出全部学生信息 student.Print(); break; case 8://退出 exit(1); break; default: cout<<"choose 输入错误!!!请重新输入choose :"<<endl; cin>>choose; } } while (choose!=8); } 运行效果图:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 应用文书 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服