3.查找具体学生成绩(ID)
//查 void Teacher::FindStudentGrade() { cout << "请输入要查询的学生ID" << endl; string ID; cin >> ID; if (_stuinfo.find(ID) == _stuinfo.end()) { cout << "查无此人" << endl; } else { cout << "查询成功" << endl; cout << "-------------------------学生信息-------------------------" << endl; cout << " " << "学号" << " " << "姓名" << " " << "课程名" << " " << "平时成绩" << " " << "期末成绩" << " " << "总评成绩" << endl; //for (auto e : _stuinfo[ID]._stuscore) //{ // cout << ID << " " << _stuinfo[ID]._name << " " << e.first << " " << e.second[0] << " " << e.second[1] << " " << e.second[2] << endl; // cout << "----------------------------------------------------------" << endl; //} string pre; for (auto e : _stuinfo[ID]._stuscore) { if (pre != ID) { cout << ID << " " << _stuinfo[ID]._name << " "; pre = ID; } else { cout << " "; } cout << e.first << " "; printf("%f %f %f\n", e.second[0], e.second[1], e.second[2]); cout << "----------------------------------------------------------" << endl; } } system("pause"); system("cls"); }
4.删除具体学生信息(ID)
//删 void _DeleteGrede(map<string, student>& _stuinfo, string ID) { _stuinfo.erase(_stuinfo.find(ID)); } void Teacher::DeleteGrede() { cout << "请输入要删除的学生ID" << endl; string ID; cin >> ID; if (_stuinfo.find(ID) == _stuinfo.end()) { cout << "查无此人" << endl; } else { _DeleteGrede(_stuinfo, ID); cout << "删除成功" << endl; } system("pause"); system("cls"); }
5.修改具体学生信息(ID)
//改 void Teacher::ModifyStudentGrade() { cout << "请输入要修改的学生ID" << endl; string ID; cin >> ID; if (_stuinfo.find(ID) == _stuinfo.end()) { cout << "查无此人" << endl; } else { cout << "该学生信息如下:"; cout << "---------------------------学生信息---------------------------" << endl; cout << " " << "学号" << " " << "姓名" << " " << "课程名" << " " << "平时成绩" << " " << "期末成绩" << " " << "总评成绩" << endl; string pre; for (auto e : _stuinfo[ID]._stuscore) { if (pre != ID) { cout << ID << " " << _stuinfo[ID]._name << " "; pre = ID; } else { cout << " "; } cout << e.first << " "; printf("%f %f %f\n", e.second[0], e.second[1], e.second[2]); cout << "--------------------------------------------------------------" << endl; } cout << "是否需要修改?(1.修改/0.不修改)"; int n = 0; cin >> n; if (n) { _DeleteGrede(_stuinfo, ID);//删除学生信息 cout << "请重新输入学生信息" << endl; AddGrade();//新增学生信息 cout << "修改成功" << endl; } } system("pause"); system("cls"); }
6.文件保存
struct ConfigManager { public: ConfigManager(const char* filename) :_filename(filename) {} void WriteText(const map<string, student>& info, const string& crou,const string& less) { ofstream ofs(_filename); ofs << "-------------------------学生信息-------------------------" << endl; ofs << " " << "学号" << " " << "姓名" << " " << "课程名" << " " << "平时成绩" << " " << "期末成绩" << " " << "总评成绩" << endl; ofs << "----------------------------------------------------------" << endl; for (auto it : info) { if (it.second._lesson == less) { string pre; for (auto e : it.second._stuscore) { if (e.first == crou) { if (pre != it.second._name) { ofs << it.second._id << " " << it.second._name << " "; pre = it.second._name; } else { ofs << " "; } ofs << e.first << " " << e.second[0] << " " << e.second[1] << " " << e.second[2] << endl; ofs << "----------------------------------------------------------" << endl; } } } } } private: string _filename; // 配置文件 }; //保存至文件 void Teacher::Save() { //每个班级每门课程的成绩可以从学生的成绩总表提取出子表并存储成一个文本文件。该文本文件名由班级号 //和课程名拼音字符串构成。输入班级号和课程名字符串后自动生成文件名。如1班,语文,则文件名为:c1yuwen.txt。 string filename; string less, crou;//班级号,课程名 cout << "请输入班级号:>"; cin >> less; filename += 'c'; filename += less; int flag = 1; while (flag) { flag = 0; cout << "请输入课程名:>"; cin >> crou; if (crou == "语文") { filename += "yuwen"; } else if (crou == "数学") { filename += "shuxue"; } else if (crou == "英语") { filename += "yingyu"; } else if (crou == "物理") { filename += "wuli"; } else if (crou == "化学") { filename += "huaxue"; } else if (crou == "生物") { filename += "shengwu"; } else { cout << "输入课程名错误,请重新输入!" << endl; flag = 1; } } filename += ".txt"; ConfigManager file(filename.c_str()); file.WriteText(_stuinfo, crou,less); cout << "保存成功" << endl; system("pause"); system("cls"); }
五、界面菜单声明的函数的实现
1.主界面
void Menu() { cout << "***********************************************" << endl; cout << "********* 欢迎来到学生成绩管理系统! *********" << endl; cout << "********* 请选择您的身份 *********" << endl; cout << "********* 1.管理员 *********" << endl; cout << "********* 2.老师 *********" << endl; cout << "********* 0.退出系统 *********" << endl; cout << "***********************************************" << endl; }
2.管理员
void AdmMenu(){ cout << "*********************************" << endl; cout << "********* 1、添加老师 *********" << endl; cout << "********* 2、浏览老师 *********" << endl; cout << "********* 3、查询老师 *********" << endl; cout << "********* 4、删除老师 *********" << endl; cout << "********* 0、退出系统 *********" << endl; cout << "*********************************" << endl; }
3.老师
void TeachMenu() { cout << "*************************************" << endl; cout << "********* 1、学生成绩录入 *********" << endl; cout << "********* 2、浏览学生成绩 *********" << endl; cout << "********* 3、查询学生成绩 *********" << endl; cout << "********* 4、删除学生成绩 *********" << endl; cout << "********* 5、修改学生成绩 *********" << endl; cout << "********* 6、保存学生数据 *********" << endl; cout << "********* 0、退出系统 *********" << endl; cout << "*************************************" << endl; }
六、主函数
enum oper//操作的枚举 { ADD = 1, LOOK, FIND, DELETE, MODIFY, SAVE, }; int main() { while (1) { Menu(); int n = 0; cin >> n; system("cls"); if (n == 1)//管理员 { cout << "**********************管理员**********************" << endl; cout << "管理员您好,请输入您的姓名:>" << endl; string name,id; cin >> name; cout << "请输入您的密码:>" << endl; cin >> id; system("cls"); if (id != PASSWARD) { cout << "**********************管理员**********************" << endl; cout << "密码有误,请重新登录!" << endl; } else { Admini adm(name); int input = 1; while (input) { cout << "**********************管理员**********************" << endl; AdmMenu(); cout << "请选择您要进行的操作:>" << endl; cin >> input; system("cls"); cout << "**********************管理员**********************" << endl; switch (input) { case ADD: adm.AddTeacher(); break; case LOOK: adm.LookTeacher(); break; case FIND: adm.FindTeacher(); break; case DELETE: adm.DelTeacher(); break; case 0: break; default: cout << "非法输入!" << endl; break; } } } } else if (n == 2)//老师 { cout << "**********************老师**********************" << endl; cout << "老师您好,请输入您的姓名:>" << endl; string name, id; cin >> name; cout << "请输入您的密码:>" << endl; cin >> id; system("cls"); if (id != PASSWARD) { cout << "**********************老师**********************" << endl; cout << "密码有误,请重新登录!" << endl; } else { Teacher tch(name,id); int input = 1; while (input) { cout << "**********************老师**********************" << endl; TeachMenu(); cout << "请选择您要进行的操作:>" << endl; cin >> input; system("cls"); cout << "**********************老师**********************" << endl; switch (input) { case ADD: tch.AddGrade(); break; case LOOK: tch.LookGrade(); break; case FIND: tch.FindStudentGrade(); break; case DELETE: tch.DeleteGrede(); break; case MODIFY: tch.ModifyStudentGrade(); break; case SAVE: tch.Save(); break; case 0: break; default: cout << "非法输入!" << endl; break; } } } } else if(n == 0)//退出系统 { break; } else { cout << "非法输入!" << endl; } system("cls"); } return 0; }
、
总结
以上就是今天要讲的内容,本文主要介绍了用C++实现的一个学生成绩信息管理系统,主要介绍它所实现功能以及具体实现的代码,大家可以根据代码自行此时。本文作者目前也是正在学习C++相关的知识,如果文章中的内容有错误或者不严谨的部分,欢迎大家在评论区指出,也欢迎大家在评论区提问、交流。
最后,如果本篇文章对你有所启发的话,希望可以多多支持作者,谢谢大家!