某次歌手比赛中,有JudgeNum个评委给选手打分,参加比赛的选手有PlayerNum名,现为比赛记分编写一个CompetitionResult类,类的定义如下:
class CompetitionResult
{
short num; //选手号码
char name[10]; //选手姓名
float score[JudgeNum]; //记录各评委给选手的打分
float average; //选手最后得分,去掉一个最高分和最低分后的平均分
public:
CompetitionResult();
CompetitionResult(short n,char *ps);
float MaxScore(); //求评委打的最高分
float MinScore(); //求评委打的最低分
void SetAvg(); //求选手的最后得分
float GetAvg(); //读选手的最后得分
short GetNo(); //求选手的编号
void setNo(int j); //设置选手的编号
char * GetName(); //读选手的姓名
float GetScore(int j); //读第j个评委的打分
void SetScore(int j,float av); //记录第j个评委的打分
friend void Sort(CompetitionResult *pr,int n); //按最后得分从高到低排序
};
1>写出所有成员函数的实现代码
2>编写main()函数对该类进行测试。在函数体中定义CompetitionResult类的对象数组r[PlayerNum],它的每个元素记录每个选手的所有信息,各评委的打分通过键盘输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到低排序输出。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
const int JudgeNum = 6;
class CompetitionResult
{
short num; //选手号码
char name[10]; //选手姓名
float score[JudgeNum]; //记录各评委给选手的打分
float average; //选手最后得分,去掉一个最高分和最低分后的平均分
public:
CompetitionResult();
CompetitionResult(short n, char *ps);
float MaxScore(); //求评委打的最高分
float MinScore(); //求评委打的最低分
void SetAvg(); //求选手的最后得分
float GetAvg(); //读选手的最后得分
short GetNo(); //求选手的编号
void setNo(int j); //设置选手的编号
char * GetName(); //读选手的姓名
float GetScore(int j); //读第j个评委的打分
void SetScore(int j, float av); //记录第j个评委的打分
friend void Sort(CompetitionResult *pr, int n); //按最后得分从高到低排序
};
CompetitionResult::CompetitionResult()
:num (0)
{
strcpy(name, "");
for (int i = 0; i < JudgeNum; i++)
{
score[i] = 0.0;
}
average = 0.0;
}
CompetitionResult::CompetitionResult(short n, char *ps)
:num(n)
{
strcpy(name, ps);
for (int i = 0; i < JudgeNum; ++i)
{
score[i] = 0.0;
}
average = 0.0;
}
float CompetitionResult::MaxScore()
{
float Max = score[0];
for (int i = 1; i < JudgeNum; ++i)
{
if (score[i] > Max)
{
Max = score[i];
}
}
return Max;
}
float CompetitionResult::MinScore()
{
float Min = score[0];
for (int i = 1; i < JudgeNum; ++i)
{
if (score[i] < Min)
{
Min = score[i];
}
}
return Min;
}
void CompetitionResult::SetAvg()
{
float sum = 0.0;
for (int i = 0; i < JudgeNum; ++i)
{
sum += score[i];
}
average = (sum - MaxScore() - MinScore()) / (JudgeNum - 2);
}
float CompetitionResult::GetAvg()
{
return average;
}
short CompetitionResult::GetNo()
{
return num;
}
void CompetitionResult::setNo(int j)
{
num = j;
}
char*CompetitionResult::GetName()
{
return name;
}
float CompetitionResult::GetScore(int j)
{
return score[j];
}
void CompetitionResult::SetScore(int j, float av)
{
score[j]=av;
}
void Sort(CompetitionResult *pr, int n)
{
CompetitionResult t;
for (int i = 0; i < n-1; i++)
{
for (int j = 0;j < n - 1 - i; ++j)
{
if ((pr[j].average) < (pr[j + 1].average))
{
t = pr[j];
pr[j] = pr[j + 1];
pr[j + 1] = t;
}
}
}
}
int main()
{
const int num = 4;
CompetitionResult player[num] =
{
CompetitionResult(1,"aaa"),
CompetitionResult(2,"bbb"),
CompetitionResult(3,"ccc"),
CompetitionResult(4,"ddd"),
};
for (int i = 0; i < num; ++i)
{
cout << "请给第" << i + 1 << "个选手打分:" << endl;
float a;
for (int j = 0; j < JudgeNum; j++)
{
cin >> a;
player[i].SetScore(j, a);
}
player[i].SetAvg();
}
Sort(player, num);
cout << "最终得分为:" << endl;
for (int i = 0; i < num; ++i)
{
cout << i + 1;
cout << setw(6) << player[i].GetAvg();
cout << " " << player[i].GetName() << endl;
}
return 0;
}
运行结果: