草庐IT

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

GodOuO 2023-04-10 原文
  • 任务

学校演讲比赛,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);		//选手编号 存入v1
		this->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 1
		random_shuffle(v1.begin(),v1.end());
		for_each(v1.begin(), v1.end(), printVectorInt());
	}
	else if (2 == this->m_Index) {		//Round 2
		random_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 Top
		d.pop_back();		//del Last

		double 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 =ID
		if (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;			//查到’,‘ loc
		int 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()
{
}

有关C++演讲比赛流程管理系统_黑马的更多相关文章

  1. ruby - i18n Assets 管理/翻译 UI - 2

    我正在使用i18n从头开始​​构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在ruby​​onrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi

  2. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  3. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

  4. 电脑0x0000001A蓝屏错误怎么U盘重装系统教学 - 2

      电脑0x0000001A蓝屏错误怎么U盘重装系统教学分享。有用户电脑开机之后遇到了系统蓝屏的情况。系统蓝屏问题很多时候都是系统bug,只有通过重装系统来进行解决。那么蓝屏问题如何通过U盘重装新系统来解决呢?来看看以下的详细操作方法教学吧。  准备工作:  1、U盘一个(尽量使用8G以上的U盘)。  2、一台正常联网可使用的电脑。  3、ghost或ISO系统镜像文件(Win10系统下载_Win10专业版_windows10正式版下载-系统之家)。  4、在本页面下载U盘启动盘制作工具:系统之家U盘启动工具。  U盘启动盘制作步骤:  注意:制作期间,U盘会被格式化,因此U盘中的重要文件请注

  5. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  6. kvm虚拟机安装centos7基于ubuntu20.04系统 - 2

    需求:要创建虚拟机,就需要给他提供一个虚拟的磁盘,我们就在/opt目录下创建一个10G大小的raw格式的虚拟磁盘CentOS-7-x86_64.raw命令格式:qemu-imgcreate-f磁盘格式磁盘名称磁盘大小qemu-imgcreate-f磁盘格式-o?1.创建磁盘qemu-imgcreate-fraw/opt/CentOS-7-x86_64.raw10G执行效果#ls/opt/CentOS-7-x86_64.raw2.安装虚拟机使用virt-install命令,基于我们提供的系统镜像和虚拟磁盘来创建一个虚拟机,另外在创建虚拟机之前,提前打开vnc客户端,在创建虚拟机的时候,通过vnc

  7. ruby - (Ruby || Python) 窗口管理器 - 2

    我想用这两种语言中的任何一种(最好是ruby​​)制作一个窗口管理器。老实说,除了我需要加载某种X模块外,我不知道从哪里开始。因此,如果有人有线索,如果您能指出正确的方向,那就太好了。谢谢 最佳答案 XCB,X的下一代API使用XML格式定义X协议(protocol),并使用脚本生成特定语言绑定(bind)。它在概念上与SWIG类似,只是它描述的不是CAPI,而是X协议(protocol)。目前,C和Python存在绑定(bind)。理论上,Ruby端口只是编写一个从XML协议(protocol)定义语言到Ruby的翻译器的问题。生

  8. ruby - 在没有基准或时间的情况下用 Ruby 测量用户时间或系统时间 - 2

    因为我现在正在做一些时间测量,我想知道是否可以在不使用Benchmark类或命令行实用程序time的情况下测量用户时间或系统时间。使用Time类只显示挂钟时间,而不显示系统和用户时间,但是我正在寻找具有相同灵active的解决方案,例如time=TimeUtility.now#somecodeuser,system,real=TimeUtility.now-time原因是我有点不喜欢Benchmark,因为它不能只返回数字(编辑:我错了-它可以。请参阅下面的答案。)。当然,我可以解析输出,但感觉不对。*NIX系统的time实用程序也应该可以解决我的问题,但我想知道是否已经在Ruby中实

  9. ruby-on-rails - 事件管理员和自定义方法 - 2

    这是我在ActiveAdmin中的自定义页面ActiveAdmin.register_page"Settings"doaction_itemdolink_to('Importprojects','settings/importprojects')endcontentdopara"Text"endcontrollerdodefimportprojectssystem"rakedataspider:import_projects_ninja"para"OK"endendend我想做的是,当我单击“导入项目”按钮时,我想在Controller中执行rake任务。但是我无法访问该方法。可能是什

  10. ruby - 以毫秒为单位获取当前系统时间 - 2

    在Ruby中,以毫秒为单位获取自纪元(1970)以来的当前系统时间的正确方法是什么?我试过了Time.now.to_i,好像不是我想要的结果。我需要结果显示毫秒并且使用long类型,而不是float或double。 最佳答案 (Time.now.to_f*1000).to_iTime.now.to_f显示包含十进制数字的时间。要获得毫秒数,只需将时间乘以1000。 关于ruby-以毫秒为单位获取当前系统时间,我们在StackOverflow上找到一个类似的问题:

随机推荐