会C++的帮忙做一道题啊 非常感谢
发布网友
发布时间:2022-10-12 06:05
我来回答
共2个回答
热心网友
时间:2023-10-19 06:24
#include "stdafx.h"
#include<iostream>
#include<list>
using namespace std;
typedef unsigned int u_int;
#defineMAX_NUM_SCORE5//有几科成绩
#ifndefFALSE
#defineFALSE0
#endif
#ifndefTRUE
#defineTRUE 1
#endif
#defineSORT_TRUETRUE
typedef enum
{
SORT_XUEHAO,//按学号排序
SORT_SCORE1,//按学科1成绩排序
SORT_SCORE2,
SORT_SCORE3,
SORT_SCORE4,
SORT_SCORE5,
SORT_SCOREALL,//按总成绩排序
};
//每科的及格分数
static const u_int JIGE_FENSHU[MAX_NUM_SCORE] = {90,90,90,90,90};
typedef struct _StudentInfo
{
charXueHao[8];//学号
charName[20];//姓名
u_intScore[MAX_NUM_SCORE];//成绩
u_intAve;//学生个人的平均成绩
u_intSumScore;//学生总成绩
u_intIsGuaKE[MAX_NUM_SCORE +1];//挂科信息
}StudentInfo;
typedef bool(*cbfun)(list<StudentInfo>::iterator s1,list<StudentInfo>::iterator s2);
class Student
{
public:
u_intStudentNum;//学生总数
u_intStudentAve[MAX_NUM_SCORE + 1];//所有学成的单独每一个平均成绩
//添加一个学生信息
bool AddOneStudent(char* Xuehao,
char* Name,
u_int score_1,
u_int score_2,
u_int score_3,
u_int score_4,
u_int score_5);
//输出所有学生信息
void ShowStudentInfoAll(void);
//初始化所有学生的ave信息
void InitAllStudentAveInfo(void);
//初始化挂科信息
void InitStudentInfoIsGuaKE(void);
//排序,按照commonder命令
void SoftStudentInfo(int commonder);
//显示挂科信息
void ShowGuaKeInfo(void);
Student();
~Student();
private:
list<StudentInfo>mlist;//学生信息链表,这里使用了STL LIST库,没有单独写链表
//初始化一些学生的其他信息
void InitOneStudentAveInfo(StudentInfo* info);
//排序,使用冒泡,对于只有不足百条的学生数据足够
void for_each(cbfun fun);
};
Student::Student()
{
StudentNum = 0;
for (int i = 0; i < sizeof(StudentAve) / sizeof(u_int) ; i ++)
{
StudentAve[i] = 0;
}
}
Student::~Student()
{
list<StudentInfo>::iterator it = mlist.begin();
while (it != mlist.end())
{
it ++;
mlist.pop_front(); //清理链表
}
}
//初始化一些学生的ave信息
void Student::InitOneStudentAveInfo(StudentInfo* info)
{
if (info == NULL)
{
return ;
}
info->SumScore = 0;
for (int i = 0 ; i < MAX_NUM_SCORE ; i ++)
{
info->SumScore += info->Score[i];
}
info->Ave = info->SumScore / MAX_NUM_SCORE;
}
//初始化所有学生的ave信息
void Student::InitAllStudentAveInfo(void)
{
u_int allave[MAX_NUM_SCORE] = {0};
u_int allscore= 0;
if (StudentNum == 0)
{
return ;
}
list<StudentInfo>::iterator it = mlist.begin();
for (int i = 0 ; i < StudentNum ; i ++)
{
for (int j = 0 ; j < MAX_NUM_SCORE ; j ++)
{
allave[j] += it->Score[j];
}
it ++;
}
for (int k = 0 ; k < MAX_NUM_SCORE ; k ++)
{
StudentAve[k] = allave[k] / StudentNum ; //每门学科的平均成绩
allscore += allave[k];
}
//总成绩的平均成绩
StudentAve[MAX_NUM_SCORE] = allscore / StudentNum;
}
//添加一个学生信息
bool Student::AddOneStudent(char* Xuehao,
char* Name,
u_int score_1,
u_int score_2,
u_int score_3,
u_int score_4,
u_int score_5)
{
StudentInfo tmp = {0};
//对姓名和学号检查
if (Xuehao == NULL || Xuehao[0] == '\0' || Name == NULL || Name[0] == '\0' )
{
return FALSE;
}
//对成绩检查
if(score_1 > 150 || score_2 > 150 || score_3 > 150 || score_4 > 150 || score_5 > 150)
{
return FALSE;
}
//所有数据有效才添加,注意使用了strcpy,进行拷贝保护
strncpy(tmp.Name,Name,sizeof(tmp.Name) - 1);
strncpy(tmp.XueHao,Xuehao,sizeof(tmp.XueHao) -1);
tmp.Score[0] = score_1;
tmp.Score[1] = score_2;
tmp.Score[2] = score_3;
tmp.Score[3] = score_4;
tmp.Score[4] = score_5;
InitOneStudentAveInfo(&tmp);
//加入链表
mlist.push_front(tmp);
StudentNum ++;
return TRUE;
}
//输出所有学生信息
void Student::ShowStudentInfoAll()
{
list<StudentInfo>::iterator it = mlist.begin();
StudentInfo tmp = {0};
//memcpy(&tmp,it,sizeof(StudentInfo));
printf("学号 姓名 成绩1 成绩2 成绩3 成绩4 成绩5 平均成绩 总成绩\n");
while( it != mlist.end() )
{
printf("%-8s %-8s %3d %3d %3d %3d %3d %3d %3d\n",
it->XueHao,
it->Name,
it->Score[0],
it->Score[1],
it->Score[2],
it->Score[3],
it->Score[4],
it->Ave,
it->SumScore);
it ++;
}
InitAllStudentAveInfo();
printf("------------------------\n平均成绩: %3d %3d %3d %3d %3d %3d\n\n",
StudentAve[0],
StudentAve[1],
StudentAve[2],
StudentAve[3],
StudentAve[4],
StudentAve[5]);
}
bool soft_xuehao(StudentInfo* s1,StudentInfo* s2)
{
int No1 = 0,No2 = 0;
if (s1 == NULL || s2 == NULL)
{
return !SORT_TRUE; //不会执行本句话
}
else
{
No1 = atoi(s1->XueHao);
No2 = atoi(s2->XueHao);
//调整SORT_TRUE的值可以改变排序顺序
if (No1 > No2)
{
return SORT_TRUE;
}
}
return !SORT_TRUE;
}
#define SORT_FUN_BY_SCORE(funname,id) \
bool soft_fun_##funname##(list<StudentInfo>::iterator s1,list<StudentInfo>::iterator s2) \
{if( s1->Score[id] > s1->Score[id] )return SORT_TRUE;return !SORT_TRUE;}
//定义排序函数bool soft_fun_score1(StudentInfo* s1,StudentInfo* s2)
SORT_FUN_BY_SCORE(score1,0)
SORT_FUN_BY_SCORE(score2,1)
SORT_FUN_BY_SCORE(score3,2)
SORT_FUN_BY_SCORE(score4,3)
SORT_FUN_BY_SCORE(score5,4)
bool soft_fun_score_all(list<StudentInfo>::iterator s1,list<StudentInfo>::iterator s2)
{
//调整SORT_TRUE的值可以改变排序顺序
if (s1->SumScore > s2->SumScore)
{
return !SORT_TRUE;
}
return SORT_TRUE;
}
static void copyitinfo(StudentInfo* st,list<StudentInfo>::iterator itx)
{
if(st == NULL)
return;
strcpy(st->Name,itx->Name);
strcpy(st->XueHao,itx->XueHao);
st->SumScore = itx->SumScore;
st->Ave = itx->Ave;
memcpy(st->Score,itx->Score,sizeof(st->Score));
}
//排序,使用冒泡,对于只有不足百条的学生数据足够
void Student::for_each(cbfun fun)
{
if (fun == NULL || StudentNum < 2)
{
return;
}
list<StudentInfo>::iterator it = mlist.begin();
list<StudentInfo>::iterator itx = mlist.begin();
for (int szie_list = 0; szie_list < StudentNum - 1 ;szie_list ++)
{
it = mlist.begin();
for (int tmp = StudentNum - szie_list - 1; tmp > 0 ; tmp --)
{
itx = it;
it ++;
if (fun(itx,it))
{//交换数据
StudentInfo s1 = {0};
copyitinfo(&s1,itx);
//删除数据
mlist.erase(itx);
//插入数据
it ++;
mlist.insert(it,s1);
}
}
}
}
//排序,按照commonder命令
void Student::SoftStudentInfo(int commonder)
{
switch (commonder)
{
case SORT_SCORE1:
//mlist.sort(soft_fun_score1);
for_each(soft_fun_score1);
break;
case SORT_SCORE2:
//mlist.sort(soft_fun_score2);
for_each(soft_fun_score2);
break;
case SORT_SCORE3:
//mlist.sort(soft_fun_score3);
for_each(soft_fun_score3);
break;
case SORT_SCORE4:
//mlist.sort(soft_fun_score4);
for_each(soft_fun_score4);
break;
case SORT_SCORE5:
//mlist.sort(soft_fun_score5);
for_each(soft_fun_score5);
break;
case SORT_SCOREALL:
//mlist.sort(soft_fun_score_all);
for_each(soft_fun_score_all);
break;
}
}
//初始化挂科信息
void Student::InitStudentInfoIsGuaKE(void)
{
list<StudentInfo>::iterator it = mlist.begin();
while (it != mlist.end())
{
it->IsGuaKE[MAX_NUM_SCORE] = 0;
for (int i= 0; i < MAX_NUM_SCORE ; i ++)
{
if (it->Score[i] >= JIGE_FENSHU[i])
{//没挂科
it->IsGuaKE[i] = 0;
}
else
{
it->IsGuaKE[i] = 1;
it->IsGuaKE[MAX_NUM_SCORE] += 1;
}
}
it ++;
}
}
//显示挂科信息
void Student::ShowGuaKeInfo(void)
{
list<StudentInfo>::iterator it = mlist.begin();
InitStudentInfoIsGuaKE();
printf("学号 姓名 成绩1 成绩2 成绩3 成绩4 成绩5 挂科总数 是否通过\n");
while( it != mlist.end() )
{
printf("%-8s %-8s %s %s %s %s %s %d %s\n",
it->XueHao,
it->Name,
it->IsGuaKE[0] ? "挂科" : "通过",
it->IsGuaKE[1] ? "挂科" : "通过",
it->IsGuaKE[2] ? "挂科" : "通过",
it->IsGuaKE[3] ? "挂科" : "通过",
it->IsGuaKE[4] ? "挂科" : "通过",
it->IsGuaKE[5],
it->IsGuaKE[5] >= 3 ? "不通过" : "通过");
it ++;
}
}
int main()
{
Student stu;
stu.AddOneStudent("1001","111",100,110,120,130,148);
stu.AddOneStudent("1002","222",100,110,120,130,142);
stu.AddOneStudent("1003","333",100,110,120,130,143);
stu.AddOneStudent("1004","444",100,110,120,130,144);
stu.AddOneStudent("1005","555",100,110,120,130,145);
stu.ShowStudentInfoAll();
stu.SoftStudentInfo(SORT_SCOREALL);
stu.ShowStudentInfoAll();
stu.ShowGuaKeInfo();
return 0;
}
注意#include "stdafx.h"是C++ 自动生成的
你要的所有功能都实现了,请自己在main函数中写自己的命令操作命令,我在main里面已经给你演示了个个函数的用法了,此处不再写命令。 同时,你自己也可以试下STL的 排序方法,我这里是VC 6.0 STL排序还没有安装补丁 所以先给你写了个冒泡排序。 忙了几个小时了,请追加分数,辛苦
热心网友
时间:2023-10-19 06:25
这个样本让我情何以堪,,,,
私有成员都设计成public???
而且还自带一个Student *stu 你这是要哪样啊