1. 比赛规则
学校举行一场演讲比赛,共12个人参加,比赛共两轮,第一轮淘汰赛,第二轮决赛
每名选手都有编号(如10001~10012)
比赛方式:分组比赛,每组6个人
第一轮分为两个小组,整体按照选手编号进行抽签后顺序演讲
十名评委分别给每名选手打分,去除最高分和最低分,平均分为选手成绩
小组演讲完毕后,淘汰组内排名最后的三名选手,前三名晋级,进入下一轮比赛
第二轮为决赛,前三名胜出
每轮比赛过后需要显示晋级选手的信息
1.2 程序功能
开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段
查看往届记录:查看之前比赛的前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存
清空比赛记录:将文件中数据清空
退出比赛系统:可以退出当前程序
头文件
speaker.h
#pragma once
#include
using namespace std;
//选手类
class Speaker {
public:
string m_Name;
double m_Score[2]; //分数 最多有两轮得分
};
speechManager.h
#pragma once
#include
using namespace std;
#include
#include
#include
源文件
speechManager.cpp
#include "speechManager.h"
#define SAVEFILE "saveFile.csv"
//构造函数
SpeechManager::SpeechManager()
{
//初始化比赛
initSpeech();
//创建选手
createSpeech();
//加载往届记录
loadRecord();
}
//显示菜单
void SpeechManager::showMenu()
{
cout << "------------------------------------------------" << endl;
cout << "*************** 欢迎参加演讲比赛 *************" << endl;
cout << "*************** 1.开始演讲比赛 ***************" << endl;
cout << "*************** 2.查看往届记录 ***************" << endl;
cout << "*************** 3.清空比赛记录 ***************" << endl;
cout << "*************** 0.退出比赛程序 ***************" << endl;
cout << "------------------------------------------------" << endl;
cout << endl;
}
//初始化比赛
void SpeechManager::initSpeech()
{
//容器清空
this->v1.clear();
this->v2.clear();
this->vVictory.clear();
this->m_Speaker.clear();
//记录比赛轮数
this->m_Index = 1;
//将记录的容器也清空
this->m_Record.clear();
}
//创建选手
void SpeechManager::createSpeech()
{
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < 12; i++)
{
string name = "选手";
name += nameSeed[i];
Speaker sp;
sp.m_Name = name;
for (int j = 0; j < 2; j++)
{
sp.m_Score[j] = 0;
}
this->v1.push_back(1001 + i);
this->m_Speaker.insert(make_pair(1001 + i, sp));
}
}
//开始比赛 1
void SpeechManager::startSpeech()
{
//第一轮比赛
//1、抽签
this->drawSpeech();
//2、比赛
this->contestSpeech();
//3、显示晋级结果
this->showScore();
//第二轮比赛
this->m_Index++;
//1、抽签
this->drawSpeech();
//2、比赛
this->contestSpeech();
//3、显示胜利结果
this->showScore();
//4、保存比赛记录
this->saveRecord();
//重置比赛,获取记录
//初始化比赛
this->initSpeech();
//创建12名选手
this->createSpeech();
//加载往届记录
this->loadRecord();
cout << "本届比赛完毕!" << endl;
system("pause");
system("cls");
}
//选手抽签 1-1
void SpeechManager::drawSpeech()
{
cout << "------- 第" << this->m_Index << "轮比赛选手正在抽签 ---------" << endl;
if (this->m_Index == 1)
{
random_shuffle(this->v1.begin(), this->v1.end());
//for (vector::iterator it = v1.begin(); it != v1.end(); it++)
//{
// cout << *it << " ";
//}
//cout << endl;
}
else
{
random_shuffle(this->v2.begin(), this->v2.end());
//for (vector::iterator it = v2.begin(); it != v2.end(); it++)
//{
// cout << *it << " ";
//}
//cout << endl;
}
}
//打分比赛 1-2
void SpeechManager::contestSpeech()
{
cout << "第" << this->m_Index << "轮比赛正式开始" << endl;
//准备临时容器 存放小组成员
multimap> groupScore;
int num = 0; //记录人员个数 6人一组
vector v;
if (this->m_Index == 1)
{
v = v1;
}
else
{
v = v2;
}
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
num++;
deque d;
for (int i = 0; i < 10; i++)
{
double score = (rand() % 401 + 600) / 10.f;
d.push_back(score);
}
sort(d.begin(), d.end(), greater());
d.pop_front();
d.pop_back();
double sum = accumulate(d.begin(), d.end(), 0.0f);
double avg = sum / (double)d.size();
this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;
groupScore.insert(make_pair(avg, *it));
//每6人取出前三名
if (num % 6 == 0)
{
cout << "第" << num / 6 << "小组比赛名次如下" << endl;
for (multimap>::iterator it = groupScore.begin(); it != groupScore.end(); it++)
{
cout << "编号:" << it->second
<< " 姓名:" << this->m_Speaker[it->second].m_Name
<< " 分数:" << this->m_Speaker[it->second].m_Score[this->m_Index - 1] << endl;
}
cout << endl;
int count = 0;
for (multimap>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++)
{
if (this->m_Index == 1)
{
v2.push_back(it->second);
}
else
{
vVictory.push_back(it->second);
}
}
groupScore.clear(); //小组临时容器清空
}
}
cout << "--------- 第" << this->m_Index << "轮比赛完毕!------------" << endl;
cout << endl;
}
//显示分数 1-3
void SpeechManager::showScore()
{
cout << "------- 第" << this->m_Index << "轮晋级选手如下:---------" << endl;
vector v;
if (this->m_Index == 1)
{
v = v2;
}
else
{
v = vVictory;
}
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << "选手编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 分数:" << this->m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
}
cout << endl;
system("pause");
system("cls");
this->showMenu();
}
//保存记录 1-4
void SpeechManager::saveRecord()
{
ofstream ofs;
ofs.open(SAVEFILE, ios::out | ios::app);
for (vector::iterator it = vVictory.begin(); it != vVictory.end(); it++)
{
ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << ",";
}
ofs << endl;
ofs.close();
//有记录后不为空
this->m_FileIsEmpty = false;
cout << "比赛记录已经保存" << endl;
}
//读取记录 2
void SpeechManager::loadRecord()
{
ifstream ifs(SAVEFILE, ios::in);
//文件不存在
if (!ifs.is_open())
{
this->m_FileIsEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
//文件为空
if (ifs.eof())
{
this->m_FileIsEmpty = true;
ifs.close();
return;
}
this->m_FileIsEmpty = false; //文件不为空
ifs.putback(ch);
string data;
int index = 0;
while (ifs >> data)
{
//cout << data << endl;
int pos = -1; //查找逗号的位置
int start = 0;
int len = 0;
string str;
vector v_str;
while (true)
{
//返回查找逗号的位置
pos = data.find(',', start);
if (pos == -1)
{
//没有找到的情况
break;
}
else
{
//计算截取的字符串长度
len = pos - start;
//截取逗号前的字符串
str = data.substr(start, len);
//添加到容器中
v_str.push_back(str);
//更新截取起始值
start = pos + 1;
}
}
this->m_Record.insert(make_pair(index, v_str));
//累计届数
index++;
}
ifs.close();
//for (map>::iterator it = this->m_Record.begin(); it != this->m_Record.end(); it++)
//{
// cout << "第" << it->first + 1 << "届比赛记录:"
// << " 冠军编号:" << it->second[0] << " 分数:" << it->second[1]
// << " 亚军编号:" << it->second[2] << " 分数:" << it->second[3]
// << " 季军编号:" << it->second[4] << " 分数:" << it->second[5] << endl;
//}
}
//显示往届记录
void SpeechManager::showRecord()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或为空" << endl;
}
else
{
for (int i = 0; i < this->m_Record.size(); i++)
{
cout << "第" << i + 1 << "届 "
<< "冠军编号:" << this->m_Record[i][0] << " 得分:" << this->m_Record[i][1] << " "
<< "亚军编号:" << this->m_Record[i][2] << " 得分:" << this->m_Record[i][3] << " "
<< "季军编号:" << this->m_Record[i][4] << " 得分:" << this->m_Record[i][5] << endl;
}
}
system("pause");
system("cls");
}
//清空比赛记录
void SpeechManager::clearRecord()
{
cout << "请确认是否要清空!" << endl;
cout << "1、确认\n2、返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
ofstream ofs(SAVEFILE, ios::trunc);
ofs.close();
//初始化比赛
initSpeech();
//创建选手
createSpeech();
//加载往届记录
loadRecord();
cout << "清空成功" << endl;
}
system("pause");
system("cls");
}
//退出系统 0
void SpeechManager::exitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
//析构函数
SpeechManager::~SpeechManager()
{
}
演讲比赛流程管理系统.cpp
#include "speechManager.h"
#include
int main()
{
srand((unsigned int)time(NULL));
SpeechManager sm;
int choice = 0;
while (true)
{
sm.showMenu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 1: //开始比赛
sm.startSpeech();
break;
case 2: //查看往届比赛记录
sm.showRecord();
break;
case 3: //清空比赛记录
sm.clearRecord();
break;
case 0: //退出系统
sm.exitSystem();
break;
default:
system("cls");//清屏
break;
}
}
system("pause");
return 0;
}