本文主要是介绍【C++】基于STL演讲比赛流程管理系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 一、比赛规则
- 二、比赛功能
- 三、全部代码
- 1. test.cpp
- 2. Speecher.h
- 3. SpeecherManager.h
- 4. SpeecherManager.cpp
💙 本管理系统参考黑马C++课程B站课程指路,仅作参考与学习资料。
🧡 核心课程笔记 👉C++核心
🧡 提高课程笔记 👉C++提高
一、比赛规则
- 学校举行一场演讲比塞,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。每名选手都有对应的编号。如10001 ~10012。
- 比赛方式:分组比赛,每组6个人。
- 十个评委分别给每名选手打分,去除最高分和最低分,求的平均分为本轮选手的成绩当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。第二轮为决赛,前三名胜出。
- 每轮比赛过后需要显示晋级选手的信息。
二、比赛功能
- 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段
- 查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用
.csv
后缀名保存 - 清空比赛记录:将文件中数据清空
- 退出比赛程序:可以退出当前程序
[ 注意 ]:csv
格式可以用excel或记事本方式打开。
三、全部代码
1. test.cpp
#include "Speecher.h"
#include "SpeechManager.h"
#include<iostream>
using namespace std;
int main()
{SpeechManager sm;sm.SpeechMeun();int choose = 0;srand((unsigned int)time(NULL));do{cout << "请输入:";cin >> choose;switch (choose){case 1:sm.StartSpeech();break;case 2:sm.ShowRecord();break;case 3:sm.ClearRecord();break;case 0:sm.ExitManager();break;default:cout << "输入有误,请重新输入" << endl;break;}} while (choose);return 0;
}
2. Speecher.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Speecher
{
public:string name;double score[2];//两次分数:第一次为晋级分数,第二次为决赛分数
};
3. SpeecherManager.h
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include <deque>
#include <algorithm>
#include <functional>
#include <numeric>
#include <fstream>
#include "Speecher.h"
using namespace std;
//演讲比赛管理类
class SpeechManager
{
public:SpeechManager();//构造~SpeechManager();//析构//菜单功能void SpeechMeun();//初始化void InitManager();//创建12位选手void CreatSpeaker();//开始比赛void StartSpeech(){//第一轮比赛//1.抽签this->SpeechDraw();//2.比赛this->SpeechContest();//3.公布晋级选手this->ShowScore();this->idex++;//1.抽签this->SpeechDraw();//2.比赛this->SpeechContest();//3.最终结果this->ShowScore();//4.最终结果保存在文件中this->SaveRecord();cout << "本届比赛完毕!" << endl;this->InitManager();this->CreatSpeaker();this->LoadRecord();}void SpeechDraw();//选手抽签void SpeechContest();//选手比赛void ShowScore();//晋级分数或决赛分数void SaveRecord();//保存决赛前三成绩void ShowRecord();//展示往届决赛成绩void LoadRecord();//读取往届成绩void ClearRecord();//清空数据void ExitManager();//退出vector<int> v1;//12个选手的编号vector<int> v2;//第一轮晋级选手的编号vector<int> victory;//胜利前三名选手的编号map<int, Speecher> m_speaker;//编号 和 对应的选手int idex;//比赛轮数bool FileIsEmpty;map<int, vector<string>> m_Record;//往届记录,包括:届数和每届冠军季军亚军
};
注意:
4. SpeecherManager.cpp
#include "SpeechManager.h"
SpeechManager::SpeechManager()
{this->InitManager();this->CreatSpeaker();this->LoadRecord();
}
void SpeechManager::SpeechMeun()
{cout << "************************" << endl;cout << "*****1.开始演讲比赛*****" << endl;cout << "*****2.查看往届记录*****" << endl;cout << "*****3.清空比赛记录*****" << endl;cout << "*****0. 退出程序 *****" << endl;cout << "************************" << endl;
}
void SpeechManager::InitManager()
{this->v1.clear();this->v2.clear();this->victory.clear();this->m_speaker.clear();this->idex = 1;
}
void SpeechManager::CreatSpeaker()
{string name_seed = "ABCDEFGHIJKL";for (int i = 0; i < 12; i++){string name = "选手";name = name + name_seed[i];Speecher s;s.name = name;s.score[0] = 0;s.score[1] = 0;this->v1.push_back(i+1001);this->m_speaker.insert(make_pair(i + 1001, s));}}
void SpeechManager::SpeechDraw()
{cout << "第" << this->idex << "轮比赛" << endl;cout << "选手抽签结果:" << endl;if (this->idex == 1){random_shuffle(v1.begin(), v1.end());for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)cout << *it << " ";}else{random_shuffle(v2.begin(), v2.end());for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)cout << *it << " ";}cout << endl;cout << "---------------------------------------------" << endl;
}
void SpeechManager::SpeechContest()
{multimap<double, int, greater<double>> gourp_temp;//临时小组 cout << "第" << this->idex << "轮比赛" << endl;vector<int> v_temp;if (this->idex == 1)v_temp = v1;elsev_temp = v2;int num = 0;for (vector<int>::iterator it = v_temp.begin(); it != v_temp.end(); it++){num++;deque<double> score;double each_score = 0.0;//十个评委打分for (int i = 0; i < 10; i++){each_score=(rand() % 401 + 600) / 10.f;score.push_back(each_score);}//去掉最高分与最低分 1.排序 2.pop首尾sort(score.begin(), score.end(), greater<double>());score.pop_back();score.pop_front();//求平均分double avg= accumulate(score.begin(), score.end(), 0)/ (double)score.size();//将分数放入map中this->m_speaker[*it].score[this->idex - 1] = avg;//分数和个人放在maltimap中gourp_temp.insert(make_pair(avg, *it));if (num % 6 == 0){//输出当前小组成绩cout << "第"<<num/6<<"组小组成绩:" << endl;for (multimap<double, int, greater<double>>::iterator it = gourp_temp.begin(); it != gourp_temp.end(); it++){cout << "编号:" << it->second << " 姓名: " << this->m_speaker[it->second].name<< " 成绩:" << m_speaker[it->second].score[this->idex - 1] << endl;}//取前三名int count = 0;for (multimap<double, int, greater<double>>::iterator it = gourp_temp.begin(); it != gourp_temp.end()&&count!=3; it++,count++){if (this->idex == 1){v2.push_back((*it).second);}else{victory.push_back((*it).second);}}gourp_temp.clear();}}cout << "----------第" << this->idex << "轮比赛结束-----------" << endl;
}
void SpeechManager::ShowScore()
{//如果是晋级if (this->idex == 1){cout << "晋级名单:" << endl;for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)cout << "编号:" << *it << "姓名:" << m_speaker[*it].name << "成绩:" << m_speaker[*it].score[this->idex - 1] << endl;}//如果是决赛else{cout << "最终结果:" << endl;for (vector<int>::iterator it = victory.begin(); it != victory.end(); it++)cout << "编号:" << *it << "姓名:" << m_speaker[*it].name << "成绩:" << m_speaker[*it].score[this->idex - 1] << endl;}
}
void SpeechManager::SaveRecord()
{ofstream ofs;ofs.open("speech.csv", ios::out | ios::app);//写文件,用追加(app)方式//将每个选手信息,写入到文件中for (vector<int>::iterator it = victory.begin(); it != victory.end(); it++){//csv格式需要用,分割ofs << *it << "," << this->m_speaker[*it].score[1] << ",";}ofs << endl;ofs.close();//关闭文件this->FileIsEmpty = false;cout << "记录已经保存" << endl;
}
void SpeechManager::ShowRecord()
{if (this->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;}}
}
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;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);v.push_back(temp);start = pos + 1;}this->m_Record.insert(make_pair(index, v));index++;}ifs.close();
}
void SpeechManager::ClearRecord()
{cout << "是否确定清空文件" << endl;cout << "1.是" << endl;cout << "2.否" << endl;int select = 0;cin >> select;if (select == 1){ofstream ofs("speech.csv", ios::trunc);ofs.close();this->InitManager();this->CreatSpeaker();this->LoadRecord();cout << "清空成功" << endl;}else{}
}
void SpeechManager::ExitManager()
{cout << "欢迎下次使用" << endl;exit(0);
}
SpeechManager::~SpeechManager()
{}
这篇关于【C++】基于STL演讲比赛流程管理系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!