某次歌手比赛中,有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 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.结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值