C++学习笔记:数组类的排序处理

题目: 在歌唱比赛中有10位选手,5个评委,选手的得分计算规则是去掉最高分和最低分后求平均分,计算输出每个选手在本次比赛中的平均分并且排序。


在歌唱比赛中有10位选手,5个评委,选手的得分计算规则是去掉最高分和最低分后求平均分,计算输出每个选手在本次比赛中的平均分并且排序。


刚开始参考 (https://blog.csdn.net/yinkaishikd/article/details/46620477?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161747151616780265452746%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161747151616780265452746&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-2-46620477.pc_search_result_no_baidu_js&utm_term=C%2b%2b%E6%80%8E%E4%B9%88%E5%B0%86%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%95%B0%E6%8D%AE%E8%BE%93%E5%85%A5%E5%88%B0%E6%95%B0%E7%BB%84%E4%B8%AD%E8%AE%A1%E7%AE%97) 的介绍 通过循环实现

初步代码如下

#include <iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
 class People {
 public:
	string name;
	float Point[5] = {};
	float Avg;
	int ID;
	int getID(){
		return ID;
	}
	float GetAvg();
	People();
	People(int ID, string name);
	People(const People& obj);
	~People(void);
	void Show()
	{
		Avg = GetAvg();
		cout << "选手" << name << "信息如下:" << endl;
		cout << "\t编号:" << ID << endl;
		cout << "\t平均分:" << Avg << endl;
	}

	void in()
	{
		cin >> Point[0] >> Point[1] >> Point[2] >> Point[3] >> Point[4];
	}

 };
float People::GetAvg() {
	sort(Point, Point + 5);
	Avg = (Point[1] + Point[2] + Point[3]) / 3;
	return Avg;
 }
 People::People(void){
 }
 People::People(int ID, string name){
	 ID = ID;
	 name = name;
 }
People::People(const People& obj)
 {
	 ID = obj.ID;
	 name = obj.name;
 }
People::~People()
{
}

 int main(){
	 int n = 0;
	 float AVG=0;
	 int ID;
	 string name;
	 cout << "请输入学生个数" << endl;
	 cin >> n;
	 People *ctor = new People[n];
//	 People *ctor[20];
	 for (int i = 0; i < n; i++){
		 cout << endl << "请输第" << i + 1 << "个选手的信息:" << endl;
		 cout << "编号:";
		 cin >> ID;
		 cout << flush << "姓名:";
		 cin >> name;
		 ctor[i].name = name;
		 ctor[i].ID = ID;
		 cout << flush << "请输入五个评委给的分数:";
		 ctor[i].in();
	 }
	 cout << endl << "以下是所有选手的排名信息:\n" << endl;
	 for (int j = 0; j < n; j++)
	 {
		 for (int k = 0; k < n - j - 1; k++)
		 {
			 float Avg1 = ctor[k].GetAvg();
			 float Avg2 = ctor[k + 1].GetAvg();
			 if (Avg1 < Avg2)
			 {
				 People temp = ctor[k];
				 ctor[k] = ctor[k + 1];
				 ctor[k + 1] = temp;
			 }
		 }
	 }
	 for (int i = 0; i < n; i++){
		 ctor[i].Show();
	 }
	 delete[] ctor;
	 return 0;
 }

这种方法运行以后发现无论是升序降序 最后一个被排序的对象的Avg值一直是0,一直找不到原因。然后参考了
https://blog.csdn.net/zsy162534/article/details/52100878
用Sort解决排序问题。实现输入输出排序,完成作业。

#include <iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
 class People {
 public:
	string name;
	float Point[5];
	float Avg;
	int ID;
	float GetAvg();
	People();
	People(int ID, string name);
	People(const People& obj);
	~People(void);
	void Show()
	{
		Avg = GetAvg();
		cout << "选手" << name << "信息如下:" << endl;
		cout << "\t编号:" << ID << endl;
		cout << "\t平均分:" << Avg << endl;
	}

	void in()
	{
		cin >> Point[0] >> Point[1] >> Point[2] >> Point[3] >> Point[4];
	}
 };
float People::GetAvg() {
	sort(Point, Point + 5);
	Avg = (Point[1] + Point[2] + Point[3]) / 3;
	return Avg;
 }
 People::People(void){
 }
 People::People(int ID, string name){
	 ID = ID;
	 name = name;
 }
People::People(const People& obj)
 {
	 ID = obj.ID;
	 name = obj.name;
 }
People::~People()
{
}
int cmp(const void *a, const void *b)
{
	return ((People *)b)->GetAvg() - ((People *)a)->GetAvg();
}

 int main(){
	 int n = 0;
	 int ID;
	 string name;
	 cout << "请输入选手个数" << endl;
	 cin >> n;
	 People *ctor = new People[n];
//	 People *ctor[20];
	 for (int i = 0; i < n; i++){
		 cout << endl << "请输第" << i + 1 << "个选手的信息:" << endl;
		 cout << "编号:";
		 cin >> ID;
		 cout << flush << "姓名:";
		 cin >> name;
		 ctor[i].name = name;
		 ctor[i].ID = ID;
		 cout << flush << "请输入五个评委给的分数:";
		 ctor[i].in();
	 }
	 cout << endl << "以下是所有选手的排名信息:\n" << endl;
	 qsort(ctor, n, sizeof(ctor[0]), cmp);
	 for (int i = 0; i < n; i++){
		 ctor[i].Show();
	 }
	 delete[] ctor;
	 return 0;
 }

在这里插入图片描述

记录学习过程,以后遇到类似问题能解决
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值