C++演讲比赛

Posted 别碰我的宏定义

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++演讲比赛相关的知识,希望对你有一定的参考价值。

提示:本文主要参考此篇博客黑马程序员,对其中的内容进行了分装和处理.创建一个新项目直接在添加一个main.cpp和SpeechManager.hpp.希望对你有用


下面直接展示代码:
SpeechManager.hpp

#pragma once
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<deque>
#include<functional>
#include<numeric>
#include<string>
#include<fstream>
using namespace  std;
class Speaker 
public:
	string m_Name;
	double m_Score[2];//两轮得分
;
class SpeechManager 
public:
	SpeechManager() //构造函数完成演讲初始化和创造选手工作
		//初始化容器
		InitSpeech();
		//创建演讲者
		CreateSpeaker();
	
	//显示菜单
	void ShowMenu() 
		cout << "\\t******************************\\t" << endl;
		cout << "\\t******欢迎参加演讲比赛********\\t" << endl;
		cout << "\\t*********1.开始比赛***********\\t" << endl;
		cout << "\\t*********2.查看记录***********\\t" << endl;
		cout << "\\t*********3.清空记录***********\\t" << endl;
		cout << "\\t*********0.退出程序***********\\t" << endl;
		cout << "\\t******************************\\t" << endl;
	
	void SpeechProcess() //比赛过程
		//1、抽签
		SpeechDraw();

		//2、比赛
		SpeechContest();

		//3、显示晋级结果
		ShowScore();

		//4、保存分数到文件中
		SaveRecord();
	
	//开始比赛
	void StartSpeech() 

		//第一轮比赛开始
		SpeechProcess();

		//2、第二轮开始
		m_Index++;

		SpeechProcess();

		cout << "本届比赛已经结束!" << endl;
		system("pause");
		system("cls");
	
	//展示往届记录
	void ShowRecord() 
		LocalRecord(); //加载往届信息
		for (size_t i = 0; i < m_Record.size(); ++i) 
			cout << "第" << i + 1 << "届" << endl;
			cout << "冠军编号" << m_Record[i][0] << "\\t得分" << m_Record[i][1] << endl;
			cout << "亚军编号" << m_Record[i][2] << "\\t得分" << m_Record[i][3] << endl;
			cout << "季军编号" << m_Record[i][4] << "\\t得分" << m_Record[i][5] << endl;
		
		system("pause");
		system("cls");
	
	//清空成绩
	void ClearRecord() 
		cout << "是否确认清空文件?" << endl;
		cout << "1、是	2、否" << endl;
		int select;
		cin >> select;
		if (1 == select) 
			//清空文件
			ofstream ofs("speech.csv", ios::trunc);
			ofs.close();
			//清空当前获胜者集合
			m_Record.clear();
			system("pause");
		
		else if (2 == select) 
			//取消清空文件操作
			cout << "清空文件已取消!!!!" << endl;
		
		else 
			cout << "操作失败!!!" << endl;
			exit(1);
		
		system("cls");
	
	//结束比赛
	void ExitSystem() 
		cout << "欢迎下次使用" << endl;
		exit(0);
	
	~SpeechManager() 

	
private:
	void SpeechDraw() //抽签,采用随机种子的方法在范围内抽出
		cout << "第" << m_Index << "轮选手正在抽签" << endl;
		cout << "-----------------------" << endl;
		if (1 == m_Index) //第一轮
			random_shuffle(v1.begin(), v1.end());
			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;
		
	
	void SpeechContest() //模拟比赛

		cout << "第" << m_Index  << "轮比赛正式开始" << endl;
		//准备临时容器存放小组成绩
		multimap<double, int, greater<double>> groupScore;
		int num = 0; //6人为一组
		vector<int> v_Src; //比赛选手容器
		if (1 == m_Index) 
			v_Src = v1;
		
		else 
			v_Src = v2;
		
		for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); ++it) 
			num++;
			deque<double> dq;
			for (int i = 0; i < 10; ++i) 
				//随机生成成绩
				double score = (rand() % 401 + 600)/10;
				dq.push_back(score);
			
			sort(dq.begin(), dq.end(),greater<double>()); //将成绩按高到低排列
			//去掉最高分和最低分
			dq.pop_back();
			dq.pop_front();
			//重写
			double sum = Accumulate(dq); //将成绩加和
			double avg = sum / (double)dq.size(); //平均分

			//平均分
			m_Speaker[*it].m_Score[m_Index - 1] = avg;

			//将分数和人员匹配插入到groupScore中
			groupScore.insert(make_pair(avg, *it));
			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 << "\\t姓名:"
						<< m_Speaker[it->second].m_Name
						<< "\\t成绩:" << m_Speaker[it->second].m_Score[m_Index - 1]
						<< endl;
				
				int count = 0;
				multimap<double, int, greater<double>>::iterator it = groupScore.begin();
				for (size_t i = 0; i != groupScore.size()/2; ++i) 
					if (1 == m_Index) 
						v2.push_back((*it).second);
						it++;
					
					else 
						vVictory.push_back((*it).second);
						it++;
					
				
				groupScore.clear();
			
		
		cout << "第" << m_Index << "轮比赛完毕" << endl;
	
	//求和
	double Accumulate(deque<double> dq) 
		double sum = 0.0;
		for (deque<double>::iterator it = dq.begin(); it != dq.end(); ++it) 
			/*int n =  *it*100;  //为了截断两位小数,故需要将后面的所有数据都丢弃
			sum += (n / 100);
			*/
			sum += *it;
		
		return sum;
	
	//展示每一轮的获胜者
	void ShowScore() 
		cout << "第" << m_Index << "轮晋级选手如下" << endl;
		vector<int> v;
		if (1 == m_Index) 
			v = v2; //第一轮晋级选手
		
		else 
			v = vVictory; //第二轮晋级选手
		
		//打印晋级选手名单
		for (vector<int>::iterator it = v.begin();
			it != v.end(); ++it)
			cout << "选手编号:" << *it << "\\t姓名:" << m_Speaker[*it].m_Name
				<< "\\t成绩"<< m_Speaker[*it].m_Score[m_Index -1] << endl;
		
		system("pause");
		system("cls");
		this->ShowMenu();
	
	//保存比赛成绩
	void SaveRecord() 
		ofstream ofs;
		//往文件输入并且追加
		ofs.open("speech.csv", ios::out | ios::app);
		//将每个选手的数据写入到文件中
		for (vector<int>::iterator it = vVictory.begin(); 
			it != vVictory.end(); ++it) 
			ofs << *it << "," << m_Speaker[*it].m_Score[1] << ",";
		
		ofs << endl; //输入结束流,表示录入结束
		ofs.close();
		cout << "记录已经完成" << endl;
	
	//初始化演讲
	void InitSpeech()  //容器置空
		v1.clear();
		v2.clear();
		vVictory.clear();
		m_Index = 1;
		m_Speaker.clear();
	
	//创建选手
	void CreateSpeaker() 
		//创建选手集合
		string NameSeed = "ABCDEFGHIJKL";
		for (unsigned i = 0; i < NameSeed.size(); ++i) 
			//创建具体选手
			string name = "选手";
			name += NameSeed[i];
			Speaker sp;
			sp.m_Name = name;
			for (int j = 0; j < 2; ++j) 
				sp.m_Score[j] = 0;
			
			//创建选手编号,并且放入到V1容器中
			v1.push_back(i + 10001);
			//选手编号以及对应选手放入map中
			m_Speaker.insert(make_pair(i + 10001, sp));
		
	
	//导入分数
	void LocalRecord() 
		ifstream ifs("speech.csv",ios::in);
 		if (!ifs.is_open()) //打开文件失败
			cout << "文件打开失败!!!" << endl;
			ifs.close();
			return;
		
		char ch;
		ifs >> ch;
		//文件清空
		if (ifs.eof()) //判断是否到达末尾
			cout << "文件为空" << endl;
			ifs.close();
			return;
		
		ifs.putback(ch); //往文件中尾插的函数
		string data;
		int index = 0;
		while (ifs >> data) //当依然有数据时,持续输入
			vector<string> v; //存放往届比赛信息
			int pos = -1;
			int start = 0;
			while (1) 
				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();
	
private:
	//第一轮比赛选手编号
	vector<int> v1;
	//第二轮比赛选手编号
	vector<int> v2;
	//第三轮比赛选手编号
	vector<int> vVictory;
	//存放编号以及对应具体选手容器
	map<int, Speaker> m_Speaker;
	//表示比赛轮数
	int m_Index;
	//存放两轮中的获胜者
	map<int, vector<string>> m_Record;
;

main.cpp

#include"SpeechManager.hpp"
#include<ctime>

int main(void) 
	//产生随机种子
	srand((unsigned int)time(NULL));
	SpeechManager sm;
	int choice; //提供给用户输入选择
	while (1) 
		sm.ShowMenu();
		cout << "请输入你的选择:";
		cin >> choice;
		switch (choice) 
		case 0:
			sm.ExitSystem();
			break;
		case 1: //开始比赛
			sm.StartSpeech();
			break;
		case 2: //查看往届信息
			sm.ShowRecord();
			break;
		case 3://清空信息
			sm.ClearRecord();
			break;
		default:
			cout << "请输入正确的序号进行选择(0——3)" << endl;
			system("cls");
			break;
		
	
	return 0;

总结

以上就是今天要讲的内容,有什么不懂的可以留言,也可以加博主QQ:3491666723。

以上是关于C++演讲比赛的主要内容,如果未能解决你的问题,请参考以下文章

Thinkbayes_Chapter6

Educational Codeforces Round 110 (Rated for Div. 2) (AB签到,C题双指针,D题DP好题)

UVALive 2218 Triathlon

奇怪的比赛

agc009bTournament

算法--胜者树-败者树