您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > 学生选修课管理系统-C++课程设计
#includeiostream.h#includestring.h#includefstream.h#includestdlib.h#includetime.h//usingnamespacestd;constintcount=3;classsituaction;classstudent{public:intsno;charsname[20];charsex[3];intage;chardepart[20];charclassno[20];charlianx[40];//public:friendvoidsselect();friendvoidsupdate();friendvoidsdelet();friendvoidsaccount();friendvoidsdisplay();friendistream&operator(istream&in,student&stu){instu.snostu.snamestu.sexstu.agestu.departstu.classnostu.lianx;returnin;}friendostream&operator(ostream&out,student&stu){outstu.snostu.snamestu.sexstu.agestu.departstu.classnostu.lianxendl;returnout;}friendvoidxkadd();friendvoidsadd();};voidsadd(){while(1){intflag1=0;students;cout请输入要添加的学生的学号、姓名、性别、年龄、系别、班级、联系方式endl;cins;ifstreamin(e:\\studentfile.txt);if(!in){cout打开学生信息文件失败!endl;return;}studentstu;in.read((char*)&stu,sizeof(student));while(!in.eof()){if(s.sno==stu.sno){flag1=1;break;}in.read((char*)&stu,sizeof(student));}in.close();if(flag1==1)cout该学生已经在文件中存在不允许添加!endl;else{ofstreamout(e:\\studentfile.txt,ios::app);out.write((char*)&s,sizeof(student));out.close();}system(pause);break;}}voidsselect(){while(1){system(cls);cout***********请选择要查询的类别:*************endl;cout***********按学生学号请按1:*************endl;cout***********按学生姓名请按2:*************endl;cout***********按学生系别请按3:*************endl;cout***********按学生班级请按4:*************endl;cout***********返回上一级界面请按5:*************endl;intsn;cinsn;while(sn5||sn1){cout选择的数据无效,请重新输入:endl;cinsn;}coutendl;ifstreamin(e:\\studentfile.txt);if(!in){cout打开学生信息文件失败!endl;return;}intflag=0;switch(sn){case1:{intno;cout请输入要查询的学生的学号endl;cinno;studentstu;in.read((char*)&stu,sizeof(student));while(!in.eof()){if(no==stu.sno){coutstuendl;flag=1;}in.read((char*)&stu,sizeof(student));}break;}case2:{charname[20];cout请输入要查询的学生的姓名endl;cinname;studentstu;in.read((char*)&stu,sizeof(student));while(!in.eof()){if(strcpy(name,stu.sname)==0){coutstuendl;flag=1;}in.read((char*)&stu,sizeof(student));}break;}case3:{chardep[20];cout请输入要查询的学生的系别endl;cindep;studentstu;in.read((char*)&stu,sizeof(student));while(!in.eof()){if(strcmp(dep,stu.depart)==0){coutstuendl;flag=1;}in.read((char*)&stu,sizeof(student));}break;}case4:{charclno[20];cout请输入要查询的学生的班级endl;cinclno;studentstu;in.read((char*)&stu,sizeof(student));while(!in.eof()){if(strcmp(clno,stu.classno)==0){coutstuendl;flag=1;}in.read((char*)&stu,sizeof(student));}break;}case5:return;}in.close();if(flag==0)cout要查询的学生在文件不存在!endl;system(pause);}}voidsupdate(){while(1){system(cls);cout***********请选择按下列类别查询后修改:*************endl;cout***********按学生学号请按1:*************endl;cout***********按学生姓名请按2:*************endl;cout***********按学生系别请按3:*************endl;cout***********按学生班级请按4:*************endl;cout***********返回上一级界面请按5:*************endl;intsn;cinsn;while(sn5||sn1){cout选择的数据无效,请重新输入:endl;cinsn;}coutendl;fstreamin(e:\\studentfile.txt,ios::in|ios::out);if(!in){cout打开学生信息文件失败!endl;return;}intflag=0;switch(sn){case1:{intno;cout请输入要查询的学生的学号endl;cinno;studentstu;inti=0;in.seekg(i*sizeof(student),ios::beg);in.read((char*)&stu,sizeof(student));while(!in.eof()){if(no==stu.sno){coutstuendl;cout请输入要修改的学生的学号、姓名、性别、年龄、系别、班级、联系方式endl;cinstu;in.seekp(i*sizeof(student),ios::beg);in.write((char*)&stu,sizeof(student));flag=1;}i++;in.seekg(i*sizeof(student),ios::beg);in.read((char*)&stu,sizeof(student));}break;}case2:case3:case4:case5:return;}in.close();if(flag==0)cout要修改的学生在文件不存在!endl;system(pause);}}voidsdelet(){while(1){system(cls);cout***********请选择按下列类别查询后删除:*************endl;cout***********按学生学号请按1:*************endl;cout***********按学生姓名请按2:*************endl;cout***********按学生系别请按3:*************endl;cout***********按学生班级请按4:*************endl;cout***********返回上一级界面请按5:*************endl;intsn;cinsn;while(sn5||sn1){cout选择的数据无效,请重新输入:endl;cinsn;}coutendl;studentstu[20];intflag=0;switch(sn){case1:{ifstreamin(e:\\studentfile.txt);if(!in){cout打开学生信息文件失败!endl;return;}intn=0;in.read((char*)&stu[n],sizeof(student));while(!in.eof()){n++;in.read((char*)&stu[n],sizeof(student));}in.close();intno;cout请输入要删除的学生的学号endl;cinno;for(inti=0;in;i++){if(no==stu[i].sno){coutstu[i]endl;cout是否要删除该学生的信息!endl;charch;cinch;if(ch='Y'||ch=='y'){for(intj=i;jn;j++)stu[j]=stu[j+1];n--;}flag=1;}}if(flag==0)cout要修改的学生在文件不存在!endl;else{ofstreamout(e:\\studentfile.txt);if(!out){cout打开学生信息文件失败!endl;return;}for(intk=0;kn;k++){out.seekp(k*sizeof(student),ios::beg);out.write((char*)&stu[k],sizeof(student));}out.close();}break;}case2:case3:case4:case5:return;}system(pause);}}voidsaccount(){ifstreamifs(e:\\studentfile.txt);if(!ifs){cout打开文件失败!;system(pause);return;}inti=0;students;ifs.read((char*)&s,sizeof(student));while(!ifs.eof()){i++;ifs.read((char*)&s,sizeof(student));}ifs.close();cout学生:i人endl;ifs.close();system(pause);}voidsdisplay(){ifstreami
本文标题:学生选修课管理系统-C++课程设计
链接地址:https://www.777doc.com/doc-5030718 .html