程序代码:
#include<iostream>
#include<vector>
using namespace std;
#define MAX_NAME_LEN 30
#define COURSE1_NAME "课程1"
#define COURSE2_NAME "课程2"
#define COURSE3_NAME "课程3"
#define COURSE4_NAME "课程4"
#define COURSE5_NAME "课程5"
typedef struct tagSTUDENT
{
unsigned int uNo;//学号
char szName[MAX_NAME_LEN]; //姓名
float fCourse1;
float fCourse2;
float fCourse3;
float fCourse4;
float fCourse5;
float fAvg;
float fTotal;
tagSTUDENT()
{
memset(this,0,sizeof(tagSTUDENT));
}
}STUDENT,*LPSTUDENT;
class CStudent
{
private:
STUDENT m_stu;
static int m_iTotal;//学生总人数
static int m_iCourse1TotalPass;//课程1及格人数
static int m_iCourse2TotalPass;
static int m_iCourse3TotalPass;
static int m_iCourse4TotalPass;
static int m_iCourse5TotalPass;
static float m_fCourse1MinScore;//课程1最低分
static float m_fCourse2MinScore;
static float m_fCourse3MinScore;
static float m_fCourse4MinScore;
static float m_fCourse5MinScore;
static char m_szNameOfMinScore[5][MAX_NAME_LEN];//最低分学生名字
static float m_fCourse1MaxScore;//课程1最高分
static float m_fCourse2MaxScore;
static float m_fCourse3MaxScore;
static float m_fCourse4MaxScore;
static float m_fCourse5MaxScore;
static char m_szNameOfMaxScore[5][MAX_NAME_LEN];//最高分学生名字
static float m_fCourse1TotalScore;//课程1总分
static float m_fCourse2TotalScore;
static float m_fCourse3TotalScore;
static float m_fCourse4TotalScore;
static float m_fCourse5TotalScore;
public:
CStudent(unsigned int no,char * name,float c1,float c2,float c3,float c4,float c5)
{
if( c1< 0.0f||c1 > 100.0f
||c2< 0.0f||c2 > 100.0f
||c3< 0.0f||c3 > 100.0f
||c4< 0.0f||c4 > 100.0f
||c5< 0.0f||c5 > 100.0f
)
{
throw "分数有误";
}
m_iTotal++;
m_stu.uNo = no;
strcpy_s(m_stu.szName,MAX_NAME_LEN,name);
m_stu.fCourse1 = c1;
m_stu.fCourse2 = c2;
m_stu.fCourse3 = c3;
m_stu.fCourse4 = c4;
m_stu.fCourse5 = c5;
m_stu.fTotal = c1 + c2 + c3 + c4 + c5;
m_stu.fAvg = m_stu.fTotal / 5;
if(m_fCourse1MinScore>c1)
{
m_fCourse1MinScore=c1;
strcpy_s(m_szNameOfMinScore[0],MAX_NAME_LEN,name);
}
if(m_fCourse2MinScore>c2)
{
m_fCourse2MinScore=c2;
strcpy_s(m_szNameOfMinScore[1],MAX_NAME_LEN,name);
}
if(m_fCourse3MinScore>c3)
{
m_fCourse3MinScore=c3;
strcpy_s(m_szNameOfMinScore[2],MAX_NAME_LEN,name);
}
if(m_fCourse4MinScore>c4)
{
m_fCourse4MinScore=c4;
strcpy_s(m_szNameOfMinScore[3],MAX_NAME_LEN,name);
}
if(m_fCourse5MinScore>c5)
{
m_fCourse5MinScore=c5;
strcpy_s(m_szNameOfMinScore[4],MAX_NAME_LEN,name);
}
if(m_fCourse1MaxScore<c1)
{
m_fCourse1MaxScore = c1;
strcpy_s(m_szNameOfMaxScore[0],MAX_NAME_LEN,name);
}
if(m_fCourse2MaxScore<c2)
{
m_fCourse2MaxScore = c2;
strcpy_s(m_szNameOfMaxScore[1],MAX_NAME_LEN,name);
}
if(m_fCourse3MaxScore<c3)
{
m_fCourse3MaxScore = c3;
strcpy_s(m_szNameOfMaxScore[2],MAX_NAME_LEN,name);
}
if(m_fCourse4MaxScore<c4)
{
m_fCourse4MaxScore = c4;
strcpy_s(m_szNameOfMaxScore[3],MAX_NAME_LEN,name);
}
if(m_fCourse5MaxScore<c5)
{
m_fCourse5MaxScore = c5;
strcpy_s(m_szNameOfMaxScore[4],MAX_NAME_LEN,name);
}
if(c1>=60.0f)
m_iCourse1TotalPass++;
if(c2>=60.0f)
m_iCourse2TotalPass++;
if(c3>=60.0f)
m_iCourse3TotalPass++;
if(c4>=60.0f)
m_iCourse4TotalPass++;
if(c5>=60.0f)
m_iCourse5TotalPass++;
m_fCourse1TotalScore += c1;
m_fCourse2TotalScore += c2;
m_fCourse3TotalScore += c3;
m_fCourse4TotalScore += c4;
m_fCourse5TotalScore += c5;
}
bool QueryCourseScore(int course)//查询某门课程成绩
{
static char *szCourses[]={COURSE1_NAME,COURSE2_NAME,COURSE3_NAME,COURSE4_NAME,COURSE5_NAME};
static float *coursesScore[]={&m_stu.fCourse1,&m_stu.fCourse2,&m_stu.fCourse3,&m_stu.fCourse4,&m_stu.fCourse5};
if(course<0||course>=sizeof(szCourses)/sizeof(char*))
return false;
cout<<"\t"<<szCourses[course]<<":"<<*coursesScore[course]<<endl;
return true;
}
unsigned int GetNo(){return m_stu.uNo;}//获取学号
void QueryAvg()
{
cout<<"\t平均分:"<<m_stu.fAvg<<endl;
}
static void PrintCourses()
{
cout<<"==================================\n"
<<"\t 1 "<<COURSE1_NAME<<"\n"
<<"\t 2 "<<COURSE2_NAME<<"\n"
<<"\t 3 "<<COURSE3_NAME<<"\n"
<<"\t 4 "<<COURSE4_NAME<<"\n"
<<"\t 5 "<<COURSE5_NAME<<"\n"
<<"==================================\n";
}
bool QuerygGrade(int course)//等级
{
static char * szGrade[]={"优","良","中","差","不及格"};
static char *szCourses[]={COURSE1_NAME,COURSE2_NAME,COURSE3_NAME,COURSE4_NAME,COURSE5_NAME};
static float *coursesScore[]={&m_stu.fCourse1,&m_stu.fCourse2,&m_stu.fCourse3,&m_stu.fCourse4,&m_stu.fCourse5};
if(course<0||course>=sizeof(szCourses)/sizeof(char*))
return false;
int i=4;
if(*szCourses[course]>=90)
i=0;
else if(*szCourses[course]>=80)
i=1;
else if(*szCourses[course]>=70)
i=2;
else if(*szCourses[course]>=60)
i=3;
cout<<"\t"<<szCourses[course]<<":"<<szGrade[course]<<endl;
return true;
}
float GetScore(int course)
{
static float *coursesScore[]={&m_stu.fCourse1,&m_stu.fCourse2,&m_stu.fCourse3,&m_stu.fCourse4,&m_stu.fCourse5};
return *coursesScore[course];
}
static void PrintCourseAvg(int course)//平均分
{
static char *szCourses[]={COURSE1_NAME,COURSE2_NAME,COURSE3_NAME,COURSE4_NAME,COURSE5_NAME};
static float *totalScore[]={&m_fCourse1TotalScore,&m_fCourse2TotalScore,&m_fCourse3TotalScore,&m_fCourse4TotalScore,&m_fCourse5TotalScore};
if(m_iTotal ==0)
throw "无学生成绩信息";
cout<<"\t"<<szCourses[course]<<"平均成绩:"<<*totalScore[course]/m_iTotal<<endl;
}
static void PrintCoursePassedPercent(int course)//及格率
{
static char *szCourses[]={COURSE1_NAME,COURSE2_NAME,COURSE3_NAME,COURSE4_NAME,COURSE5_NAME};
static int *totalPass[]={&m_iCourse1TotalPass,&m_iCourse2TotalPass,&m_iCourse3TotalPass,&m_iCourse4TotalPass,&m_iCourse5TotalPass};
if(m_iTotal ==0)
throw "无学生成绩信息";
cout<<"\t"<<szCourses[course]<<"及格率:"<<*totalPass[course]*100/m_iTotal<<"%"<<endl;
}
static void PrintCourseMinScore(int course)//最低分
{
static char *szCourses[]={COURSE1_NAME,COURSE2_NAME,COURSE3_NAME,COURSE4_NAME,COURSE5_NAME};
static float *minScore[]={&m_fCourse1MinScore,&m_fCourse2MinScore,&m_fCourse3MinScore,&m_fCourse4MinScore,&m_fCourse5MinScore};
if(m_iTotal ==0)
throw "无学生成绩信息";
cout<<"\t"<<szCourses[course]<<"最低分:"<<*minScore[course]<<"\t学生名字:"<<m_szNameOfMinScore[course]<<endl;
}
static void PrintCourseMaxScore(int course)//最高分
{
static char *szCourses[]={COURSE1_NAME,COURSE2_NAME,COURSE3_NAME,COURSE4_NAME,COURSE5_NAME};
static float *maxScore[]={&m_fCourse1MaxScore,&m_fCourse2MaxScore,&m_fCourse3MaxScore,&m_fCourse4MaxScore,&m_fCourse5MaxScore};
if(m_iTotal ==0)
throw "无学生成绩信息";
cout<<"\t"<<szCourses[course]<<"最高分:"<<*maxScore[course]<<"\t学生名字:"<<m_szNameOfMaxScore[course]<<endl;
}
};
int CStudent::m_iTotal=0;//学生总人数
int CStudent::m_iCourse1TotalPass=0;//课程1及格人数
int CStudent::m_iCourse2TotalPass=0;
int CStudent::m_iCourse3TotalPass=0;
int CStudent::m_iCourse4TotalPass=0;
int CStudent::m_iCourse5TotalPass=0;
float CStudent::m_fCourse1MinScore=100.0f;
float CStudent::m_fCourse2MinScore=100.0f;
float CStudent::m_fCourse3MinScore=100.0f;
float CStudent::m_fCourse4MinScore=100.0f;
float CStudent::m_fCourse5MinScore=100.0f;
float CStudent::m_fCourse1MaxScore=0.0f;
float CStudent::m_fCourse2MaxScore=0.0f;
float CStudent::m_fCourse3MaxScore=0.0f;
float CStudent::m_fCourse4MaxScore=0.0f;
float CStudent::m_fCourse5MaxScore=0.0f;
float CStudent::m_fCourse1TotalScore=0.0f;
float CStudent::m_fCourse2TotalScore=0.0f;
float CStudent::m_fCourse3TotalScore=0.0f;
float CStudent::m_fCourse4TotalScore=0.0f;
float CStudent::m_fCourse5TotalScore=0.0f;
char CStudent::m_szNameOfMinScore[5][MAX_NAME_LEN]={0};
char CStudent::m_szNameOfMaxScore[5][MAX_NAME_LEN]={0};
vector<CStudent> vstu;
void show_usage()
{
cout<<"================================\n"
<<"1 按学生查询\n"
<<"2 按课程查询\n"
<<"3 按班级查询\n"
<<"4 添加学生成绩\n"
<<"0 退出\n"
<<"================================\n";
}
typedef void (*USAGE_FUN)();
int get_user_sel(USAGE_FUN f,int min_sel,int max_sel)
{
f();
int sel;
cout<<"请输入选择:";
cin>>sel;
while(sel<min_sel|| sel > max_sel)
{
cout<<"输入有误,请重新输入:\n";
f();
cout<<"请输入选择:";
cin>>sel;
}
return sel;
}
void add_stu_score()
{
unsigned int no;
char name[MAX_NAME_LEN]={0};
float c1,c2, c3, c4, c5;
cout<<"输入学号:";
cin>>no;
cout<<"输入姓名:";
cin>>name;
cout<<"输入"<<COURSE1_NAME<<"成绩:";
cin>>c1;
cout<<"输入"<<COURSE2_NAME<<"成绩:";
cin>>c2;
cout<<"输入"<<COURSE3_NAME<<"成绩:";
cin>>c3;
cout<<"输入"<<COURSE4_NAME<<"成绩:";
cin>>c4;
cout<<"输入"<<COURSE5_NAME<<"成绩:";
cin>>c5;
vstu.push_back(CStudent(no,name,c1,c2,c3,c4,c5));
}
void show_usage_cource()
{
cout<<"===============================\n"
<<"1 查询平均成绩\n"
<<"2 查询及格率\n"
<<"3 查询课程最小成绩\n"
<<"4 查询课程最大成绩\n"
<<"===============================\n";
}
void show_usage_student()
{
cout<<"===============================\n"
<<"1 查询学生某门课成绩\n"
<<"2 查询学生平均成绩\n"
<<"3 查询学生某门课等级\n"
<<"===============================\n";
}
CStudent* get_stu(unsigned int no)
{
for(unsigned int i=0;i<vstu.size();i++)
{
if(vstu[i].GetNo() == no)
{
return &vstu[i];
}
}
return NULL;
}
int main(int argc,char * argv[])
{
int sel=0;
try
{
while((sel=get_user_sel(show_usage,0,4)) != 0)
{
switch(sel)
{
case 1:
{
int stu_sel = get_user_sel(show_usage_student,1,3);
cout<<"输入学生学号:";
unsigned int no;
cin>>no;
CStudent* pstu = get_stu(no);
int course_sel =0;
if(stu_sel == 1 || stu_sel == 3)
{
course_sel = get_user_sel(CStudent::PrintCourses,1,5);
}
course_sel--;
if(pstu)
{
switch(stu_sel)
{
case 1:pstu ->QueryCourseScore(course_sel);break;
case 2:pstu ->QueryAvg();break;
case 3:pstu ->QuerygGrade(course_sel);break;
default:
break;
}
}
else
{
cout<<"无学生信息"<<endl;
}
}
break;
case 2:
{
int query_sel = get_user_sel(show_usage_cource,1,4);
int course_sel = get_user_sel(CStudent::PrintCourses,1,5);
course_sel--;
switch(query_sel)
{
case 1:CStudent::PrintCourseAvg(course_sel);break;
case 2:CStudent::PrintCoursePassedPercent(course_sel);break;
case 3:CStudent::PrintCourseMinScore(course_sel);break;
case 4:CStudent::PrintCourseMaxScore(course_sel);break;
default:
break;
}
}
break;
case 3:
{
//自己实现,写几个函数操作vstu即可
}
break;
case 4:add_stu_score();break;
default:
break;
}
}
}
catch(char *e)
{
vstu.clear();
cout<<e<<endl;
system("pause");
exit(-1);
}
vstu.clear();
return 0;
}