比赛规则:
学校举行一场演讲比赛,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。
每名选手都有对应的编号,如 10001~ 10012。
比赛方式: 分组比赛,每组6个人。
第一轮分为两个小组,整体按照选手编号进行抽签后顺序演讲。
十个评委分别给每名选手打分,去除最高分和最低分,求的平均分为本轮选手的成绩。
当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。
第二轮为决赛,前三名胜出。
每轮比赛过后需要显示晋级选手的信息。
题目要求:
开始演讲比赛: 完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段。
查看往届记录: 查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存。
清空比赛记录: 将文件中数据清空。
退出比寒程序: 可以退出当前程序。
项目展示:
数据保存到csv文件中。
本人用的软件:vs2022
代码结构:
首先新建一个控制台程序,起个名字,我的是:“Speech Competition Management System”
然后会自带一个cpp文件,这个cpp就是主函数(Speech Competition Management System.cpp),内容:
#include <iostream>
#include"speechManager.h"
using namespace std;
int main()
{
//随机数种子
srand(time(0));
//记录用户的输入
int choice = 0;
//循环
while (true) {
SpeechManager sm;
sm.show_Menu();
cout << "请输入你的选择:" << endl;
cin >> choice;
switch (choice)
{
case 1:
//开始演讲比赛
sm.startSpeech();
break;
case 2:
//查看往届记录
sm.showRecord();
break;
case 3:
//清空比赛记录
sm.clearRecord();
break;
case 0:
//退出比赛程序
sm.exit_System();
break;
default:
break;
}
}
}
下面是.h文件:
speaker.h
#pragma once
#include<iostream>
using namespace std;
//选手类
class Speaker {
public:
//选手姓名
string m_Name;
//选手分数
double m_Score[2];
};
speechManager.h
#pragma once
#include<iostream>
#include<vector>
#include<map>
#include<deque>
#include<functional>
#include<numeric>
#include<algorithm>
#include <fstream>
#include"speaker.h"
using namespace std;
class SpeechManager {
public:
//构造函数的定义
SpeechManager();
//菜单成员函数
void show_Menu();
//退出功能
void exit_System();
//初始化函数
void initSpeech();
//创建选手
void createSpeaker();
//开始比赛
void startSpeech();
//抽签
void speechDraw();
//比赛
void speechContest();
//显示得分函数
void showScore();
//保存文件函数
void saveRecord();
//读取记录
void loadRecord();
//文件为空的标志
bool fileIsEmpty;
//往届记录
map<int, vector<string>>m_Record;
//显示往届得分
void showRecord();
//清空文件
void clearRecord();
//析构函数的定义
~SpeechManager();
//成员属性
//比赛选手
vector<int>v1;
//第一轮晋级
vector<int>v2;
//胜利前三名
vector<int>vVectory;
//编号对应的具体选手
map<int, Speaker>m_Speaker;
//记录比赛轮数
int m_Index;
};
下面是cpp文件
speechManager.cpp
#include"speechManager.h"
//构造函数的实现
SpeechManager::SpeechManager() {
//初始化
this->initSpeech();
//创建演讲者
this->createSpeaker();
//加载文件
this->loadRecord();
}
//菜单函数的实现
void SpeechManager::show_Menu() {
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->vVectory.clear();
this->m_Speaker.clear();
//初始化比赛人数
this->m_Index = 1;
this->m_Record.clear();
}
//创建选手实现
void SpeechManager::createSpeaker() {
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < nameSeed.size(); i++)
{
string name = "选手";
//给选手命名
name += nameSeed[i];
Speaker sp;
//将姓名赋值给sp对象里面的成员
sp.m_Name = name;
//初始化分数为0
for (int i = 0; i < 2; i++)
{
sp.m_Score[i] = 0;
}
//将编号放入v1容器中
this->v1.push_back(i + 10001);
//将编号和对象放入map容器中
this->m_Speaker.insert(make_pair(i + 10001, sp));
}
}
//开始比赛实现
void SpeechManager::startSpeech() {
//第一轮比赛
// 1、抽签
speechDraw();
// 2、比赛
speechContest();
// 3、显示晋级结果
showScore();
// 第二轮比赛
this->m_Index++;
// 1、抽签
speechDraw();
// 2、比赛
speechContest();
// 3、显示最终结果
showScore();
// 4、保存分数
saveRecord();
//重置比赛
this->initSpeech();
this->createSpeaker();
this->loadRecord();
cout << "本届比赛完毕!" << endl;
system("pause");
system("cls");
}
//抽签功能实现
void SpeechManager::speechDraw() {
cout << "第" << "<<" << this->m_Index << ">>" << "轮比赛选手正在抽签" << endl;
cout << "----------------" << endl;
cout << "抽签后演讲顺序如下:" << endl;
//判断是否是第一轮
if (this->m_Index == 1) {
//随机函数调用
random_shuffle(v1.begin(), v1.end());
//遍历v1打印抽签的序号
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
else
{
random_shuffle(v2.begin(), v2.end());
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
system("pause");
cout << endl;
}
//比赛功能实现
void SpeechManager::speechContest() {
cout <<"-----------------" << "第" << m_Index << "轮比赛正式开始" << "-----------------" << endl;
//临时容器,存放小组信息
multimap<double, int, greater<double>> groupScore;
int num = 0;
//比赛选手容器
vector<int>v_Src;
//判断是否第一轮
if (m_Index == 1) {
v_Src = v1;
}
else
{
v_Src = v2;
}
//遍历所有选手的比赛
for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {
num++;
//评委打分
deque<double>d;
for (int i = 0; i < 10; i++)
{
//生成400到1000之间的整数再除以10
double score = (rand() % 401 + 600) / 10.f;
//cout << score << " ";
//将分数放入容器
d.push_back(score);
}
//cout << endl;
//降序排序
sort(d.begin(), d.end(), greater<double>());
d.pop_front();//去除最高分
d.pop_back();//去除最低分
//总分
double sum = accumulate(d.begin(), d.end(), 0.0f);
//平均分
double avg = sum / (double)d.size();
//打印平均分
//cout << "编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name <<
//" 平均分:" << avg<<endl;
//将平均分放入map容器中
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<double, int, greater<double>>::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;
}
//取走前三名
int count = 0;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end()&&count<3; it++,count++) {
if (this->m_Index == 1) {
v2.push_back((*it).second);
}
else
{
vVectory.push_back((*it).second);
}
}
//清空
groupScore.clear();
cout << endl;
}
}
cout << "---------------------第" << this->m_Index << "轮比赛完成!---------------------"<<endl;
system("pause");
}
//显示得分函数实现
void SpeechManager::showScore() {
cout << "---------------------第" << this->m_Index << "轮选手晋级信息如下:---------------------" << endl;
vector<int>v;
if (this->m_Index == 1) {
v = v2;
}
else
{
v = vVectory;
}
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << "选手编号:"
<< *it << " 姓名:"
<< m_Speaker[*it].m_Name
<< " 得分:" << m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
}
cout << endl;
system("pause");
system("cls");
this->show_Menu();
}
//保存文件函数
void SpeechManager::saveRecord(){
ofstream ofs;
//用输出的方式打开文件---写文件
ofs.open("speech.csv", ios::out | ios::app);
//将每个人数据写入到文件中
for (vector<int>::iterator it = vVectory.begin(); it != vVectory.end(); it++) {
ofs << *it << "," << m_Speaker[*it].m_Score[1] << ",";
}
ofs << endl;
//关闭文件
ofs.close();
cout << "记录已保存" << endl;
this->fileIsEmpty = false;
}
//读取函数
void SpeechManager::loadRecord() {
ifstream ifs("speech.csv", ios::in);
if (!ifs.is_open()) {
this->fileIsEmpty = true;
//cout << "文件不存在!" << endl;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof()) {
//cout << "文件为空!" << endl;
this->fileIsEmpty = true;
ifs.close();
return;
}
//文件不为空
this->fileIsEmpty = false;
ifs.putback(ch);
string data;
//第几届
int index = 0;
while (ifs >> data) {
//容器
vector<string>v;
//判断是否找到
int pos = -1;
//开始位置
int start = 0;
while (true) {
pos = data.find(",", start);
if (pos == -1) {
//没有找到
break;
}
string temp = data.substr(start, pos - start);
//cout << temp << endl;
//放入容器
v.push_back(temp);
start = pos + 1;
}
this->m_Record.insert(make_pair(index, v));
index++;
}
ifs.close();
}
//显示往届得分
void SpeechManager::showRecord() {
if (this->fileIsEmpty) {
cout << "文件不存在,或记录为空!" << endl;
}
else
{
for (map<int, vector<string>>::iterator it = m_Record.begin(); it != m_Record.end(); it++) {
cout << "------第" << it->first + 1 << "届大赛信息如下:------" << endl;
cout << "冠军编号:" << it->second[0] << " 冠军得分:" << it->second[1] << endl;
cout << "亚军编号:" << it->second[2] << " 亚军得分:" << it->second[3] << endl;
cout << "季军编号:" << it->second[4] << " 季军得分:" << it->second[5] << endl;
}
}
system("pause");
system("cls");
}
//清空文件函数实现
void SpeechManager::clearRecord() {
if (fileIsEmpty) {
cout << "文件不存在,或记录为空!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "你确认要清空吗?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
int choice = 0;
cin >> choice;
switch (choice)
{
case 1:
{
// 使用输出模式打开文件,没有内容写入,这样就可以清空文件
ofstream ofs("speech.csv", ios::out | ios::trunc);
if (!ofs.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
ofs.close();
this->initSpeech();
this->createSpeaker();
this->loadRecord();
cout << "已经成功清空文件!" << endl;
system("pause");
system("cls");
break;
}
case 2:
{
system("pause");
system("cls");
break;
}
default:
cout << "您的输入有误,请重新输入" << endl;
clearRecord();
break;
}
}
}
//退出功能的实现
void SpeechManager::exit_System() {
cout << "欢迎下次使用!" << endl;
system("pause");
system("cls");
exit(0);
}
//析构函数的实现
SpeechManager::~SpeechManager() {
}
有问题欢迎联系我,qq2955686813