时间:2022-06-24 09:00:20 | 栏目:C代码 | 点击:次
本文实例为大家分享了C++实现评教管理系统的具体代码,供大家参考,具体内容如下
#include<iostream> #include<fstream> #include<cstring> #include <stdlib.h> #include <conio.h> #include<vector> using namespace std; int n; class Student {public: char sname[20]; char snum[20]; char sclass[20]; void input(); }; /*--------------------------学生登陆-----------------------------*/ void Student::input() { cout<<"请输入您的姓名:"; cin>>sname; cout<<"请输入您的学号:"; cin>>snum; cout<<"请输入您的班级:"; cin>>sclass; cout<<"登陆成功!输入任意字符继续"; getch(); } /*--------------------------评教信息 类模板-----------------------------*/ template<typename T1,typename T2,typename T3> class Lesson {public: Lesson(){}; ~Lesson(){}; T1 tname[20]; T2 tlesson[20]; T3 grade; void display(); void input(); void show(); }; /*--------------------------输出函数-----------------------------*/ template<typename T1,typename T2,typename T3> void Lesson<T1,T2,T3>::show() { cout<<"教师姓名:"<<tname<<endl; cout<<"课程名称:"<<tlesson<<endl; cout<<"评教分数:"<<grade<<endl; } /*--------------------------新建类-----------------------------*/ class System {public: System(){}; ~System(){}; void add(); int lname(char *x); void find(); void change(); void dele(); void menu(); void display(); void save(); void read(); }; vector<Lesson<char,char,double> >v; /*--------------------------输入函数-----------------------------*/ template<typename T1,typename T2,typename T3> void Lesson<T1,T2,T3>::input() { cout<<"请输入教师姓名:"; cin>>tname; cout<<"请输入课程名称:"; cin>>tlesson; cout<<"请输入打分(0-100):"; cin>>grade; try{ //异常处理 if(grade<0||grade>100) throw 0; else cout<<endl;} catch(int) {cout<<"输入数据错误,请重新输入";} cout<<endl; } /*--------------------------读取函数-----------------------------*/ void System::read() { Lesson<char,char,double> ch; ifstream infile("sys.txt",ios::binary); if(!infile){ cout<<"文件打不开"<<endl; } infile.read((char*)&ch,sizeof(ch)); while(!infile.eof()) { v.push_back(ch); infile.read((char*)&ch,sizeof(ch)); cout<<"教师姓名:"<<ch.tname<<endl; cout<<"课程名称:"<<ch.tlesson<<endl; cout<<"评教分数:"<<ch.grade<<endl; cout<<endl; } infile.close(); cout<<"读取成功"<<endl; getch(); } /*--------------------------保存函数-----------------------------*/ void System::save() { ofstream outfile("sys.txt",ios::binary); Lesson<char,char,double>t; vector<Lesson<char,char,double> >::iterator ap; if(!outfile) cout<<"文件不能被打开"<<endl; else { for(ap=v.begin();ap!=v.end();ap++) { t=*ap; outfile.write((char*)&t,sizeof(t)); cout<<"保存成功"<<endl; } outfile.close(); getch(); } } void System::display() { for(int i=0;i<n;i++) {v[i].show();} cout<<"输入任意字符继续"; getch(); } /*--------------------------主菜单-----------------------------*/ void System::menu() { cout<<"--------------*评 教 管 理 系 统*-------------"<<endl; cout<<"--------------*$1. 学生登陆界面*--------------"<<endl; cout<<"--------------*$2. 增加评教信息*--------------"<<endl; cout<<"--------------*$3. 更改评教信息*--------------"<<endl; cout<<"--------------*$4. 查找评教信息*--------------"<<endl; cout<<"--------------*$5. 显示评教信息*--------------"<<endl; cout<<"--------------*$6. 删除评教信息*--------------"<<endl; cout<<"--------------*$7. 保存相关数据*--------------"<<endl; cout<<"--------------*$8. 读取相关数据*--------------"<<endl; cout<<"--------------*$9. 退出评教系统*--------------"<<endl; } /*--------------------------添加数据-----------------------------*/ void System::add() { string h="y"; do{ Lesson<char,char,double>l; l.input(); n++; v.push_back(l); cout<<"是否继续添加(y/n)?"; cin>>h; }while(h=="y"); cout<<"添加成功!输入任意字符继续"; getch(); } /*--------------------------查找课程-----------------------------*/ int System::lname(char *x) { int i; for(i=0;i<n;i++) { if(strcmp(v[i].tlesson,x)==0) { return i;} } return -1; } void System::find() { char x[20]; int a; cout<<"请输入要查找的课程名称:"; cin>>x; a=lname(x); if(a!=-1) v[a].show(); else cout<<"没有找到相关课程信息"<<endl; cout<<"输入任意字符继续"; getch(); } /*--------------------------更改数据-----------------------------*/ void System::change() { char b[20]; int k; cout<<"请输入需要修改的课程名称:"; cin>>b; k=lname(b); if(k!=-1){ cout<<"已找到,请输入新的信息:"<<endl; v[k].input();} cout<<"输入任意字符继续:"; getch(); } /*--------------------------删除数据-----------------------------*/ void System::dele() { char c[20]; int d; cout<<"请输入要删除的课程名称:"; cin>>c; d=lname(c); if(d!=-1) {for(;d<n-1;d++) {strcpy(v[d].tname,v[d+1].tname); strcpy(v[d].tlesson,v[d+1].tlesson); v[d].grade==v[d+1].grade; } n--; } else cout<<"输入错误,找不到该课程信息"<<endl; cout<<"输入任意字符继续"; getch(); } /*--------------------------主函数-----------------------------*/ int main() { Student x; System y; int a; string ss="y"; do{ system("cls"); cout<<"====================欢迎进入评教管理系统!===================="<<endl; y.menu() ; cout<<"请输入相应阿拉伯数字:"; cin>>a; switch(a) { case 1:x.input();break; case 2:y.add();break; case 3:y.change();break; case 4:y.find();break; case 5:y.display();break; case 6:y.dele();break; case 7:y.save();break; case 8:y.read();break; case 9:ss="n";break; } }while(ss=="y"); return 0; }