> 文章列表 > C++演讲比赛流程管理系统_黑马

C++演讲比赛流程管理系统_黑马

C++演讲比赛流程管理系统_黑马

  • 任务

学校演讲比赛,12人,两轮,第一轮淘汰赛,第二轮决赛
选手编号 [ 10001 - 10012 ]
分组比赛 每组6人
10个评委 去除最高分 最低分,求平均分 为该轮成绩
每组淘汰后三名,前三名晋级决赛
决赛 前三名胜出
每轮 比赛过后 要显示晋级选手信息

(点击此处-下载源代码)

  • 功能

  1. 开始演讲比赛:完成 一整届比赛流程,每阶段给用户提示,任意键进入下一阶段
  2. 查看往届记录:查看比赛前三名结果,每次比赛都记录到文件中,用*.csv存储
  3. 清空记录:将文件中数据清空
  4. 退出程序:将文件中数据清空
  • 效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

speechManager.h

#pragma once
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <deque>
#include <functional>
#include <numeric>
#include <fstream>
#include"speaker.h"
using namespace std;
/*1.提供惨淡界面与用户交互2.对演讲比赛流程进行控制3.与文件读写交互
*/
class SpeechManager
{
public:vector<int> v1;				//比赛选手		12人 第一轮vector<int> v2;				//第一轮晋级		6人vector<int> vVectory;		//晋级			3人map<int, Speaker> m_Speaker;//编号,具体选手	map容器int m_Index;				//比赛轮数map<int, vector<string>> m_Record;		//存放往届记录bool fileIsEmpty;			//file是否为空SpeechManager();void show_Menu();void initSpeech();			//初始化容器属性void createSpeaker();		//创建12名选手void exit_Sys_0();void speechDraw();			//抽签void speechContest();		//竞赛void showScore();			//显示得分void saveRecord();			//Save 记录void startSpeech_1();			//开始整个流程void loadRecode();			//读取往届记录void showRecode_2();			//查看记录void clearRecode_3();~SpeechManager();
};class printVectorInt{
public:void operator()(int val) {cout << val << " ; ";}
};

speaker.h

#pragma once
#include <iostream>
using namespace std;class Speaker
{
public:void setSpeakerName(string name);void setSpeakerS1(double a);void setSpeakerS2(double a);string getName();double* getScore();private:string m_Name;double* m_Score = new double[2];		//两轮得分,数组
};

SpeechCompetition_Sys.cpp

#include<iostream>
#include<string>
#include <ctime>
#include"speechManager.h"
#include"speaker.h"using namespace std;int main() {srand((unsigned int)time(NULL));SpeechManager sm;
//test://for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); ++it) {//	double* arr = it->second.getScore();//	cout << "Test ID: " << it->first//		<< "\\t Name: " << it->second.getName()//		<< "\\t Score: " << arr[0] << endl;//}int inputChoice = -1;while (1) {sm.show_Menu();cout << "Please input Your Choice :" << endl;cin >> inputChoice;switch (inputChoice) {case 1:		//开始比赛sm.startSpeech_1();break;case 2:		//往届记录sm.showRecode_2();break;case 3:		//清空记录sm.clearRecode_3();break;case 0:		//退出sm.exit_Sys_0();break;default:system("cls");break;}}system("pause");return 0;
}

speaker.cpp

#include"speaker.h"
#include<string>
using namespace std;void Speaker::setSpeakerName(string name) {this->m_Name = name;
}void Speaker::setSpeakerS1(double a) {this->m_Score[0] = a;
}void Speaker::setSpeakerS2(double a) {this->m_Score[1] = a;
}string Speaker::getName() {return m_Name;
}
double* Speaker::getScore() {return this->m_Score;
/*Speaker s;string name = "GodOuO";double score[2] = { 12,34.5 };s.setSpeaker(name, score);double* arr = s.getScore();cout << arr[0] << arr[1];delete[] arr;
*/
}

speechManager.cpp

#include"speechManager.h"SpeechManager::SpeechManager()
{this->initSpeech();		//初始化容器属性this->createSpeaker();	//创建12名选手this->loadRecode();		//加载数据
}void SpeechManager::show_Menu() {cout << "*******************************" << endl<< "*****SpeechCompetition_Sys*****" << endl<< "**********#1.Start   **********" << endl<< "**********#2.Histry  **********" << endl<< "**********#3.Clean   **********" << endl<< "**********#0.Quit    **********" << endl << endl;
}void SpeechManager::exit_Sys_0() {cout << "Bye Bye !!!" << endl;system("pause");exit(0);
}void SpeechManager::initSpeech() {//容器置空this->v1.clear();this->v2.clear();this->vVectory.clear();this->m_Speaker.clear();//index置空this->m_Index = 1;this->m_Speaker.clear();
}void SpeechManager::createSpeaker() {string nameSpace = "ABCDEFGHIJKL";for (int i = 0; i < nameSpace.length(); ++i){string name = "Name";name += nameSpace[i];Speaker s;s.setSpeakerName(name);s.setSpeakerS1(0.0);s.setSpeakerS2(0.0);this->v1.push_back(i + 10001);		//选手编号 存入v1this->m_Speaker.insert(make_pair(i + 10001, s));//选手编号 和对应选手 存入map}
}void SpeechManager::speechDraw() {cout << "The No.(" << this->m_Index << ") Round's Player is Drawing..." << endl<< "-----------------------------------------------------" << endl<< "The Order is :" << endl;if (1 == this->m_Index) {			//Round 1random_shuffle(v1.begin(),v1.end());for_each(v1.begin(), v1.end(), printVectorInt());}else if (2 == this->m_Index) {		//Round 2random_shuffle(v2.begin(), v2.end());for_each(v2.begin(), v2.end(), printVectorInt());}cout <<endl<< "-----------------------------------------------------" << endl;system("pause");cout << endl;
}void SpeechManager::speechContest() {cout << "The No.(" << this->m_Index << ") Round's Contest is Fighting..." << endl<< "-----------------------------------------------------" << endl;vector<int> v_Player;		//比赛选手容器multimap<double, int, greater<double>> groupScore;		//准备临时容器,存放小组成绩int num = 0;		//记录人员个数	6人一组if (1 == this->m_Index) {v_Player = this->v1;}else if (2 == this->m_Index) {v_Player = this->v2;}for (vector<int>::iterator it= v_Player.begin(); it != v_Player.end(); ++it){	//遍历所有选手deque<double> d;		//评委打分++num;					//统计人数for (int i = 0; i < 10; ++i){double score = (rand() % 401 + 600) / 10.f;			//(600-1000) /10.f//test:// cout<<"Test:"<<endl;//cout << score << " ; ";d.push_back(score);}//cout << endl;sort(d.begin(), d.end(), greater<double>());	//倒叙排序d.pop_front();		//del Topd.pop_back();		//del Lastdouble sum = accumulate(d.begin(), d.end(), 0.0f);		//起始累加值 0.0f小数double avg = sum / (double)d.size();					//平均分if (1 == m_Index)this->m_Speaker[*it].setSpeakerS1(avg);			//平均分 存入 map容器else if(2 == m_Index)this->m_Speaker[*it].setSpeakerS2(avg);			//平均分 存入 map容器groupScore.insert(make_pair(avg, *it));				//key =平均成绩,value =IDif (0 == num % 6) {			//每六个人 取前三cout << "No.<" << num / 6 << "> group‘s Contest List: " << endl<< "-----------------------------------------------------" << endl;for (multimap<double,int,greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); ++it){double* arr = this->m_Speaker[it->second].getScore();cout << "ID: " << it->second<< "\\tName: " << this->m_Speaker[it->second].getName()<< "\\tScore: " << arr[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 (1 == this->m_Index)this->v2.push_back((*it).second);else this->vVectory.push_back((*it).second);}groupScore.clear();		//del 缓存 组成绩cout << endl;}/*cout << "Test:" << endl;double* arr = this->m_Speaker[*it].getScore();cout << "ID: " << *it<< "\\tName: " << this->m_Speaker[*it].getName()<< "\\tScore: " << arr[0];*/}			cout << "No.(" << this->m_Index << ") group‘s Contest Done!!! " << endl;system("pause");cout << endl;
}void SpeechManager::showScore() {cout << "The No.(" << this->m_Index << ") Round's Winner List:" << endl<< "-----------------------------------------------------" << endl;vector<int> v;if (1 == this->m_Index){v = v2;}else if (2 == this->m_Index) {v = vVectory;}for (vector<int>::iterator it = v.begin(); it!= v.end(); ++it){double* arr = this->m_Speaker[*it].getScore();cout << "ID: " << *it<< "\\tName: " << this->m_Speaker[*it].getName()<< "\\tScore: " << arr[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)	//数据写入文件{double* arr = this->m_Speaker[*it].getScore();		//冠亚季三人ofs << *it << "," << arr[1] << ",";}ofs << endl;ofs.close();this->fileIsEmpty = false;cout << "Save it!!!" << endl;
}void SpeechManager::startSpeech_1(){//第一轮比赛:1.抽签;2.比赛;3.显示结果this->speechDraw();this->speechContest();this->showScore();//第二轮比赛:1.抽签;2.比赛;3.显示结果++m_Index;this->speechDraw();this->speechContest();this->showScore();//保存分数到文件this->saveRecord();//重置比赛,获取记录this->initSpeech();		//初始化容器属性this->createSpeaker();	//创建12名选手this->loadRecode();		//加载数据cout << endl << "This Round is Finished!!!" << endl;system("pause");system("cls");
}void SpeechManager::showRecode_2() {if (this->fileIsEmpty){cout << "File is Empty!!!" << endl;}else {for (int i = 0; i < this->m_Record.size(); ++i){cout << " NO.(" << i + 1 << ") Round Champion's ID: " << this->m_Record[i][0]<< "\\tScore: " << this->m_Record[i][1] << endl;cout << " NO.(" << i + 1 << ") Round <No.2>'s ID: " << this->m_Record[i][2]<< "\\tScore: " << this->m_Record[i][3] << endl;cout << " NO.(" << i + 1 << ") Round <No.3>'s ID: " << this->m_Record[i][4]<< "\\tScore: " << this->m_Record[i][5] << endl;}}system("pause");system("cls");
}void SpeechManager::loadRecode() {ifstream ifs("speech.csv", ios::in);if (!ifs.is_open())		//文件不存在{this->fileIsEmpty = true;//cout << "File is NOT Exist !!!" << endl;ifs.close();return;}//文件清空char c;ifs >> c;if (ifs.eof()) {this->fileIsEmpty = true;//cout << "File is Empty !!!" << endl;ifs.close();return;}this->fileIsEmpty = false;ifs.putback(c);			//将上文读取单个字符 存回string data; int index = 0;			//第几届while (ifs >> data) {//test://cout << data << endl;int loc = -1;			//查到’,‘ locint start = 0;vector<string> vtemp;		//存放 6个 string 字符串while (1){loc = data.find(",", start);if (-1 == loc) {			//未找到 ’,‘break;}string temp = data.substr(start, loc - start);//test://cout << temp << endl;vtemp.push_back(temp);start = loc + 1;}this->m_Record.insert(make_pair(index, vtemp));++index;}ifs.close();// test:// for (map<int,vector<string>>::iterator it = this->m_Record.begin(); it != this->m_Record.end() ; ++it)//{//	cout << it->first//		<< " Winner ID: " << it->second[0]//		<< "\\tScore: " << it->second[1] << endl;//}}void SpeechManager::clearRecode_3(){cout << endl << "Sure ?" << endl << "1.Yes" << endl << "2.No" << endl;int select = 0;cin >> select;if (1 == select) {//ios::trunc 如果文件存在,删除文件 重新创建ofstream ofs("speech.csv", ios::trunc);ofs.close();//初始化this->initSpeech();		//初始化容器属性this->createSpeaker();	//创建12名选手this->loadRecode();		//加载数据cout << "Clean Done!" << endl;}system("pause");system("cls");
}SpeechManager::~SpeechManager()
{
}