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 k,float av); //记录第j个评委的打分
friend void Sort(CompetitionResult *pr,int n); //按最后得分从高到低排序
};
1>写出所有成员函数的实现代码
2>编写main()函数对该类进行测试。在函数体中定义CompetitionResult类的对象数组r[PlayerNum],它的每个元素记录每个选手的所有信息,各评委的打分通过键盘输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到低排序输出。
1程序代码如下:
#include<iostream>
#include<iomanip>
#include<string>
const int JudgeNum = 6;
using namespace std;
class CompetitionResult
{
short num; //选手号码
char name[5]; //选手姓名
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 k, 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;
}
CompetitionResult::CompetitionResult(short n, char * ps)
{
num = n;
strcpy(name, ps);
for (int i = 0; i < JudgeNum; i++)
{
score[i] = 0.0;
}
average = 0;
}
float CompetitionResult::MaxScore()
{
int max = score[0]; //假设第零个得分最高
for (int i = 0; i < JudgeNum; i++)
{
if (max < score[i])
{
max = score[i]; //求出最高分
}
return max;
}
}
float CompetitionResult::MinScore()
{
int min = score[0]; //假设第零个得分最小
for (int i = 0; i < JudgeNum; i++)
{
if (min > score[i])
{
min = score[i]; //求出最低分
}
return min;
}
}
void CompetitionResult::SetAvg()
{
int i;
float sum = 0.0;
for (i = 0; i < JudgeNum; i++)
{
sum += score[i];
}
sum = sum - MaxScore() - MinScore();
average = sum / (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]; //读第j个评委的打分
}
void CompetitionResult::SetScore(int k, float av)
{
score[k] = av; //记录第j个评委的打分
}
void Sort(CompetitionResult * pr, int n)
{
int i, j, k;
CompetitionResult temp;
for (i = 0; i < n - 1; i++) //对分数进行从高到低排序
{
k = i; //假设第i个得分最高
for (j = i + 1; j < n; j++)
{
if (pr[j].average > pr[k].average) //比较
{
k = j;
}
if (i!= k)
{
temp = pr[i];
pr[i] = pr[k];
pr[k] = temp;
}
}
}
}
int main()
{
const int num = 10;
CompetitionResult player[num] = { CompetitionResult(1,"刘若英"),
CompetitionResult(2,"许嵩"),CompetitionResult(3,"周传雄"),
CompetitionResult(4,"陈小春"), CompetitionResult(5,"黄家驹"),
CompetitionResult(6,"梁静茹"), CompetitionResult(7,"邓紫棋"),
CompetitionResult(8,"李荣浩"), CompetitionResult(9,"王力宏"),
CompetitionResult(10,"周杰伦") };
float a;
for (int i = 0; i <num; i++)
{
cout << "请评委给第" << i + 1 << "个选手打分:" << endl;
for (int j = 0; j < JudgeNum; j++)
{
cin >> a;
player[i].SetScore(j, a);
}
player[i].SetAvg();
cout << endl;
}
Sort(player, num);
cout << " 最后的得分情况为: " << endl;
for (int j = 0; j < num; j++)
{
cout << j + 1 << " ";
cout << setw(6) << player[j].GetAvg();
cout << " " << player[j].GetName() << endl;
}
return 0;
``
2.结果