演讲比赛管理系统
1. 比赛规则
- 12名选手,分组比赛,每组6人;
- 选手有编号10001~10012;
- 第一轮分为两个小组,整体按照选手编号进行抽签后顺序演讲;
- 十名评委给每名选手打分,去除最高分和最低分,求平均分为成绩;
- 小组演讲完成后,淘汰最后三名选手,前三名晋级,进行下一轮;
- 第二轮为决赛,前三名胜出;
- 每轮比赛过后显示晋级选手信息。
2. 程序设计
2.1 管理类
Manager.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
#include <Windows.h>
#include <numeric>
#include "Player.h"
#include "myfunc.hpp"
class Manager
{
public:
Manager();
~Manager();
void Menu();//功能菜单
void Exit();//退出程序
void OtherKeys();//一个保险功能,用户按了其他键的话执行这个函数
void Scoring(std::vector<Player*> &v);//打分
void Mysort(std::vector<Player*> &v);//排序
void MatchProcess();//比赛流程
public:
std::vector<Player*> m_player;//保存所有的参赛人员信息
std::vector<Player*> m_match1;//第一轮的第一个分组
std::vector<Player*> m_match2;//第一轮的第二个分组
std::vector<Player*> m_Winner;//第二轮的人员
};
Manager.cpp
#include "Manager.h"
Manager::Manager() //构造函数
{
srand((int)time(0));
Player *p = new Player;
std::vector<unsigned> vec;
vec.resize(10);
m_player.reserve(12);
for (int i = 0; i != 12; ++i)
{
p->Setm_num(10001 + i);
p->Setm_averscore(0);
p->Setm_score(std::vector<unsigned>(vec));
m_player.push_back(new Player(*p)); //为了能够delete掉p特地深拷贝
}
delete p;//这样就可以执行Player的析构函数了
this->m_match1.resize(6);
this->m_match2.resize(6);
this->m_Winner.resize(6);
}
Manager::~Manager()
{
//std::cout << "~Manager()" << std::endl;
if (!this->m_player.empty())
{
for (auto &r : this->m_player)
{
delete r;
r=NULL;
}
this->m_player.clear();
}
m_match1.clear();
m_match2.clear();
m_Winner.clear();
}
void Manager::Menu()
{
std::cout << "*****************" << std::endl;
std::cout << "1.开始演讲比赛" << std::endl;
std::cout << "2.查看往届记录" << std::endl;
std::cout << "3.清空比赛记录" << std::endl;
std::cout << "0.退出比赛程序" << std::endl;
std::cout << "*****************" << std::endl;
std::cout << "请选择:" << std::endl;
}
void Manager::Exit()
{
std::cout << "欢迎下次使用!" << std::endl;
}
void Manager::OtherKeys()
{
std::cout << "输入错误" << std::endl;
system("pause");
system("cls");
}
void Manager::Scoring(std::vector<Player*> &v)
{
for (auto &r : v)
{
std::vector<unsigned> vec;
vec.reserve((*v.begin())->Getm_score().size());
for (int i = 0; i != (*v.begin())->Getm_score().size(); ++i)
{
vec.push_back(rand() % 10 + 1);
}
r->Setm_score(vec);
std::sort(r->Getm_score().begin(), r->Getm_score().end());
r->Setm_averscore((double)std::accumulate(r->Getm_score().begin() + 1, r->Getm_score().end() - 1, 0)/8);
}
}
void Manager::Mysort(std::vector<Player*> &v)
{
std::sort(v.begin(), v.end(), Mysort_averscore());
}
void Manager::MatchProcess()
{
std::cout << "正在分组";
for (int i = 0; i != 3; ++i)
{
std::cout << "..";
Sleep(100);
}
std::cout << std::endl;
std::cout << "分组完成!" << std::endl;
std::random_shuffle(this->m_player.begin(), this->m_player.end());
std::transform(this->m_player.begin(),this->m_player.begin() + 6,this->m_match1.begin(), Transform());
std::transform(this->m_player.begin() + 6, this->m_player.end(), this->m_match2.begin(), Transform());
system("pause");
system("cls");
std::cout << "分组与顺序:" << std::endl;
std::cout << "第一组:" << std::endl;
std::for_each(m_match1.begin(), m_match1.end(), PrintPlayer());
std::cout << std::endl;
std::cout << "第二组:" << std::endl;
std::for_each(m_match2.begin(), m_match2.end(), PrintPlayer());
std::cout << std::endl;
this->Scoring(this->m_match1);
this->Scoring(this->m_match2);
this->Mysort(this->m_match1);
this->Mysort(this->m_match2);
std::cout << "第一轮比赛中";
for (int i = 0; i != 3; ++i)
{
std::cout << "..";
Sleep(100);
}
std::cout << std::endl;
std::cout << "第一轮比赛结束!" << std::endl;
system("pause");
system("cls");
std::cout << "比赛结果为:" << std::endl;
std::cout << "第一组:" << std::endl;
std::for_each(m_match1.begin(), m_match1.end(), PrintPlayer());
std::cout << std::endl;
std::cout << "第二组:" << std::endl;
std::for_each(m_match2.begin(), m_match2.end(), PrintPlayer());
std::cout << std::endl;
std::cout << "晋级选手有:" << std::endl;
std::transform(this->m_match1.begin(), this->m_match1.begin() + 3, this->m_Winner.begin(), Transform());
std::transform(this->m_match2.begin(), this->m_match2.begin() + 3, this->m_Winner.begin() + 3, Transform());
std::for_each(m_Winner.begin(), m_Winner.end(), PrintPlayer());
system("pause");
system("cls");
std::cout << "第二轮比赛中";
for (int i = 0; i != 3; ++i)
{
std::cout << "..";
Sleep(100);
}
std::cout << std::endl;
std::cout << "第二轮比赛结束!" << std::endl;
system("pause");
system("cls");
this->Scoring(this->m_Winner);
this->Mysort(this->m_Winner);
std::cout << "优胜选手为:" << std::endl;
std::for_each(m_Winner.begin(), m_Winner.begin() + 3, PrintPlayer());
system("pause");
system("cls");
}
2.2 参赛人员类
Player.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
class Player
{
public:
Player();
~Player();
bool operator==(const Player &p);
bool operator<(const Player &p);
bool operator>(const Player &p);
void Setm_num(unsigned num);
void Setm_averscore(double score);
void Setm_score(std::vector<unsigned> &score);
unsigned Getm_num() const;
double Getm_averscore() const;
std::vector<unsigned> &Getm_score();
private:
unsigned m_num;//参赛人员编号
double m_averscore;//计算得到的平均分
std::vector<unsigned> m_score;//评委给选手打的分数
};
Player.cpp
#include "Player.h"
Player::Player()
{
//std::cout << "Player()" << std::endl;
this->m_num = 0;
this->m_averscore = 0;
this->m_score.clear();
}
Player::~Player()
{
std::cout << "~Player()" << std::endl;
}
void Player::Setm_num(unsigned num)
{
this->m_num = num;
}
void Player::Setm_averscore(double score)
{
this->m_averscore = score;
}
unsigned Player::Getm_num() const
{
return this->m_num;
}
double Player::Getm_averscore() const
{
return this->m_averscore;
}
void Player::Setm_score(std::vector<unsigned> &score)
{
this->m_score = score;
}
std::vector<unsigned> & Player::Getm_score()
{
return this->m_score;
}
bool Player::operator==(const Player &p)
{
return (this->Getm_num() == p.Getm_num() &&
this->Getm_averscore() == p.Getm_averscore());
}
bool Player::operator<(const Player &p)
{
return this->Getm_averscore() < p.Getm_averscore();
}
bool Player::operator>(const Player &p)
{
return this->Getm_averscore() > p.Getm_averscore();
}
2.3 仿函数类
myfunc.hpp
#pragma once
#include "Manager.h"
class PrintPlayer //打印输出选手的编号与平均分
{
public:
void operator()(const Player *p) const
{
std::cout << "编号: " << p->Getm_num() << "\t" << "成绩: " << p->Getm_averscore() << std::endl;
}
};
class Transform //为transform算法准备的仿函数
{
public:
Player* operator()(Player *p)
{
return p;
}
};
class Mysort_averscore //为sort准备的仿函数,降序排列
{
public:
bool operator()(Player* &p1, Player* &p2)
{
return p1->Getm_averscore() > p2->Getm_averscore();
}
};
2.4 main函数
#include "Manager.h"
#include "Player.h"
using namespace std;
int main()
{
Manager m;
string key;
while (1)
{
m.Menu();
cin >> key;
switch (atoi(key.c_str()))
{
case 1:
m.MatchProcess(); break;
case 2:
break;
case 3:
break;
case 0:
m.Exit(); return 0;
default:
m.OtherKeys(); break;
}
}
}
功能2与功能3还没写,还有文件写入读取的功能也没写,暂时不想写了,以后再补起来把。