1. 演讲比赛程序需求
1.1 比赛规则
- 学校举行一场演讲比赛,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛
- 每名选手都有对应的编号,如10001~10012
- 比赛方式:分组比赛,每组6个人
- 第一轮分为两个小组,整体按照选手编号进行抽签后顺序演讲
- 十个评委分别给每名选手打分,去除最高分和最低分,求的平均分为本轮选手的成绩
- 当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛
- 第二轮为决赛,前三名胜出
- 每轮比赛过后需要显示晋级选手的信息
1.2 程序功能
- 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段
- 查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存
- 清空比赛记录:将文件中数据情况
- 退出比赛程序:可以退出当前程序
2. 代码
player.h
#pragma once
#include<iostream>
#include<cstring>
using namespace std;
class Player
{
public:
int m_Index; //选手编号
string m_Name; //选手姓名
double m_Score; //选手得分
public:
Player(int index, string name, double score);
~Player();
};
player.cpp
#include "player.h"
Player::Player(int index, string name, double score)
{
this->m_Index = index;
this->m_Name = name;
this->m_Score = score;
}
Player::~Player()
{
}
speechManager.h
#pragma once
#include <iostream>
#include <vector>
#include "player.h"
using namespace std;
class SpeechManager
{
public:
//构造函数
SpeechManager();
//析构函数
~SpeechManager();
public:
//展示菜单
void show_Menu();
//第一轮抽签
void first_Chouqian(vector<Player>& v);
//打分
void SetScore(vector<Player>& v);
//分组 排序
void MySort(vector<Player>& v);
//打印第一轮比赛结果
void show_first_result(vector<Player>& v1, vector<Player>& v2);
//第二轮抽签
void second_Chouqian(vector<Player>& v);
//打印第二轮比赛结果
void show_second_result(vector<Player>& v);
//写文件
void Save(const vector<Player>& v2);
//读文件
void Output();
//清空比赛记录
void Clear();
};
speechManager.cpp
#include "speechManager.h"
#include "player.h"
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
#include <deque>
#include <fstream>
SpeechManager::SpeechManager(){}
SpeechManager::~SpeechManager(){}
//打印菜单
void SpeechManager::show_Menu()
{
cout << "-------------------------------" << endl;
cout << "------- 欢迎参加演讲比赛 -------" << endl;
cout << "------- 1. 开始演讲比赛 -------" << endl;
cout << "------- 2. 查看往届记录 -------" << endl;
cout << "------- 3. 清空比赛记录 -------" << endl;
cout << "------- 0. 退出比赛程序 -------" << endl;
cout << "-------------------------------" << endl;
}
//打印vector
void printVector(const vector<Player>& v)
{
for(vector<Player>::const_iterator it = v.begin(); it != v.end(); ++it)
{
cout << "编号" << it->m_Index << "\t姓名: " << it->m_Name
<< "\t成绩: " << it->m_Score << endl;
}
}
//第一轮抽签
void SpeechManager::first_Chouqian(vector<Player>& v)
{
srand((unsigned)time(NULL));
cout << "第 << 1 >> 轮比赛选手正在抽签" << endl;
random_shuffle(v.begin(), v.end());
cout << "---------------------------" << endl;
cout << "抽签后演讲顺序如下: " << endl;
for(vector<Player>::const_iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Index << " ";
}
cout << endl;
cout << "---------------------------" << endl;
system("pause");
}
//按照成绩降序排列
class MyCompare
{
public:
bool operator()(Player &p1, Player &p2)
{
return p1.m_Score > p2.m_Score;
}
};
//打分
void SpeechManager::SetScore(vector<Player>& v)
{
srand((unsigned)time(NULL));
int temp_score = 0; //打分
//评委打分完毕
for(vector<Player>::iterator it = v.begin(); it != v.end(); ++it)
{
deque<int> d; //用来保存10位评委的评分
double score = 0; //最终得分
for(int i = 0; i < 10; ++i)
{
temp_score = 60 + rand() % 41;
d.push_back(temp_score);
}
sort(d.begin(), d.end());
d.pop_back(); //去掉一个最高分
d.pop_front(); //去掉一个最低分
for(deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
score += *dit;
}
score /= 8;
it->m_Score = score;
}
}
//分组 排序
void SpeechManager::MySort(vector<Player>& v)
{
//对结果需要进行排序 按照选手得分进行降序排序
//排序之前先要将12个人分成两组,每组6个人,组内进行排序
vector<Player>::iterator it = v.begin();
for(int i = 0; i < 6; ++i)
{
it++;
}
sort(v.begin(), it, MyCompare()); //对1-6名进行排序
sort(it, v.end(), MyCompare()); //对7-12名进行排序
}
//打印第一轮比赛结果 并淘汰每组最后三名选手
void SpeechManager::show_first_result(vector<Player>& v1, vector<Player>& v2)
{
cout << "------- 第1轮比赛正式开始 -------" << endl;
cout << "第1小组比赛名次: " << endl;
vector<Player>::iterator it = v1.begin();
for(int i = 0; i < 6; ++i)
{
if(i < 3) //每一组的前三名晋级
{
v2.push_back(*it);
}
cout << "编号" << it->m_Index << "\t姓名: " << it->m_Name
<< "\t成绩: " << it->m_Score << endl;
it++;
}
cout << endl;
cout << "第2小组比赛名次: " << endl;
for(int i = 0; i < 6; ++i)
{
if(i < 3) //每一组的前三名晋级
{
v2.push_back(*it);
}
cout << "编号" << it->m_Index << "\t姓名: " << it->m_Name
<< "\t成绩: " << it->m_Score << endl;
it++;
}
cout << endl;
cout << "------- 第1轮比赛完毕 -------" << endl;
system("pause");
cout << "------- 第1轮晋级选手信息如下: -------" << endl;
for(vector<Player>::iterator it = v2.begin(); it != v2.end(); ++it)
{
cout << "编号" << it->m_Index << "\t姓名: " << it->m_Name
<< "\t成绩: " << it->m_Score << endl;
}
cout << endl;
system("pause");
}
//第二轮抽签
void SpeechManager::second_Chouqian(vector<Player>& v)
{
srand((unsigned)time(NULL));
cout << "第 << 2 >> 轮比赛选手正在抽签" << endl;
random_shuffle(v.begin(), v.end());
cout << "---------------------------" << endl;
cout << "抽签后演讲顺序如下: " << endl;
for(vector<Player>::const_iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Index << " ";
}
cout << endl;
cout << "---------------------------" << endl;
system("pause");
}
//打印第二轮比赛结果
void SpeechManager::show_second_result(vector<Player>& v)
{
sort(v.begin(), v.end(), MyCompare());
cout << "------- 第2轮比赛正式开始 -------" << endl;
cout << "第1小组比赛名次: " << endl;
printVector(v);
cout << endl;
cout << "------- 第2轮比赛完毕 -------" << endl;
v.pop_back();
v.pop_back();
v.pop_back();
system("pause");
cout << "------- 第2轮晋级选手信息如下: -------" << endl;
for(vector<Player>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << "编号" << it->m_Index << "\t姓名: " << it->m_Name
<< "\t成绩: " << it->m_Score << endl;
}
system("pause");
}
//保存文件
void SpeechManager::Save(const vector<Player>& v)
{
ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app);
for(vector<Player>::const_iterator it = v.begin(); it != v.end(); ++it)
{
ofs << it->m_Index << "," << it->m_Score << ",";
}
ofs << endl;
ofs.close();
cout << "记录已经保存" << endl;
cout << "本届比赛完毕!" << endl;
system("pause");
}
//读文件
void SpeechManager::Output()
{
ifstream ifs;
ifs.open("speech.csv", ios::in);
if(!ifs.is_open())
{
cout << "文件打开失败" << endl;
}
string buf;
int counter = 1; //记录一共有多少行
while(getline(ifs, buf))
{
string index;
string score;
for(int i = 0; i < 3; ++i)
{
int pos = buf.find(',');
index = buf.substr(0, pos);
buf = buf.erase(0, pos + 1);
pos = buf.find(',');
score = buf.substr(0, pos);
buf = buf.erase(0, pos + 1);
if(i == 0)
{
cout << "第" << counter << "届 冠军编号: " << index << "\t 得分: " << score << " ";
}
if(i == 1)
{
cout << "\t亚军编号: " << index << "\t 得分: " << score << " ";
}
if(i == 2)
{
cout << "\t季军编号: " << index << "\t 得分: " << score << endl;
}
}
counter++;
}
system("pause");
system("cls");
}
//清空比赛记录
void SpeechManager::Clear()
{
cout << "--- 确认清空? ---" << endl;
cout << "--- 1. 确认 ---" << endl;
cout << "--- 2. 返回 ---" << endl;
int choice;
cin >> choice;
if(choice == 1)
{
//打开模式 ios::trunc 如果文件存在,先删除,再重建
ofstream ofs("speech.csv", ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
main.cpp
#include <iostream>
#include <vector>
#include "speechManager.h"
#include "player.h"
using namespace std;
int main()
{
vector<Player>v1; //第一轮比赛选手
vector<Player>v2; //第二轮比赛选手 获胜选手也保存在v2中
string nameside = "ABCDEFGHIJKL";
int index = 10001;
for(int i = 0; i < 12; ++i)
{
string name = "选手";
name += nameside[i];
Player p(index, name, 0);
v1.push_back(p);
index++;
}
SpeechManager s;
while(1)
{
s.show_Menu();
cout << "请输入您的选择: ";
int choice = 0;
cin >> choice;
switch(choice){
case 0:
cout << "成功退出!" << endl;
exit(0);
break;
case 1:
{
s.first_Chouqian(v1); //第一轮抽签
s.SetScore(v1); //打分
s.MySort(v1); //分组 排序
s.show_first_result(v1, v2); //第一轮比赛结果
s.second_Chouqian(v2); //第二轮抽签
s.SetScore(v2); //打分
s.show_second_result(v2); //第二轮比赛结果
s.Save(v2); //保存文件
}
break;
case 2:
s.Output();
break;
case 3:
s.Clear();
break;
default:
break;
}
}
}
3. 效果演示