【C++】基于STL演讲比赛流程管理系统

目录

  • 一、比赛规则
  • 二、比赛功能
  • 三、全部代码
      • 1. test.cpp
      • 2. Speecher.h
      • 3. SpeecherManager.h
      • 4. SpeecherManager.cpp


本管理系统参考黑马C++课程B站课程指路,仅作参考与学习资料。
核心课程笔记 C++核心
提高课程笔记 C++提高

一、比赛规则

  1. 学校举行一场演讲比塞,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。每名选手都有对应的编号。如10001 ~10012。
  2. 比赛方式:分组比赛,每组6个人。
  3. 十个评委分别给每名选手打分,去除最高分和最低分,求的平均分为本轮选手的成绩当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。第二轮为决赛,前三名胜出。
  4. 每轮比赛过后需要显示晋级选手的信息。

二、比赛功能

  1. 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段
  2. 查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存
  3. 清空比赛记录:将文件中数据清空
  4. 退出比赛程序:可以退出当前程序
    【C++】基于STL演讲比赛流程管理系统_第1张图片

[ 注意 ]csv格式可以用excel或记事本方式打开。

三、全部代码

1. test.cpp

#include "Speecher.h"
#include "SpeechManager.h"
#include
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
#include
using namespace std;
class Speecher
{
public:
	string name;
	double score[2];//两次分数:第一次为晋级分数,第二次为决赛分数
};

3. SpeecherManager.h

#pragma once
#include
#include
#include
#include
#include 
#include 
#include 
#include 
#include 
#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;//往届记录,包括:届数和每届冠军季军亚军
};

注意:【C++】基于STL演讲比赛流程管理系统_第2张图片

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;
	else
		v_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++,c++,开发语言,后端)