还剩41页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
西安电子科技大学面向对象实验报月日1120char buf_ID
[20]={0};char buf_name
[20]={0};char buf_age
[20]={0};char buf_function
[20]={0};char buf_academy
[20]={0};〃分状况读取文献switcha{case1:ifstream infileHStudentCarde.txtn,ios::in;〃定义文献输入流if!infile{cerr«nopen failed!;while!infile.eof infile.getlinebuLID,20;\n,;〃如果是1就表达是保存StudentCarde文献,将相应信息读取到vector中infile.getlinebuf_name,20,,\n,;infile.getlinebufLage,20,\n;infile.getlinebu^function,20,,\n,;infile.getlinebufLacademy,20,,\n,;ifstrlenbufL.ID==O||strlenbufL.name==O||strlenbuf_age==O||strlenbufLfun ction==0||strlenbufLacademy==0{continue;}StudentCarde p1;pl.setStudentCardebufLID,bufLname,atoibufLage,bufLfunction,buf_academy;v_StudentCarde.push_backp1;infile.close;break;}case2:ifstream infilenGoodStudent.txtM,ios::in;〃定义文献输入流if!infile{cerr«nopen failed!!;while!infile.eof infile.getlmebuLID,20;\n,;〃如果是2就表达是保存GoodStudent文献,将相应信息读取到vector中infile.getlinebuCname,20,^n1;infile.getlinebufLage,20/\n,;ifstrlenbuf_ID==O||strlenbufLname==O||strlenbuf_age==O{continue;GoodStudent p2;p
2.setGoodStudentbufLID,bufLname,atoibufLage;v_GoodStudent.push_backp2;}infile.close;break;}case3::{ifstream infileHGoodStudentCarde.txtn,ios::in;〃定义文献输入流if!infile{cerr«open failed!H;while!infile.eof{infile.getlinebuJID,20,\n;〃如果是3就表达是保存GoodStudentCarde文献,将相应信息读取到vector中infile.getlinebuCname,20,!\n*;infile.getlinebufLage,20,\n!;infile.getlinebuCfunction,20,!\0*;infile.getlinebu^academy,20,An1;ifstrlenbufLID==O||strlenbuCname==O||strlenbufLage==O||strlenbufLfunction==0||strlenbufLacademy==0{continue;GoodStudentCarde p3;p
3.setGoodStudentCardebufLID,buCname,atoibuiLage,bufLfunction,bufLacademy;infile.close;break;default:cout«*1read_error!n;break;//定义write函数,将信息写入到文献中去void writeint a/*char buf_ID
[20];char bufLname
[20];char bufLage|20];char bufLfunction
[20];char buCacademy
[20];*/switcha case1:ofstream outfilenStudentCarde.txtn,ios::out;if!outfilecerr«nopen failed!;exitl;for vectorStudentCarde::size_typei=O;i!=v_StudentCarde.sizev_StudentCarde[i].getStudent_ageO;i++//如果是1话就将学生干部类数遍写入到文献中outfile«v_StudentCarde[i].getStudent_ID«endl;outfilc«v_StudcntCardc[i].gctStudcnt_namc«cndl;outfile«v_StudentCarde[i].getStudent_age«endl;outfile«v_StudentCarde[i].getStudentCarde_function«endl;outfile«v_StudentCarde[i].getStudentCarde_academy«endl;//v_StudentCarde.clear;outfile.close;break;}case2:ofstream outfilenGoodStudent.txtn,ios::out;if!outfilecerr«nopenfailed!0;exitl;for vectorGoodStudent::size_type j=0;j!=v_GoodStudent.size;j++//如果是2话就将先进学生数据写入到文献中去outfile«v_GoodStudent[j].getStudent_ID«endl;outfile«v_GoodStudent[j].getStudent_name«endl;outfile«v_GoodStudent[j].getStudent_age«endl;//v_GoodStudent.clear;outfile.close;break;case3:ofstream outfileHGoodStudentCarde.txtn,ios::out;if!outfile cerr«nopen failed!n;exitl;}for vectorGoodStudent::size_ty pek=0;k!=v_GoodStudentCarde.sizev_GoodStudentCarde[k].getStudent_age0;k++//如果是3话就将先进学生干部类数据写入到文献中去outfile«v_GoodStudentCarde[k].getStudent_ID«endl;outfile«v_GoodStudentCarde[k].getStudent_name«endl;outfile«v_GoodStudentCarde[k].getStudent_age«endl;outfile«v_GoodStudentCarde[k].getStudentCarde_function«endl;outfile«v_GoodStudentCarde[k].getStudentCarde_academy«endl;//v_GoodStudentCarde.clear;outfile.close;break;default:cout«nwrite error**;break;//定义增长函数void addinti{string buf_ID;string buf_name;int bujage;string bufLfunction;string bufLacademy;switchi{case1:{cout”请依次输入学生干部类学号,姓名,年龄,职务,学院:“endl;〃依次输入学生干部类有关数据cin»buf_ID»buCname»buCage»buCfunction»bufLacaderny;StudentCarde a;a.setStudentCardebuf_ID,bufLname,bufLage,buf_function,buLacademy;v_StudentCarde.push_backa;〃将学生干部类对象添加到v_GoodStudentCarde中去cout”请到文献中查看“endl;break;case2:cout”请依次输入先进学生类学号,姓名,年龄”endl;//依次输入学生干部类有关数据cin»bufLID»bufLname»bufLage;GoodStudent a;a.setGoodStudentbuOD,bufLname,buCage;v_GoodStudent.push_backa;cout«n请到文献中查看v Vendl;break;case3:COUtVV”请依次输入先进学生干部类学号,姓名,年龄,职务,学院:kvendl;//依次输入先进学生干部类有关数据cin»bufLID»buf_name»bufLage»bufLfunction»bufLacademy;GoodStudentCarde a;a.setGoodStudentCardebufLID,buCname,buCage,buCfunction,buCacademy;v_GoodStudentCarde.push_backa;〃将先进学生干部类对象添加到v_GoodStudentCarde中去coutvv”请到文献中查看vvendl;break;default:cout«nadd_error!n;break;〃定义查询函数void inquiryint a string inquiry_ID;switchacase1:coutv”请输入要查询学生干部类对象学号:“vendl;//如果是1话就进行学生干部类查询cin»inquiry_ID;forvectorStudentCarde::size_type i=O;i!=v_StudentCard已size;i++ifv_StudentCarde[i].getStudent_ID==inquiry_IDcout〈”查询学生干部成果:endl;v_StudentCarde[i].printStudentCarde;〃查询就可以输出break;break;case2:cout”请输入要查询先进学生类对象学号:vvendl;//如果是2话就进行先进学生类查询cin»inquiry_ID;forvectorGoodStudent::size_type j=0;j!=v_GoodStudent.size;j++ifv_GoodStudent[j].getStudent_ID==inquiry_IDcoutv”查询先进学生成果:endl;v_GoodStudent[j].printStudent;//查询就可以输出break;}break;case3:cout〈”请输入要查询先进学生干部类对象学号:endl;//如果是3话就进行先进学生干部类查询cin»inquiry_ID;forvectorGoodStudentCarde::s ize_typek=0;k!=v_GoodStudentCarde.size;k++ifv_GoodStudentCarde[k].getStudent_ID==inquiry_IDcoutw”查询先进学生干部成果:“《endl;v_GoodStudentCarde[k].printStudentCarde;〃查询就可以输出break;}break;default:cout«Hinquiry_error!H;break;〃定义删除函数void Deleteint astringDelete_ID;switcha case1:cout”请输入要删除学生干部类对象学号:;cout«endl;//如果是1话就进行学生干部类删除操作cin»Delete_ID;vectorStudentCarde:iterator it;forit=v_StudentCarde.begin;it!=v_StudentCarde.end;if*it.getStudent_ID-Delete_IDcout删除学生干部类成果:“endl;*it.printStudentCarde;〃输出删除元素v_StudentCarde.eraseit;break;it++;}break;case2:cout”请输入要删除先进学生类对象学号:;coutvvendl;//如果是2话就进行先进学生类删除操作cin»Delete_ID;vcctorGoodStudcnt::iterator it;forit=v_GoodStudent.begin;it!=v_GoodStudent.end;if*it.getStudent_ID==Delete_ID{coutvv”删除先进学生类成果:“endl;*i t.prints tudent;〃输出删除元素v_GoodStudent.eraseit;break;++it;break;}case3:cout”请输入要删除先进学生干部类对象学号:;cout«endl;//如果是1话就进行学生干部类删除操作cin»Delete_ID;vectorGoodStudentCarde:iterator it;forit=v_GoodStudentCarde.begin;it!=v_GoodStudentCarde.end;if*it.getStudent_ID==Delete_ID|cout«”删除先进学生干部类成果:“endl;*it.printStudentCarde;〃输出删除元素v_GoodStudentCarde.eraseit;break;}++it;break;default:cout«Delete_error!H;break;〃定义更新操作void updateint astring bufLID;string buf_ID1;string bufLname;int bufLage;string bufLfunction;string buf_academy;switchacase1:cout«”请输入要更新学生干部类学号:“endl;cin»bufLIDl;coutvv”请输入要更新属性vvendl;cin»bufLID»bufLname»bufLage»bufLfunction»bufLacademy;//输入要更新属性值forvectorStudentCarde::size_type i=O;i!=v_StudentCarde.size;i++ifv_StudentCarde[i].getStudent_ID-buCIDlv_StudentCarde[i].setStudentCardebufLID,buCname,bufLage,buCfunction,buf_academy;〃更新数据cout”更新成功:n«endl;break;case2:cout”请输入要更新先进学生类学号:“endl;cin»bufLIDl;coutvv”请输入要更新属性vvendl;cin»bufLID»buf_name»bufLage;〃输入要更新属性值for vectorGoodStudent::size_type j=0;j!=v_GoodStudent.size;j++ifv_GoodStudent[j].getStudent_ID==bufLIDl{v_GoodStudcnt[j].sctGoodStudcntbufLID,bulLnamc,bufLagc;〃更新数据cout更新成功vvendl;break;case3:coutvv”请输入要更新先进学生干部类学号:“vvendl;cin»bufLIDl;cout”请输入要更新属性”endl;cin»buf_ID»buCname»buiLage»bufLfunction»bufLacademy;〃输入要更新属性值for vectorGoodStudent::size_typek=0;k!=v_GoodStudentCarde.size;k++ifv_GoodStudentCarde[k].getStudent_ID==bufLID1{v_GoodStudentCarde[k].setGoodStudentCardebufLID,buLname,bufLage,buf—function,bu匚academy;〃更新数据cout”更新成功:n«endl;break;default:cout«Hupdate_error!H«endl;〃重组文献void recombinefilewritel;write2;write3;void SeletStudentcout〈”请输入要选取学生类型”cndl;coutvv”类型1学生干部类”endl;cout类型2先进学生类endl;cout”类型3先进学生干部类”endl;//主函数实现void maincout〈”欢迎来到学生管理系统,请选取要进行操作”《endl;cout«”操作1增长学生”vvendl;coutv”操作2查询学生”vvendl;coutvv”操作3删除学生vvendl;cout”操作4更新学生”《endl;重coutv操作5组文献vvendl;记coutvv操作6录人数”vendl;inta;whilecin»a{switchacase1:SeletStudent;〃增长操作int b;cin»b;readb;addb;writeb;break;case2:〃查询操作SeletStudent;int c;cin»c;readc;inquiryc;break;case3:〃删除操作SeletStudent;int d;cin»d;readd;Deleted;writed;break;case4:〃更新操作SeletStudent;int e;cin»e;reade;updatee;writee;break;case5:〃重组文献操作recombinefile;break;case6:cout输出三类学生人数”endl;readl;read2;read3;coutvv学生干部类学生人数n«v_StudentCarde.size«endl;cout先进学生类学生人数n«v_GoodStudent.size«endl;coutvv”先进学生干部类学生人数n«v_GoodStudentCarde.size«endl;cout”请选取操作H«endl;设计一种虚基类一一学生类,包括学号、姓名和年龄保护数据成员以及有关成员函数;由学生类派生出学生干部类,包括职务和学院保护数据成员以及有关成员函数;再由学生类派生出先进学生类;然后由学生干部类和先进学生类派生出先进学生干部类设计一种运用文献解决方式实现对学校学生(涉及学生干部、先进学生和先进学生干部)进行管理,具备增长数据、更新数据、查询数据、删除数据以及重组文献功能(删除数据在记录中做删除标志,重组文献指在物理上删除有删除标志记录)此外规定分别记录这三类学生人数以及所有学生总数*/#include string#includeiostream#includefstream#includestdlib.h#includevectorusing namespacestd;〃编写学生类class Student{protected:string ID;string name;int age;public://Student;〃无参数构造函数〃〜Student;〃析构函数void setStudentstring a,string b,int c;〃输入函数void printStudent;〃输出函数string getStudent_ID;〃获取函数string getStudent_name;int getStudent_age;;〃编写学生干部类,继承自Student类class StudentCarde:virtual publicStudent{protected:string function;string academy;public://StudentCarde;//^StudentCarde;void setStudentCardestring a,string b,int c,string d,string e;void printStudentCarde;void getStudentCarde;string getStudentCarde_function;string getStudentCarde_academy;;〃先进学生类class GoodStudent:virtual publicStudent{public://GoodStudent;//〜GoodStudent;void setGoodStudentstring a,string b,int c;void printGoodStudent;void getGoodStudent;;〃先进学生干部类class GoodStudentCarde:public StudentCarde,public GoodStudent{public://GoodStudentCarde;//-GoodStudentCarde;void setGoodStudentCardestring a,string b,int c,string d,string e;void printGoodStudentCarde;};〃定义三类学生各自vector,设定为全局变量vectorStudentCarde vStudentCarde;vectorGoodStudent v_GoodStudent;vectorGoodStudentCarde vGoodStudentCarde;//获取vector大小值//int vStudentCarde Size=0;//int vGoodStudent Size=0;//int v_GoodStudentCarde_Size=0;〃实现学生类setStudent函数void Student::setStudentstring a,string b,int c〃实现学生类set函数ID=a;name=b;age=c;}〃实现学生类输出函数void Student::printStudent cout«nID:n«ID«endl;cout«nname:n«name«endl;cout«nage:n«age«endl;〃实现学生干部类有关函数void StudentCarde::setStudentCardestring a,string bjntc,string d,string eID=a;name=b;age=c;functioned;academy=e;}〃实现学生干部类StudentCardeprint函数void StudentCarde::printStudentCarde printStudent;cout«nfunction:n«function«endl;cout«nacademy:n«academy«endl;〃实现先进学生类setGoodStudentvoid GoodStudent::setGoodStudentstring a,string b,int cID=a;name=b;age=c;//实现先进学生printGoodStudent函数void GoodStudent::printGoodStudentprintStudent;〃实现先进学生干部类void GoodStudentCarde::setGoodStudentCardestringa,string b,int c,string d,string eID二a;name=b;age=c;function=d;academy=e;〃实现学生类getStudent_ID函数string Student::getStudent_IDreturn ID;}〃实现学生类gctStudcnt_namcstring Student::getStudent_namereturn name;//实现学生类getStudent_age函数int Student::getStudent_agereturn age;〃实现学生干部类getStudentCarde_function函数string StudentCarde::getStudentCarde_functionreturn function;〃实现学生干部类getStudentCarde函数string StudentCarde::getStudentCarde_academyreturn academy;//定义read函数,从文献中读出信息,并且存储在vector中void readinta char buf_ID
[20]={0};char buf_name
[20]={0};char buf_age
[20]={0};char buf_function
[20]={0};char buf_academy
[20]={0};〃分状况读取文献switcha{case1:ifstream infilenStudentCarde.txtn,ios::in;〃定义文献输入流if!infile{cerr«nopen failed!n;while!infile.eof infile.getlinebuLID,20;\n,;〃如果是1就表达是保存StudentCarde文献,将相应信息读取到vector中infile.getlinebufLname,20,,\n,;infile.getlinebufLage,20/\n,;infile.getlinebufLfunction,20,^n1;infile.getlinebuf_academy,20,,\n,;ifstrlenbuf_ID==O||strlenbufLname==O||strlenbufLage==O||strlenbufLfunction-0||strlenbufLacademy==0{continue;StudentCarde pl;pl.setStudentCardebuCID,bufLname,atoibufLage,buf_function,bufLacademy;v_StudentCarde.push_backpl;infile.close;break;case2:{ifstream infilenGoodStudent.txtn,ios::in;〃定义文献输入流if!infile{cerr«nopen failed!n;while!infile.eof infile.getlinebuLID,20;\n,;〃如果是2就表达是保存GoodStudent文献,将相应信息读取到vector中inEle.getlinebuJname,20,\n;inflle.getlinebufLage,20,\n;ifstrlenbufLID==O||strlenbufLname==O||strlenbuCHge==O continue;GoodStudent p2;p
2.setGoodStudentbufLID,bufLname,atoibufLage;v_GoodStudent.push_backp2;infile.close;break;}case3::{ifstream infilenGoodStudentCarde.txtH,ios::in;〃定义文献输入流if!infile{cerr«Hopen failed!n;while!infile.eofinfile.getlinebufLID,20,\n;〃如果是3就表达是保存GoodStudentCarde文献,将相应信息读取到vector中infile.getlinebuCname,20/\n*;infile.getlinebufLage,20,,\n,;infile.getlinebufLfunction,20/\n;infile.getlinebufLacademy,20,\n;ifstrlenbuf_ID==O||strlenbuf_name==O||strlenbufLage==O||strlenbufLfunction==0||strlenbufLacademy==0{continue;GoodStudentCarde p3;p
3.setGoodStudentCardebufLID,bufLname,atoibufLage,bufLfunction,buLacademy;v_GoodStudentCarde.push_backp3;infile.close;break;default:cout«*1read_error!n;break;}〃定义write函数,将信息写入到文献中去void writeinta{/*charbuf_ID
[20];面向对象大作业一:题目设计一种虚基类一一学生类,包括学号、姓名和年龄保护数据成员以及有关成员函数;由学生类派生出学生干部类,包括职务和学院保护数据成员以及有关成员函数;再由学生类派生出先进学生类;然后由学生干部类和先进学生类派生出先进学生干部类设计一种运用文献解决方式实现对学校学生(涉及学生干部、先进学生和先进学生干部)进行管理,具备增长数据、更新数据、查询数据、删除数据以及重组文献功能(删除数据在记录中做删除标志,重组文献指在物理上删除有删除标志记录)此外规定分别记录这三类学生人数以及所有学生总数二:编程思想
1.实现类图char buiLname
[20];char bufLage
[20];char bufLfunction
[20];char bufLacademy
[20];*/switcha case1:ofstream outfilenStudentCarde.txtn,ios::out;if!outfilecerr«Hopen failed!n;exitl;for vectorStudentCarde::size_typei=O;i!=v_StudentCarde.sizev_StudentCarde[i].getStudent_ageO;i++〃如果是1话就将学生干部类数痘写入到文献中outfile«v_StudentCarde[i].getStudent_ID«endl;outfile«v_StudentCarde[i].getStudent_name«endl;outfile«v_StudentCarde[i].getStudent_age«endl;outfile«v_StudentCarde[i].getStudentCarde_function«endl;outfile«v_StudentCarde[i].getStudentCarde_academy«endl;//v_StudentCarde.clear;outfile.close;break;case2:ofstream outfileHGoodStudent.txtH,ios::out;if!outfilecerr«nopen failed!;exitl;for vcctorGoodStudcnt::sizc_typc j=O;j!=v_GoodStudcnt.sizc;j++〃如果是2话就将先进学生数据写入到文薪中去outfile«v_GoodStudent[j].getStudent_ID«endl;outfile«v_GoodStudentfj].getStudent_name«endI;outfile«v_GoodStudent[j].getStudent_age«endl;//v_GoodStudent.clear;outfile.close;break;case3:ofstream outfilenGoodStudentCarde.txtn,ios::out;if!outfile cerr«nopen failed!n;exitl;forvectorGoodStudent::size_typek=0;k!=v_GoodStudentCarde.sizev_GoodStudentCarde[k].getStudent_age0;k++//如果是3话就将先进学生干部类数据写入到文献中去outfile«v_GoodStudentCarde[k].getStudent_ID«endl;outfile«v_GoodStudentCarde[k].getStudent_name«endl;outfile«v_GoodStudentCarde[k].getStudent_age«endl;outfile«v_GoodStudentCarde[k].getStudentCarde_function«endl;outfile«v_GoodStudentCarde[k].getStudentCarde_academy«endl;//v_GoodStudentCarde.clear;outfile.close;break;default:cout«Hwrite_errorn;break;//定义增长函数void addinti{string buJID;string bufLname;int bufLage;string bufLfunction;string bufLacademy;switchi{case1:cout«”请依次输入学生干部类学号,姓名,年龄,职务,学院:《endl;〃依次输入学生干部类有关数据cin»bufLID»bufLname»bufLage»buCfunction»bufLacademy;StudentCarde a;a.setStudentCardebuf_ID,bufLname,buf_age,bufl_function,bufLacademy;v_StudentCarde.push_backa;〃将学生干部类对象添加到v_GoodStudentCarde中去coutvv”请到文献中查看vvendl;break;case2:cout请依次输入先进学生类学号,姓名,年龄“endl;//依次输入学生干部类有关数据cin»buf_ID»bufLname»buCage;GoodStudent a;a.setGoodStudentbufLID,bufLname,bufLage;v_GoodStudent.push_backa;cout”请到文献中查看“endl;break;case3:cout”请依次输入先进学生干部类学号,姓名,年龄,职务学院:“vendl;〃依次输入先进学生干部类有关数据cin»bufLID»bufLname»bufLage»buCfunction»bufLacademy;GoodStudentCarde a;a.setGoodStudentCardebuOD,bufLname,bufLage,bufLfunction,bufLacademy;v_GoodStudentCarde.push_backa;〃将先进学生干部类对象添加到v_GoodStudentCarde中去coutvv”请到文献中查看vvendl;break;default:cout«nadd error!n;break;//定义查询函数void inquiryintastring inquiry_ID;switcha{case1:cout”请输入要查询学生干部类对象学号:“endl;//如果是1话就进行学生干部类查询cin»inquiry_ID;forvectorStudentCarde::size_type i=O;i!=v_StudentCarde.size;i++ifv_StudentCarde[i].getStudent_ID==inquiry_IDcout〈查询学生干部成果:endl;v_StudentCarde[i].printStudentCarde;〃查询就可以输出break;}break;case2:coutvv”请输入要查询先进学生类对象学号:“endl;//如果是2话就进行先进学生类查询cin»inquiry_ID;forvectorGoodStudent::size_type j=O;j!=v_GoodStudent.size;j++{ifv_GoodStudcntfj].gctStudcnt_ID==inquiry_ID cout查询先进学生成果:endl;v_GoodStudent[j].printStudent;〃查询就可以输出break;break;case3:cout«请输入要查询先进学生干部类对象学号:“endl;//如果是3话就进行先进学生干部类查询cin»inquiry_ID;forvectorGoodStudentCarde::size_typek=0;k!=v_GoodStudentCarde.size;k++ifv_GoodStudentCarde[k].getStudent_ID==inquiry_IDcoutv”查询先进学生干部成果:endl;v_GoodStudentCarde[k].printStudentCarde;〃查询就可以输出break;break;}default:cout«Hinquiry_error!n;break;}〃定义删除函数void Deleteintastring Delete_ID;switchacase1:{cout”请输入要删除学生干部类对象学号:;cout«endl;//如果是1话就进行学生干部类删除操作cin»Delete_ID;vectorStudentCarde:iterator it;forit=v_StudentCarde.begin;it!=v_StudentCarde.end;if*it.getStudent_ID-Delete_ID{coutvv”删除学生干部类成果:“endl;*it.printStudentCarde;〃输出删除元素v_StudentCarde.eraseit;break;it++;break;case2:cout”请输入要删除先进学生类对象学号:;cout«endl;//如果是2话就进行先进学生类删除操作cin»Delete_ID;vectorGoodStudent::iterator it;forit=v_GoodStudent.begin;it!=v_GoodStudent.end;if*it.getStudent_ID==Delete_IDcout«”删除先进学生类成果:“《endl;*it.printStudent;〃输出删除元素v_GoodStudent.eraseit;break;++it;break;case3:coutv”请输入要删除先进学生干部类对象学号:;cout«endl;〃如果是1话就进行学生干部类删除操作cin»Delete ID;vectorGoodStudentCarde::iterator it;forit=v_GoodStudentCarde.begin;it!=v_GoodStudentCarde.end;if*it.getStudent_ID==Delete_ID cout删除先进学生干部类成果:“endl;*it.printStudentCarde;〃输出删除元v_GoodStudentCarde.eraseit;break;++it;}break;default:cout«nDelete error!n;break;〃定义更新操作void updateintastring bufLID;string bufLID1;string buCname;int bujage;string buf^function;string buf_academy;switchacase1:cout«”请输入要更新学生干部类学号:“《endl;cin»buf_ID1;coutvv”请输入要更新属性vvendl;cin»bufLID»bufLname»bu^age»bufLfunction»bufLacademy;〃输入要更新属性值forvectorStudentCarde::size_type i=O;i!=v_StudentCarde.size;i++ifv_StudentCarde[i].getStudent_ID==buf_IDlv_StudentCarde[i].setStudentCardebufLID,bufl_name,buf_age,bufLfunction,buf_academy;〃更新数据cout”更新成功:n«endl;break;case2:cout”请输入要更新先进学生类学号:“《endl;cin»bufLIDl;cout”请输入要更新属性v Vendl;cin»bufLID»bufLname»bufLage;//输入要更新属性值for vectorGoodStudent::size_type j=0;j!=v_GoodStudentsize;j++ifv_GoodStudent[j].getStudent_ID==buf_IDlv_GoodStudent[j].setGoodStudentbufLID,bufLname,buf_age;〃更新数据coutvv”更新成功vvendl;break;case3:cout”请输入要更新先进学生干部类学号:“endl;cin»bufLIDl;coutvv”请输入要更新属性vvendl;cin»bu^ID»bufLname»bufLage»bufLfunction»bufLacademy;〃输入要更新属性值forvectorGoodStudent::size_typek=0;k!=v_GoodStudentCarde.size;k++{ifv_GoodStudcntCardc[k].gctStudcnt_ID-bufLID1v_GoodStudentCarde[k].setGoodStudentCardebuCID,buCname,buCage,buf_function,buCacademy;〃更新数据coutvv”更新成功:n«endl;break;default:cout«nupdate_error!n«endl;〃重组文献void recombinefilewritel;write2;write3;void SeletStudentcoutvv”请输入要选取学生类型vvendl;coutv类型1学生干部类wendl;cout”类型2先进学生类”《endl;cout类型3先进学生干部类”《endl;//主函数实现void maincout“欢迎来到学生管理系统,请选取要进行操作kvendlcout操作1增长学生”endl;coutvv”操作2查询学生”vendl;cout«”操作3删除学生”endl;coutv”操作4coutv”操作5更新学生”endl;cout操作6inta;重组文献”vvendl;whilecin»a{记录人数”cndl;switchacase1:SeletStudent;int b;cin»b;readb;〃增长操作addb;writeb;break;}case2:〃查询操作SeletStudent;int c;cin»c;readc;inquiryc;break;case3:〃删除操作SeletStudent;int d;cin»d;readd;Deleted;writed;break;case4:〃更新操作SeletStudent;int e;cin»e;reade;updatee;writee;break;case5:〃重组文献操作recombinefile;break;程序实现流图2数据整个程序实现思路3分别创立学生类、学生干部类,先进学生类,先进学生干部类,实现有关成员函数定义读取文献操作函数,写文献操作函数,增长元素操作函数,查询元素操作read writeadd函数,删除元素操作函数,更新元素操作函数,重组文献操作requiry Deleteupdate recombine函数在主函数中依照选取操作分别调用有关函数各个函数实现细节4读取文献操作函数read定义文献输入流将文献中内容所有读入到定义全局变量类型容器中去inElc,vector以便于下一步操作v_StudentCarde,v_GoodStudent,v_GoodStudnetCarde,写文献造作函数♦write定义文献输出流将类型变量中元素写入到文献中去outfile,vector增长元素操作操作add选取要增长学生类型,输入要增长元素属性值,将添加元素添加到相应变量尾vectorcase6:coutvv输出三类学生人数endl;readl;read2;read3;coutv学生干部类学生人数:n«v_StudentCarde.size«endl;coutvv先进学生类学生人数n«v_GoodStudent.size«endl;cout”先进学生干部类学生人数n«v_GoodStudentCarde.size«endl;cout〈”请选取操作H«endl;四运营成果1增长元素操作文件F编辑E格式O10312125703121257weidezhi22weidezhi Studentcoinputer2203121258Studentzhangyan20coinput erStudent03121258coinput erzhangyan0312125620qiyuzhi22Student Studentcoinputercoinput er前后对比爽迎来到学生管理系统,请选择要进行的操作删除型入要举字百生干部型阳的三,型膏件日类类却昂二生类||翻舞统计人手依次输入学生干部类的学号,姓名,年龄,职务,学院:3121256qiyuzhi22Student computer撬解查看2查询元素操作来迎七祚厂U厂操V的【丁曲要择度的粢举部部K曲一生生生生簪询除第计墨统生类类类选生干要请输入要查询的学生干部类的对象的学号:入却型型03121256查询的学生干部的结果:ID03121256到naneqiyuzhiage22functionStudenthcademycomputer情选择操作删除元素,重组文献操作32TALTALtLJ4-654321321123AXTiw%lffw^文件F编辑E醒O03121257weidezhi2203121257weidezhi22Student computer03121258Student zhangyan20Student coinputercoinput er03121258zhangyan20-\oopnomeworK\ueoug\oopnomeworK.exeStudentcoinput er||031212561qiyuzhi222Studentcomputer4组i统计人数t类部型类类干叁部生生的莘选D入0312却12型56型lane qiyuzhiige22-unctionStudenticademycomputer4更新元素操作更新前后对比J4321文件F编辑E格式I文件F漏考E布103121257weidezhi0312125722weidezhi22StudentStudentcoinput er03121258coinput erzhangyan0312125820zhangyanStudent20computer03121256Studentqiyuzhicomputer2203121250StudentQYZ20computerSTUDENTCOMPUTER国迎来到学生管理系统.请选择要进行的操作12删除3士4重组文操作6统计人:4请胡人要选择的学生的类型类型3尤秀学生干部类1请输入要更新的学生干部类的学号03121256请输入要更新的属性03121250QVZ20STUDENT COMPUTER请选择操彳乍6记录人数五问题及解决类实现某些是比较基本,就是某些函数实现,在读取文献某些遇到了问题,由于设计思想是一行一行读取文献,一开始没有考虑到文献中回车键带来问题,当文献最后还存在着回车字符时候,如果不加以判断,就会读入一行空数据,数据添加到vector类型变量当中去就可以使容器长度发生变化,从而在写文献时候就会浮现多些状况,导致得不到预期成果解决问题办法就是如果读到一行字符串为空话就用continue语句跳过这次循环在元素删除操作中,一开始运用是下标遍历并运用erase删除办法你,但无法编译通过,日后查阅书籍才懂得删除操作是不可以运用下标遍历办法进行删除日后采用了迭代器办法遍历元素就可以了一开始在主函数中,由于没有运用循环,导致每次运营时候只可以做一次操作,十分不以便,日后在主函数中又加入了whilecin»a循环控制条件,每次运营就可以选取多次操作以便了操作部查询元素操作函数requiry输入要查询元素学号,遍历类型变量,当找到符合条件就输出vector删除元素操作函数delete输入想要删除学生学号,运用迭代器遍历类型变量,找到符合条件元素就运用vector函数进行删除erase更新操作函数update输入要更新学生学号以及要更新数据,遍历类型变量,找到符合条件元素就龙类vector中定义函数进行重置操作❖重组文献操作recombine函数将已经删除元素舍弃,并重写文献三源程序/*设计一种虚基类一一学生类,包括学号、姓名和年龄保护数据成员以及有关成员函数;由学生类派生出学生干部类,包括职务和学院保护数据成员以及有关成员函数;再由学生类派生出先进学生类;然后由学生干部类和先进学生类派生出先进学生干部类O设计一种运用文献解决方式实现对学校学生(涉及学生干部、先进学生和先进学生干部)进行管理,具备增长数据、更新数据、查询数据、删除数据以及重组文献功能(删除数据在记录中做删除标志,重组文献指在物理上删除有删除标志记录)此外规定分别记录这三类学生人数以及所有学生总数*/#include string#includeiostream#includefstream#includestdlib.h#includevectorusing namespacestd;〃编写学生类class Student{protected:string ID;string name;int age;public://Student;〃无参数构造函数〃〜Student;〃析构函数void setStudentstringa,string bjntc;〃输入函数void printStudent;〃输出函数string getStudent_ID;〃获取函数string getStudent_name;int getStudent_age;;〃编写学生干部类,继承自Student类class StudentCarde:virtual publicStudent{protected:string function;string academy;public://StudentCarde;//〜StudentCarde;void setStudentCardestringa,string b,int c,string d,string e;void printStudentCarde;void getStudentCarde;string getStudentCarde_function;string getStudentCarde_academy;;//先进学生类class GoodStudent:virtual publicStudent{public://GoodStudent;//〜GoodStudent;void sctGoodStudcntstringa,string b,int c;void printGoodStudent;void getGoodStudent;);〃先进学生干部类class GoodStudentCarde:public StudentCarde,public GoodStudent{public://GoodStudentCarde;//^GoodStudentCarde;void setGoodStudentCardestringa,string b,int c,string d,string e;void printGoodStudentCarde;;//定义三类学生各自vector,设定为全局变量vectorStudentCarde v_StudentCarde;vectorGoodStudent v_GoodStudent;vectorGoodStudentCarde vGoodStudentCarde;〃获取vector大小值//int v_StudentCarde_Size=0;//int v_GoodStudent_S ize=0;//int vGoodStudentCarde Size=0;//实现学生类setStudent函数void Student::setStudentstringa,string b,int c//实现学生类set函数ID=a;name=b;age=c;//实现学生类输出函数void Student::printStudentcout«nID:n«ID«endl;cout«nnamc:n«namc«cndl;cout«nage:n«age«endl;〃实现学生干部类有关函数void StudentCarde::setStudentCardestringa,string b,int c,stringd,string eID=a;name二b;age=c;functioned;academy=e;〃实现学生干部类StudentCardeprint函数void StudentCarde::printStudentCardeprintStudent;cout«nfunction:n«function«endl;cout«nacademy:H«academy«endl;〃实现先进学生类setGoodStudentvoid GoodStudent::setGoodStudentstringa,string b,int cID二a;name=b;age=c;〃实现先进学生printGoodStudent函数void GoodStudent::printGoodStudentprintStudent;//实现先进学生干部类void GoodStudcntCardc::sctGoodStudcntCardcstring a,string b,int c,string d,string cID=a;name二b;age=c;functioned;academy=e;〃实现学生类getStudent_ID函数string Student::getStudent_ID returnID;//实现学生类getStudent_namestring Student::getStudent_name returnname;〃实现学生类getStudent_age函数int Student::getStudent_age returnage;〃实现学生干部类getStudentCarde_function函数string StudentCarde::getStudentCarde_function returnfunction;}〃实现学生干部类getStudentCarde函数string StudentCarde::getStudentCarde_academy returnacademy;〃定义read函数,从文献中读出信息,并且存储在vector中void readinta。
个人认证
优秀文档
获得点赞 0