1.需求分析
需求分析:
- 创建选手,放到vector容器中
- 遍历容器,十个评委的评分存到deque容器中
- 对评分排序,去除最高分和最低分
- 计算平均分
- 对参赛选手根据平均分排名
2.实现代码
- main.cpp
#include <iostream>
#include "Player.h"
#include <ctime>
#include <algorithm>
// 排序
// 使用的是选择排序,但是似乎有问题??
void mySort(vector<Player>&players)
{
for (int i = 1; i < players.size(); ++i)
{
int k = i;
for (int j = i + 1; j < players.size(); ++j)
{
if (players[j].score < players[k].score)
{
k = j;
}
}
if (k != i)
{
players[i].mySwap(players[k]);
}
}
}
// 仿函数||回调函数,sort()算法的排序方法
bool myCompare(Player p1, Player p2)
{
return p1.score < p2.score;
}
int main()
{
srand((unsigned int)time(NULL)); // 随机数种子
int num;
cout << "请输入参赛选手个数:";
cin >> num;
vector<Player> players;
players.reserve(num);
int age = 0; // 初始化选手年龄为0
char nameSeed = 'A';
// 初始化选手信息
for (int i = 0; i < num; ++i)
{
string name = "选手";
name += (nameSeed + i);
age = rand() % 11 + 20; // 限定范围为20-30岁
Player temp(name, age);
players.push_back(temp);
}
// 评委打分
for (int i = 0; i < players.size(); ++i)
{
cout << "\n第" << i + 1 << "个选手评分情况:" << endl;
players[i].inputScore();
}
打印输出选手得分情况
for (int i = 0; i < players.size(); ++i)
{
// cout << "\n第" << i + 1 << "个选手得分情况:" << endl;
// players[i].printV_score();
players[i].del_MaxMin(); // 删除最高分和最低分
// cout << "第" << i + 1 << "个选手去除最值分数之后的情况:" << endl;
// players[i].printD_score();
players[i].score = players[i].getAver(); // 计算平均分
// cout << "第" << i + 1 << "个选手均分情况:" << players[i].score << "分" << endl;
}
// 根据选手平均分降序排序
// mySort(players);
sort(players.begin(), players.end(), myCompare);
// 输出排名情况
cout << "\n参赛选手得分排名如下:" << endl;
int a = 1;
for (int i = players.size() - 1; i > -1; --i, ++a)
{
cout << "\t选手姓名:" << players[i].name << " 选手年龄:" << players[i].age << " 选手成绩:" << players[i].score << " 选手排名:" << a << endl;
}
system("pause");
return 0;
}
- Player.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <deque>
using namespace std;
class Player
{
public:
int score;
string name;
int age;
vector<int> v_score;
deque<int> d_score;
Player(string name, int age); // 构造函数
int inputScore(); // 评分函数
void del_MaxMin(); // 删除最高分和最低分
double getAver(); // 获取平均分
void printV_score(); // 打印初始评分
void printD_score(); // 打印删除最值后的评分
void mySwap(Player&other); // 交换选手位置
};
- Player.cpp
#include "Player.h"
#include <algorithm>
// 构造函数
Player::Player(string name, int age)
{
this->name = name;
this->age = age;
}
// 获取评分
int Player::inputScore()
{
int num = 10; // 评委个数
// int score = 0;
/*cout << "请输入参与评分的评委个数:";
cin >> num;*/
this->v_score.reserve(num);
for (int i = 0; i < num; ++i)
{
/*cout << "请输入第" << i + 1 << "个评委的评分:";
cin >> score;*/
this->v_score.push_back(rand() % 41 + 60); // 随机生成成绩
}
cout << "成绩录入完成!" << endl;
return num;
}
void Player::del_MaxMin()
{
for (int i = 0; i < v_score.size(); i++)
{
d_score.push_back(v_score[i]);
}
sort(d_score.begin(), d_score.end());
int min = d_score[0];
int max = d_score[d_score.size() - 1];
d_score.pop_front();
d_score.pop_back();
}
// 求平均值
double Player::getAver()
{
int sum = 0;
for (deque<int>::iterator temp = d_score.begin(); temp != d_score.end(); ++temp)
{
sum += *temp;
}
return sum / d_score.size();
}
void Player::printV_score()
{
if (v_score.empty())
{
cout << "评分还未录入!" << endl;
return;
}
cout << "已保存的分数为:";
for (vector<int>::iterator temp = v_score.begin(); temp != v_score.end(); ++temp)
{
cout << *temp << " ";
}
cout << endl;
}
void Player::printD_score()
{
if (d_score.empty())
{
cout << "评分还未录入!" << endl;
return;
}
cout << "已保存的分数为:";
for (deque<int>::iterator temp = d_score.begin(); temp != d_score.end(); ++temp)
{
cout << *temp << " ";
}
cout << endl;
}
// 交换
void Player::mySwap(Player& other)
{
string name;
int age;
int score;
name = other.name;
age = other.age;
score = other.score;
other.name = this->name;
other.age = this->age;
other.score = this->score;
this->name = name;
this->age = age;
this->score = score;
this->v_score.swap(other.v_score);
this->d_score.swap(other.d_score);
}