先贴一个需求图片:
如上图所示,设计几个类,如下图所示:
有五个类,分别是:Admin
、Lesson
、Student
、Stu_Score
、Teacher
,然后预置三个文件:lesson.txt
、student.txt
、teacher.txt
,文件中以英文的逗号(,)间隔每组数据
。
三个文件中:
lesson.txt
:第一行存储课程信息,第一行及后N行存储报名该课程的信息,如下图所示:
teacher.txt
:存储老师工号、无其他内容student.txt
:存储学生的所有信息
注:后两个文件,并没有太多存储信息,主要信息都存储在了lesson.txt
先看一下,最终效果(小视频):
bandicam 2019
上代码(快900行,很长……)
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <fstream>
#include <Windows.h>
using namespace std;
#define MAXStu 1000 //学生的最大人数
#define MAXLes 1000 //课程的最大门数
#define MAXTea 1000 //老师的最大人数
fstream teacher,student,lesson;
//学生成绩类,目的是为了嵌套在课程类数组里面
class Stu_Score{
public:
string SID; //学生学号
string SCollege; //学生所在学院
string SLevel; //学生成绩等级
int SAlwScr; //学生平时成绩
int SEndScr; //学生期末成绩
float S_Scr; //学生最终成绩
Stu_Score(){SAlwScr=0;SEndScr=0;S_Scr=0;SLevel="不及格";} //学生成绩类构造函数
string GetSID() const; //得到学生学号
string GetSCollege() const; //得到学生所在学院
string GetSLevel(); //得到学生成绩等级
float GetS_Scr(); //得到学生最终成绩
};
//课程类 该类包含了课程的一切内容和学生的成绩
class Lesson{
bool flag; //表示着当前课程是否开启,如果开启则为true
string LID; //课号
string LGread; //开课年级
string LName; //课程名称
string LTec; //开课教师姓名
string LCollege; //开课学院
int Top; //表示学这门课的人数
public:
Stu_Score LStu[MAXStu]; //这门课的学生成绩
Lesson(){Top=0;flag=false;} //构造函数,代表这门课开始的时候,有0个人,flag等于false,代表这门课没有开启
int GetTop() const; //得到学这么课的人数
string GetLID() const; //得到课号
string GetLGread() const; //得到课程开设年级
string GetLName() const; //得到课程名字
string GetLTec() const; //得到课程教师
string GetLCollege() const; //得到课程开设学院
void Put(string &Lid,string &Lname,string &Lgread,string &Ltec,string &Lcollege);//新增课程
void Close(); //关闭课程
void Open(); //开启课程
bool IsOpen(); //判断是否为开启状态
void PutStu(string &sid,string &scollege); //该课程新增一个学生
void Pre(); //预处理
void Save(); //存盘
};
int TTop = 0; //课程类的高度
Lesson TotLes[MAXLes]; //对象数组,表示所有的课程
//学生类 包含学生的主要信息,还有各个通道
class Student{
string StuID; //学生学号
string StuCollege; //学生所在学院
string StuGread; //学生年级
bool open; //该位置是否有学生
public:
Student(){open=false;} //构造函数,每次调用这个位置为关闭状态
string GetStuId() const; //得到学生学号
string GetStuCollege() const; //得到学生所在学院
string GetStuGread() const; //得到学生所在年级
void Put(string &stuid,string &stucoll,string &stugread); //增加一个学生
void InputStuID(); //输入学生学号
void PrintStuMenu(); //打印学生类菜单
void ViewMyLesson(); //查看自己报名的课程
void SignUpLesson(); //报名课程
void SeeMyScore(); //查看自己的成绩
bool IsOpne(); //查看是否开启
void Open(); //开启
void Close(); //关闭该学生
void Pre(); //预处理,把文件的内容读出来
void Save(); //存盘
};
int StuTop = 0;
Student TotStu[MAXStu]; //对象数组,存储学生的信息
//教师类
class Teacher{
string TeaID; //老师工号
bool open; //标志着开启
public:
Teacher(){open=false;} //构造函数,每次调用这个位置为关闭状态
string GetTeaID() const; //得到教师工号
void InputTeaID(); //输入老师工号
void PrintTeaMenu(); //打印老师类菜单
void MakeLesson(); //增加课程
void EscLesson(); //取消课程
void SeeMyLesson(); //查看自己开设的课程
void InputStuScr(); //录入学生平时成绩
void SeeMyStuScr(); //查看自己学生的成绩分布
void Put(string &teaid); //放置一个老师
bool IsOpen(); //是否开启
void Open(); //开启
void Close(); //关闭
void Pre(); //预处理
void Save(); //存盘
};
int TeaTop = 0;
Teacher TotTea[MAXTea]; //对象数组、存储老师的信息
//管理员类
class Admin{
string MyID; //管理员ID
public:
string GetMyID() const; //得到ID
void PrintAdminMenu(); //打印管理员界面
void AddStu(); //添加学生
void DelStu(); //删除学生
void AddTea(); //添加老师
void DelTea(); //删除老师
void AddLesson(); //添加课程
void DelLesson(); //删除课程
};
/*---------------------------------------------------------------------------------------------------------------*/
//管理员Admin类
string Admin::GetMyID() const{return MyID;}
void Admin::AddStu(){
//添加学生
string stuid,stucoll,stugread;
cout << "请输入学生的学号:"; cin >> stuid;
cout << "请输入学生所属学院:";cin >> stucoll;
cout << "请输入学生年级:";cin >> stugread;
//查找适合的位置
int tmp = StuTop;
for(int i = 0 ; i < StuTop ; i ++ )
if(!TotStu[i].IsOpne()){
tmp = i;
break;
}
TotStu[tmp].Put(stuid,stucoll,stugread);
TotStu[tmp].Open();
if(tmp==StuTop) StuTop++;
cout << "添加成功!\n";
system("pause");
}
void Admin::DelStu(){
//删除学生
string str;
cout << "请输入你想删除的学生学号:";
cin >> str;
bool flag = false;
for(int i = 0 ; i < StuTop ; i ++ ){
if(str==TotStu[i].GetStuId()){
flag = true;
cout << "你确定要删除该学生吗?(Y/N)\n";
char c;cin >> c;
if(c=='Y'||c=='y'){
TotStu[i].Close(); //关闭对象数组、该位置的元素
cout << "已经成功删除!\n";
}
else
cout << "不删除该学生是睿智的选择!\n";
break;
}
}
if(!flag) cout << "没有查找到对应学号的学生!\n";
system("pause");
}
void Admin::AddTea(){
cout << "请输入老师的工号:";
string str;
cin >> str;
int tmp = TeaTop;
for(int i = 0 ; i < TeaTop ; i ++ )
if(!TotTea[i].IsOpen()){
tmp=i;
break;
}
TotTea[tmp].Put(str);
TotTea[tmp].Open();
if(tmp==TeaTop) TeaTop++;
cout << "添加成功!\n";
system("pause");
}
void Admin::DelTea(){
cout << "请输入想要删除的老师工号:";
string str; cin >> str;
bool flag = false;
for(int i = 0 ; i < TeaTop ; i ++ )
if(TotTea[i].GetTeaID()==str){
flag = true;
cout << "你确定要删除该老师吗?(注意:删除该老师,该老师的课程也会随之被删除)(Y/N)\n";
char c; cin >> c;
if(c=='Y'||c=='y'){
//遍历课程类数组,删除该老师的所有开设的课程
for(int j = 0 ; j < TTop ; j ++ ){
if(TotLes[j].GetLTec()==str)
TotLes[j].Close();
}
TotTea[i].Close(); //关闭老师类数组这个位置
cout << "删除成功!\n";
}else
cout << "睿智的选择!\n";
break;
}
system("pause");
}
void Admin::AddLesson(){
//添加课程void Put(string &Lid,string &Lname,string &Lgread,string &Ltec,string &Lcollege);//新增课程
string lid,lname,lgread,ltec,lcollege;
cout << "输入课号:"; cin >> lid;
cout << "输入课程名称:"; cin >> lname;
cout << "输入课程开设年级:"; cin >> lgread;
cout << "输入课程任课教师工号:"; cin >> ltec;
cout << "输入课程归属学院:"; cin >> lcollege;
int tmp = TTop;
for(int i = 0 ; i < TTop ; i ++ )
if(!TotLes[i].IsOpen()){
tmp = i;
break;
}
TotLes[tmp].Put(lid,lname,lgread,ltec,lcollege);
TotLes[tmp].Open();
if(tmp==TTop) TTop++;
cout << "添加课程成功!\n";
system("pause");
}
void Admin::DelLesson(){
//删除课程
cout << "请输入你想删除的课程课号:";
string str; cin >> str;
bool flag = false; //用来标记是否查找到相应的课号
for(int i = 0 ; i < TTop ; i ++ )
if(TotLes[i].GetLID()==str){
flag = true;
cout << "你确定删除该课程吗?(Y/N)\n";
char c; cin >> c;
if(c=='Y'||c=='y'){
TotLes[i].Close();
cout << "删除成功!\n";
}else
cout << "不删除课程是睿智的选择!\n";
break;
}
system("pause");
}
void Admin::PrintAdminMenu(){
//打印管理员界面菜单
cout << "\t\t\t\t***********************************************\n"
<< "\t\t\t\t***********************************************\n"
<< "\t\t\t\t** 管理员界面 **\n"
<< "\t\t\t\t** 1、添加学生 **\n"
<< "\t\t\t\t** 2、删除学生 **\n"
<< "\t\t\t\t** 3、添加老师 **\n"
<< "\t\t\t\t** 4、删除老师 **\n"
<< "\t\t\t\t** 5、添加课程 **\n"
<< "\t\t\t\t** 6、删除课程 **\n"
<< "\t\t\t\t** 7、返回主界面 **\n"
<< "\t\t\t\t***********************************************\n";
cout << "\n\t\t\t\t请选择:";
}
/*---------------------------------------------------------------------------------------------------------------*/
//教师Teacher类函数
string Teacher::GetTeaID() const{return TeaID;}
void Teacher::Put(string &teaid){TeaID=teaid;}
bool Teacher::IsOpen(){return open;}
void Teacher::Open(){open=true;}
void Teacher::Close(){open=false;}
void Teacher::InputTeaID(){
//输入老师工号
string str;
cin >> str;
TeaID = str;
}
void Teacher::PrintTeaMenu(){
//打印主菜单
cout << "\t\t\t\t***********************************************\n"
<< "\t\t\t\t***********************************************\n"
<< "\t\t\t\t** 教师界面 **\n"
<< "\t\t\t\t** 1、增加课程 **\n"
<< "\t\t\t\t** 2、删除课程 **\n"
<< "\t\t\t\t** 3、查看自己开设的课程 **\n"
<< "\t\t\t\t** 4、录入成绩 **\n"
<< "\t\t\t\t** 5、查看学生总成绩 **\n"
<< "\t\t\t\t** 6、返回主页面 **\n"
<< "\t\t\t\t***********************************************\n";
cout << "\n\t\t\t\t请选择:";
}
void Teacher::MakeLesson(){
//增加课程
int tmp; //存储适当的位置
//找到课程数组开设的合适的位置
for(int i = 0 ; i < TTop ; i ++ )
if(!TotLes[i].IsOpen())
tmp;
if(tmp>=TTop) TTop++; //如果适当的位置比TTop大,那么TTop加一
string lid,lname,ltecid,lcollege,lgread;
cout << "请输入您想申请开课的课程课号:";cin >> lid;
cout << "请输入课程名称:";cin >> lname;
cout << "请输入课程所归属系:";cin >> lcollege;
cout << "请输入课程适合的年级:";cin >> lgread;
ltecid = TeaID;
TotLes[tmp].Put(lid,lname,lgread,ltecid,lcollege);
cout << "课程添加成功!\n";
system("pause");
}
void Teacher::EscLesson(){
//取消课程
SeeMyLesson();
string str;
cout << "请输入您想要取消的课程课号:";
cin >> str;
for(int i = 0 ; i < TTop ; i ++ )
//如果课号相等并且是自己开的课,那么可以执行循环体操作!
if(TotLes[i].GetLTec() == TeaID && str == TotLes[i].GetLID()){
cout << "你确定要取消该课程吗?(Y/N)\n";
char c;
cin >> c;
if(c=='y'||c=='Y'){
TotLes[i].Close(); //关闭该课程
cout << "删除成功!\n";
}
else
cout << "慎重选择!你值得拥有!\n";
break;
}
system("pause");
}
void Teacher::InputStuScr(){
//录入学生平时成绩
SeeMyLesson();
cout << "请输入您想要录入的课程的课号:";
string str; cin >> str;
int tmp=-1; //记录课程编号
for(int i = 0 ; i < TTop ; i ++ ){ //查找到想录入课程的课号
if(TotLes[i].GetLID()==str && TotLes[i].GetLTec() == TeaID){
tmp=i;
break;
}
}
if(tmp==-1)
cout << "没有找到您输入的课号!\n";
else{
cout << "该课程共有" << TotLes[tmp].GetTop() << "个学生,下面开始录入成绩!\n";
for(int i = 0 ; i < TotLes[tmp].GetTop() ; i ++ ){ //循环每个学生
cout << "学生学号:" << TotLes[tmp].LStu[i].GetSID() << "\t学生所属学院:" << TotLes[tmp].LStu[i].GetSCollege() <<endl;
cout << "请输入该学生的平时成绩:"; cin >> TotLes[tmp].LStu[i].SAlwScr;
cout << "请输入该学生的期末成绩:"; cin >> TotLes[tmp].LStu[i].SEndScr;
cout << "经过计算所得,该学生的总成绩为:" << TotLes[tmp].LStu[i].GetS_Scr() << endl;
cout << "-----------------------------------------------\n\n";
}
cout << "录入完毕!返回主界面!\n";
}
system("pause");
}
void Teacher::SeeMyLesson(){
//查看该老师开设的课程
cout << "以下是您开设的选修课程:\n";
for(int i = 0 ; i < TTop ; i ++ ){
if(TotLes[i].GetLTec() == TeaID){
cout << "课号:" << TotLes[i].GetLID() << "\t课程名称:" << TotLes[i].GetLName() << endl;
}
}
system("pause");
}
void Teacher::SeeMyStuScr(){
//老师查看学生成绩分布
SeeMyLesson(); //先列出自己的课程
cout << "请选择您想要查看学生成绩分布的课程课号:";
string str; cin >> str;
int tmp=-1; //记录课程课号
for(int i = 0 ; i < TTop ; i ++ ){
if(str==TotLes[i].GetLID()&&TeaID==TotLes[i].GetLTec()){
tmp=i;
break;
}
}
//鲁棒性
if(tmp==-1)
cout << "您输入的课号不正确!\n";
else{
//开始展示成绩分布图表:(分五次显示)
cout << "下面是学生成绩分布图表:\n\n\n" ;
int sum=TotLes[tmp].GetTop(); //得到报该课程的总人数
int a[MAXLes],at=0; //记录未录入成绩的学生名单
int you=0,liang=0,zhong=0,jige=0,bujige=0; //统计每个段的学生人数算百分比
cout << "\n-------------------------------------------------------------------\n";
cout << "学生学号 | 学生平时成绩 | 学生期末成绩 | 学生总成绩 | 学生成绩等级 |\n";
for(int i = 0 ; i < sum ; i ++ ){
if(TotLes[tmp].LStu[i].GetSLevel()=="优")
cout << TotLes[tmp].LStu[i].GetSID() << " | "
<< TotLes[tmp].LStu[i].SAlwScr << " | "
<< TotLes[tmp].LStu[i].SEndScr << " | "
<< TotLes[tmp].LStu[i].GetS_Scr() << " | "
<< TotLes[tmp].LStu[i].GetSLevel() << " |\n",you++;
else if(TotLes[tmp].LStu[i].SAlwScr==0&&TotLes[tmp].LStu[i].SEndScr==0)
a[at++]=i;
}
cout << "\n-------------------------------------------------------------------\n";
cout << "优秀人数占该课程全部人数的百分比:" << (float)you/(float)sum*100 << "%\n\n";
cout << "\n-------------------------------------------------------------------\n";
cout << "学生学号 | 学生平时成绩 | 学生期末成绩 | 学生总成绩 | 学生成绩等级 |\n";
for(int i = 0 ; i < sum ; i ++ ){
if(TotLes[tmp].LStu[i].GetSLevel()=="良")
cout << TotLes[tmp].LStu[i].GetSID() << " | "
<< TotLes[tmp].LStu[i].SAlwScr << " | "
<< TotLes[tmp].LStu[i].SEndScr << " | "
<< TotLes[tmp].LStu[i].GetS_Scr() << " | "
<< TotLes[tmp].LStu[i].GetSLevel() << " |\n",liang++;
else if(TotLes[tmp].LStu[i].SAlwScr==0&&TotLes[tmp].LStu[i].SEndScr==0)
a[at++]=i;
}
cout << "\n-------------------------------------------------------------------\n";
cout << "良好人数占该课程全部人数的百分比:" << (float)liang/(float)sum*100 << "%\n\n";
cout << "\n-------------------------------------------------------------------\n";
cout << "学生学号 | 学生平时成绩 | 学生期末成绩 | 学生总成绩 | 学生成绩等级 |\n";
for(int i = 0 ; i < sum ; i ++ ){
if(TotLes[tmp].LStu[i].GetSLevel()=="中")
cout << TotLes[tmp].LStu[i].GetSID() << " | "
<< TotLes[tmp].LStu[i].SAlwScr << " | "
<< TotLes[tmp].LStu[i].SEndScr << " | "
<< TotLes[tmp].LStu[i].GetS_Scr() << " | "
<< TotLes[tmp].LStu[i].GetSLevel() << " |\n",zhong++;
else if(TotLes[tmp].LStu[i].SAlwScr==0&&TotLes[tmp].LStu[i].SEndScr==0)
a[at++]=i;
}
cout << "\n-------------------------------------------------------------------\n";
cout << "中等人数占该课程全部人数的百分比:" << (float)zhong/(float)sum*100 << "%\n\n";
cout << "\n-------------------------------------------------------------------\n";
cout << "学生学号 | 学生平时成绩 | 学生期末成绩 | 学生总成绩 | 学生成绩等级 |\n";
for(int i = 0 ; i < sum ; i ++ ){
if(TotLes[tmp].LStu[i].GetSLevel()=="及格")
cout << TotLes[tmp].LStu[i].GetSID() << " | "
<< TotLes[tmp].LStu[i].SAlwScr << " | "
<< TotLes[tmp].LStu[i].SEndScr << " | "
<< TotLes[tmp].LStu[i].GetS_Scr() << " | "
<< TotLes[tmp].LStu[i].GetSLevel() << " |\n",jige++;
else if(TotLes[tmp].LStu[i].SAlwScr==0&&TotLes[tmp].LStu[i].SEndScr==0)
a[at++]=i;
}
cout << "\n-------------------------------------------------------------------\n";
cout << "及格人数占该课程全部人数的百分比:" << (float)jige/(float)sum*100 << "%\n\n";
cout << "\n-------------------------------------------------------------------\n";
cout << "学生学号 | 学生平时成绩 | 学生期末成绩 | 学生总成绩 | 学生成绩等级 |\n";
for(int i = 0 ; i < sum ; i ++ ){
if(TotLes[tmp].LStu[i].GetSLevel()=="不及格")
cout << TotLes[tmp].LStu[i].GetSID() << " | "
<< TotLes[tmp].LStu[i].SAlwScr << " | "
<< TotLes[tmp].LStu[i].SEndScr << " | "
<< TotLes[tmp].LStu[i].GetS_Scr() << " | "
<< TotLes[tmp].LStu[i].GetSLevel() << " |\n",bujige++;
else if(TotLes[tmp].LStu[i].SAlwScr==0&&TotLes[tmp].LStu[i].SEndScr==0)
a[at++]=i;
}
cout << "\n-------------------------------------------------------------------\n";
cout << "不及格人数占该课程全部人数的百分比:" << (float)bujige/(float)sum*100 << "%\n\n";
if(at!=0){
cout << "共有" << at << "个学生的成绩没有录入,他们分别是:\n";
for(int i = 0 ; i < at ; i ++ )
cout << TotLes[tmp].LStu[a[i]].GetSID() << " ";
cout << endl;
}else
cout << "该门课程的学生成绩分布图表如上图所示!\n";
}
system("pause");
}
/*---------------------------------------------------------------------------------------------------------------*/
//学生Student类函数
string Student::GetStuId() const{return StuID;}
string Student::GetStuCollege() const{return StuCollege;}
string Student::GetStuGread() const{return StuGread;}
bool Student::IsOpne(){return open;}
void Student::Open(){open=true;}
void Student::Close(){open=false;}
void Student::Put(string &stuid,string &stucoll,string &stugread){
StuID=stuid;
StuCollege=stucoll;
StuGread=stugread;
}
void Student::PrintStuMenu(){
//打印学生界面菜单
cout << "\t\t\t\t***********************************************\n"
<< "\t\t\t\t***********************************************\n"
<< "\t\t\t\t** 学生界面 **\n"
<< "\t\t\t\t** 1、查看成绩 **\n"
<< "\t\t\t\t** 2、查看已报名课程 **\n"
<< "\t\t\t\t** 3、报名课程 **\n"
<< "\t\t\t\t** 4、返回主界面 **\n"
<< "\t\t\t\t***********************************************\n";
cout << "\n\t\t\t\t请选择:";
}
void Student::ViewMyLesson(){
//查看自己报名的课程
//外层循环 循环 所有的课程,查看每个课程里面是否有自己的ID
for(int i = 0 ; i < TTop ; i ++ ){
bool flag = false;
for(int j = 0 ; j < TotLes[i].GetTop() ; j ++ ){
if(TotLes[i].LStu[j].GetSID() == StuID){ //如果第i门课的第j个学生的学号等于当前类的学号
//就说明他报名了该课程,那么flag为true,跳出循环,执行下一次外层循环
flag = true;
break;
}
}
if(flag)
cout << TotLes[i].GetLID() << " " << TotLes[i].GetLName() << endl;
}
system("pause");
}
void Student::SignUpLesson(){
//报名课程
//和查找一样的思路,如果你没有报名这个课,并且他和你属于统一等级,那么你可以报名
int res = 0;
cout << "以下是对应你的年级你可以报名的课程:\n";
for(int i = 0 ; i < TTop ; i ++ ){
bool flag = true;
for(int j = 0 ; j < TotLes[i].GetTop() ; j ++ ){
if(TotLes[i].LStu[j].GetSID() == StuID){
flag = false;
break;
}
}
if(TotLes[i].GetLGread() == StuGread && flag)
cout << "课号:" << TotLes[i].GetLID() << "\t课程名字:" << cout << TotLes[i].GetLName() << endl,res++;
}
if(res==0) {
cout << "并没有符合你的课程你可以报名!\n";
system("pause");
return;
}
cout << "请输入你想报名课程的课号:";
string str;
cin >> str;
int tmp;
//找到对应课号的ID
for(int i = 0 ; i < TTop ; i ++ ){
if(TotLes[i].GetLID() == str){
tmp = i;
break;
}
}
//存储学生的信息到该课程的学生目录里面
TotLes[tmp].PutStu(StuID,StuCollege);
system("pause");
}
void Student::SeeMyScore(){
//查看自己的某门课的成绩
//同查找,如果自己报名该课就输出自己的成绩、等级等等内容
for(int i = 0 ; i < TTop ; i ++ ){
for(int j = 0 ; j < TotLes[i].GetTop() ; j ++ ){
if(TotLes[i].LStu[j].GetSID() == StuID){
cout << "课号:" << TotLes[i].GetLID() << "\t课程名称:" << TotLes[i].GetLName() << "\t" <<
"总分:" << TotLes[i].LStu[j].GetS_Scr() << "\t成绩等级:" << TotLes[i].LStu[j].GetSLevel() << endl;
break;
}
}
}
system("pause");
}
void Student::InputStuID(){
//输入学生的学号
string str;
cin >> str;
StuID = str;
}
/*---------------------------------------------------------------------------------------------------------------*/
//课程Lesson类函数
int Lesson::GetTop() const{return Top;}
string Lesson::GetLID() const{return LID;}
string Lesson::GetLGread() const{return LGread;}
string Lesson::GetLName() const{return LName;}
string Lesson::GetLTec() const{return LTec;}
string Lesson::GetLCollege() const{return LCollege;}
void Lesson::Open(){flag=true;}
bool Lesson::IsOpen(){return flag;}
void Lesson::Close(){flag=false;Top=0;} //课程关闭,Top清零
void Lesson::Put(string &Lid,string &Lname,string &Lgread,string &Ltec,string &Lcollege){
//为课程的每个属性赋值
LID=Lid;
LName=Lname;
LGread=Lgread;
LTec=Ltec;
LCollege=Lcollege;
Open();
}
void Lesson::PutStu(string &sid,string &scollege){
LStu[Top].SID = sid;
LStu[Top++].SCollege = scollege;
}
/*---------------------------------------------------------------------------------------------------------------*/
//学生成绩Stu_Score类函数
string Stu_Score::GetSID() const{return SID;}
string Stu_Score::GetSCollege() const{return SCollege;}
string Stu_Score::GetSLevel(){
//得到学生成绩等级
float res = SAlwScr*0.3+SEndScr*0.7;
if(res>=90&&res<=100) SLevel="优";
else if(res>=80&&res<90) SLevel="良";
else if(res>=70&&res<80) SLevel="中";
else if(res>=60&&res<70) SLevel="及格";
else SLevel="不及格";
return SLevel;
}
float Stu_Score::GetS_Scr(){
//得到学生总分
S_Scr = SAlwScr*0.3+SEndScr*0.7;
return S_Scr;
}
/*---------------------------------------------------------------------------------------------------------------*/
//打印主菜单
void PrintMainMenu(){
cout << "\t\t\t\t***********************************************\n"
<< "\t\t\t\t***********************************************\n"
<< "\t\t\t\t** 选修课管理系统 **\n"
<< "\t\t\t\t** 1、学生登录 **\n"
<< "\t\t\t\t** 2、老师登录 **\n"
<< "\t\t\t\t** 3、管理员登录 **\n"
<< "\t\t\t\t** 4、退出系统 **\n"
<< "\t\t\t\t***********************************************\n";
cout << "\n\t\t\t\t请选择:";
}
string UTF8ToGB(const char* str); //文件内容转化中文操作~
int main(){
Admin A;
bool flag;
string str;
int tmp;
Student s;
Teacher t;
Lesson l;
s.Pre();
t.Pre();
l.Pre();
while(1){
//system("cls");
PrintMainMenu();
int x,y;
cin >> x;
switch(x){
case 1:
cout << "请输入您的学号:";
cin >> str;
flag = false;
tmp=-1;
for(int i = 0 ; i < StuTop ; i ++ )
if(str==TotStu[i].GetStuId()){
tmp = i;
flag = true;
break;
}
if(flag)
while(1){
TotStu[tmp].PrintStuMenu();
cin >> y;
if(y==1) TotStu[tmp].SeeMyScore();
else if(y==2) TotStu[tmp].ViewMyLesson();
else if(y==3) TotStu[tmp].SignUpLesson();
else break;
}
else
cout << "没有该学号!请至管理员页面注册!\n";
break;
case 2:
cout << "请输入您的教职工号:";
cin >> str;
flag = false;
tmp=-1;
for(int i = 0 ; i < TeaTop ; i ++ )
if(str==TotTea[i].GetTeaID()){
tmp = i;
flag = true;
break;
}
if(flag)
while(1){
TotTea[tmp].PrintTeaMenu();
cin >> y;
if(y==1) TotTea[tmp].MakeLesson();
else if(y==2) TotTea[tmp].EscLesson();
else if(y==3) TotTea[tmp].SeeMyLesson();
else if(y==4) TotTea[tmp].InputStuScr();
else if(y==5) TotTea[tmp].SeeMyStuScr();
else break;
}
else
cout << "教职工号不存在,请至管理员界面注册!\n";
break;
case 3:
while(1){
A.PrintAdminMenu();
cin >> y;
if(y==1) A.AddStu();
else if(y==2) A.DelStu();
else if(y==3) A.AddTea();
else if(y==4) A.DelTea();
else if(y==5) A.AddLesson();
else if(y==6) A.DelLesson();
else break;
}
break;
case 4:
cout << "你确定要退出本系统吗?(Y/N)\n";
char c; cin >> c;
if(c=='y'||c=='Y'){
s.Save();
t.Save();
l.Save();
exit(0);
}
else cout << "明智的选择!\n";
break;
default:
cout << "选错了!重来!";
break;
}
system("pause");
}
return 0;
}
//转化中文
string UTF8ToGB(const char* str)
{
string result;
WCHAR *strSrc;
LPSTR szRes;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i + 1];
MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
//获得临时变量的大小
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new CHAR[i + 1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
result = szRes;
delete[]strSrc;
delete[]szRes;
return result;
}
void Student::Pre(){
//预处理文件内容到学生数组里面 StuTop TotStu[MAXStu];
student.open("student.txt");
string s;
while(getline(student,s)){
string line = UTF8ToGB(s.c_str()).c_str();
// cout << line << endl;
int ite = line.find(',');
TotStu[StuTop].StuID = line.substr(0,ite);
// cout << TotStu[StuTop].StuID << " ";
int ite1 = line.find_last_of(',');
TotStu[StuTop].StuCollege = line.substr(ite+1,ite1-ite-1);
// cout << TotStu[StuTop].StuCollege << " ";
TotStu[StuTop].StuGread = line.substr(ite1+1,line.size()-ite1);
// cout << TotStu[StuTop].StuGread << endl;
TotStu[StuTop].Open();
StuTop++;
// cout << StuTop << endl;
}
student.close();
}
void Lesson::Pre(){
//预处理文件内容到课程对象数组里面 TTop TotLes[MAXLes];
//这个比较麻烦
string s;
lesson.open("lesson.txt");
while(getline(lesson,s)){
string line = UTF8ToGB(s.c_str()).c_str();
// cout << line << endl;
int ite1 = line.find_first_of(',');
TotLes[TTop].LID = line.substr(0,ite1);
int ite2 = line.find(',',ite1+1);
TotLes[TTop].LName = line.substr(ite1+1,ite2-ite1-1);
int ite3 = line.find(',',ite2+1);
TotLes[TTop].LCollege = line.substr(ite2+1,ite3-ite2-1);
int ite4 = line.find(',',ite3+1);
TotLes[TTop].LTec = line.substr(ite3+1,ite4-ite3-1);
int ite5 = line.find(',',ite4+1);
TotLes[TTop].LGread = line.substr(ite4+1,ite5-ite4-1);
int ite6 = line.find(',',ite5+1);
string ss = line.substr(ite5+1,ite6-ite5-1);
int sum = 0;
for(int i = 0 ; i < ss.size() ; i ++ ){
sum=sum*10+(ss[i]-48);
}
TotLes[TTop].Top = sum;
int i = 0;
while(sum--){
string sss;
getline(lesson,sss);
string lines = UTF8ToGB(sss.c_str()).c_str();
int ii1 = lines.find(',');
TotLes[TTop].LStu[i].SID = lines.substr(0,ii1);
int ii2 = lines.find(',',ii1+1);
TotLes[TTop].LStu[i].SCollege = lines.substr(ii1+1,ii2-ii1-1);
int ii3 = lines.find(',',ii2+1);
string q = lines.substr(ii2+1,ii3-ii2-1);
int tott = 0;
for(int j = 0 ; j < q.size() ; j ++)
tott = tott*10 + (q[j]-48);
TotLes[TTop].LStu[i].SAlwScr = tott;
tott=0;
q.clear();
int ii4 = lines.find(',',ii3+1);
q = lines.substr(ii3+1,ii4-ii3-1);
for(int j = 0 ; j < q.size() ; j ++ )
tott = tott*10 +(q[j]-48);
TotLes[TTop].LStu[i].SEndScr = tott;
// cout << TotLes[TTop].LStu[i].SID << " " << TotLes[TTop].LStu[i].SCollege <<
// " " << TotLes[TTop].LStu[i].SAlwScr << " " << TotLes[TTop].LStu[i].SEndScr << endl;
i++;
}
// cout << ite2 << endl;
// cout << TotLes[TTop].LID << " " << TotLes[TTop].LName << " " << TotLes[TTop].LCollege
// << " " << TotLes[TTop].LTec << " " << TotLes[TTop].LGread << " " << TotLes[TTop].Top << endl;
TotLes[TTop].Open();
TTop++;
}
lesson.close();
}
void Teacher::Pre(){
//预处理文件到老师对象数组里面 TeaTop TotTea[MAXLea];
//存储老师的工号,课号通过Lesson类读取
teacher.open("teacher.txt");
string s;
while(getline(teacher,s)){
string line = UTF8ToGB(s.c_str()).c_str();
TotTea[TeaTop].TeaID = line;
TotTea[TeaTop++].Open();
}
teacher.close();
}
void Student::Save(){
//程序结束后,把所有东西存入文件 StuTop TotStu[MAXStu];
student.open("student.txt",ios::trunc | ios::out);
for(int i = 0 ; i < StuTop ; i ++ ){
if(TotStu[i].IsOpne()){
student << TotStu[i].StuID << "," << TotStu[i].StuCollege << "," << TotStu[i].StuGread << "\n";
}
}
student.close();
}
void Lesson::Save(){
//同上,存储课程对象数组到文件 TTop TotLes[MAXLes];
//这个文件比较麻烦,第一行存储课程信息,其后N行存储学生成绩信息
lesson.open("lesson.txt",ios::trunc|ios::out);
for(int i = 0 ; i < TTop ; i ++ ){
if(TotLes[i].IsOpen()){
lesson << TotLes[i].LID << "," << TotLes[i].LName << "," << TotLes[i].LCollege << ","
<< TotLes[i].LTec << "," << TotLes[i].LGread << "," << TotLes[i].Top <<"\n";
for(int j = 0 ; j < TotLes[i].Top ; j ++ ){
lesson << TotLes[i].LStu[j].SID << "," << TotLes[i].LStu[j].SCollege << ","
<< TotLes[i].LStu[j].SAlwScr << "," << TotLes[i].LStu[j].SEndScr << "\n";
}
}
}
lesson.close();
}
void Teacher::Save(){
//存储老师对象数组到文件中 TeaTop TotTea[MAXLea];
teacher.open("teacher.txt",ios::trunc|ios::out);
for(int i = 0 ; i < TeaTop ; i ++ ){
if(TotTea[i].IsOpen()){
teacher << TotTea[i].TeaID << "\n";
}
}
teacher.close();
}