利用STL中的容器和算法实现演讲比赛流程管理系统

1. 项目需求分析

1.1 比赛规则

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

1.2 程序功能

  • 开始演讲比赛:完成整届比赛的流程,每个比赛阶段都需要给用户一个提示,用户按任意键后继续下一个阶段
  • 查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到这个文件中,文件用.csv后缀名保存
  • 清空比赛记录:将文件中数据清空
  • 退出比赛程序:可以退出当前程序

2. 代码实现

2.1 创建一个演讲比赛类

在SpeechManager.h头文件中,创建演讲比赛管理类

#pragma once
#include <iostream>
#include <vector>
#include <map>
using namespace std;

//设计演讲比赛管理类
class SpeechManager
{
public:
	//比赛选手 12人
	vector<int>v1;
	//第一轮晋级选手 6人
	vector<int>v2;
	//前三名选手 3人
	vector<int>vVictory;
	//存放编号 以及 对应的 具体选手容器
	map<int, Speaker> m_Speaker;
	//存放比赛轮数
	int m_Index;

public:
	SpeechManager();
	~SpeechManager();
	void show_Menu();
};

2.2 菜单功能和退出功能实现

在SpeechManager类中添加菜单功能,并在SpeechManager.cpp中实现

void SpeechManager::show_Menu()
{
	cout << "*********************************************" << endl;
	cout << "*************  欢迎参加演讲比赛 *************" << endl;
	cout << "*************  1、开始演讲比赛  *************" << endl;
	cout << "*************  2、查看往届记录  *************" << endl;
	cout << "*************  3、清空比赛记录  *************" << endl;
	cout << "*************  4、退出比赛程序  *************" << endl;
	cout << "*********************************************" << endl;
	cout << endl;
}

void SpeechManager::exit_System()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

2.3 提供功能接口

在main函数中提供分支选择,提供每个功能接口

#include <iostream>
using namespace std;
#include "SpeechManager.h"

int main()
{
	SpeechManager sm;
	int choice = 0;

	while (true)
	{
		sm.show_Menu();
		cout << "请输入您的选择: " << endl;
		cin >> choice;

		switch (choice)
		{
		case 1: //开始演讲比赛
			break;
		case 2: //查看往届记录
			break;
		case 3: //清空比赛记录
			break;
		case 4: //退出比赛程序
			sm.exit_System();
			break;
		default:
			system("cls"); //清屏
			break;
		}
	}

	system("pause");
	return 0;
}

2.4 创建选手类

在Speaker.h头文件中创建Speaker类

#pragma once
#include <iostream>
using namespace std;

class Speaker
{
public:
	string m_Name;
	double m_Score[2];
};

2.5 比赛功能实现

2.5.1 初始化SpeechManager类属性

void SpeechManager::initSpeech()
{
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->m_Speaker.clear();
	this->m_Index = 1;
}

2.5.2 创建选手

void SpeechManager::createSpeaker()
{
	string nameseed = "ABCDEFGHIJKL";
	for (int i = 0; i < 12; i++)
	{
		string name = "选手";
		name += nameseed[i];
		Speaker sp;
		sp.m_Name = name;
		for (int i = 0; i < 2; i++)
		{
			sp.m_Score[i] = 0;
		}
		this->v1.push_back(i + 10001);
		this->m_Speaker.insert(make_pair(i + 10001, sp));
	}
}

2.5.3 构造函数实现

SpeechManager::SpeechManager()
{
	this->initSpeech();
	this->createSpeaker();
}

2.5.4 开始比赛成员函数添加

  • 在speechManager.h中提供开始比赛的成员函数
  • 该函数功能是控制比赛流程
void startSpeech();
  • 在speechManager.cpp中将startSpeech的空实现先写入
void SpeechManager::startSpeech()
{
	//第一轮比赛
	  //1、抽签
	
	  //2、比赛
	
	  //3、显示晋级结果

	//第二轮比赛

	  //1、抽签

	  //2、比赛

	  //3、显示最终结果

	  //4、保存分数
}

2.5.5 抽签功能实现

  • 在speechManager.h中提供抽签的成员函数
void speechDraw();
  • 在speechManager.cpp中实现
void SpeechManager::speechDraw()
{
	cout << "第 << " << this->m_Index << " >> 轮比赛选手正在抽签" << endl;
	cout << "----------------------------" << endl;
	cout << "抽签后演讲顺序如下: " << endl;
	vector<int>v;
	if (this->m_Index == 1)
		v = v1;
	else
		v = v2;
	random_shuffle(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	cout << "----------------------------" << endl;
	system("pause");
}

2.5.6 开始比赛

  • 在speechManager.h中提供比赛的成员函数
void speechContest();
  • 在speechManager.cpp中实现
void SpeechManager::speechContest()
{
	cout << "----------第" << this->m_Index << "轮比赛正式开始----------" << endl;

	//准备临时容器 存放小组成绩
	multimap<double, int, greater<double>> groupScore;

	int num = 0; // 记录人员个数 6人一组
	
	vector<int>v_Src; //比赛选手容器
	if (this->m_Index == 1)
		v_Src = v1;
	else
		v_Src = v2;

	for (auto it = v_Src.begin(); it != v_Src.end(); it++)
	{
		num++;
		deque<double>d;
		for (int i = 0; i < 10; i++)
		{
			if (this->m_Index == 1)
				score = (rand() % 401 + 600) / 10.f;
			else
				score = (rand() % 201 + 800) / 10.f;
			d.push_back(score);
		}
		//cout << endl;

		sort(d.begin(), d.end(), greater<double>());
		d.pop_back();
		d.pop_front();

		double sum = accumulate(d.begin(), d.end(), 0.0f);
		double avg = sum / (double)d.size();  //平均分

		this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;

		groupScore.insert(make_pair(avg, *it));

		if (num % 6 == 0)
		{
			cout << "第" << num / 6 << "小组比赛名次如下: " << endl;
			for (auto it = groupScore.begin(); it != groupScore.end(); it++)
			{
				cout << "编号: " << it->second << " 姓名: " << this->m_Speaker[it->second].m_Name <<
					" 成绩: " << this->m_Speaker[it->second].m_Score[this->m_Index - 1] << endl;
			}
			cout << endl;
			//取走前三名
			int count = 0;
			for (auto it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++)
			{
				if (this->m_Index == 1)
					v2.push_back((*it).second);
				else
					vVictory.push_back((*it).second);
			}
			
			groupScore.clear();
		}
	}
	
}

2.5.7 显示晋级选手

  • 在speechManager.h中提供显示晋级选手的成员函数
void showScore();
  • 在speechManager.cpp中实现
void SpeechManager::showScore()
{
	cout << "-----------第" << this->m_Index << "轮比赛正式完毕----------" << endl;
	cout << endl;
	cout << "第" << this->m_Index << "轮晋级选手信息如下:" << endl;
	vector<int>v;
	if (this->m_Index == 1)
		v = v2;
	else
		v = vVictory;

	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << "选手编号:" << *it << " 姓名:" << m_Speaker[*it].m_Name << " 得分:"
			<< m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
	}
	cout << endl;
	system("pause");
	system("cls");
	this->show_Menu();
}

2.5.8 保存记录功能实现

  • 在speechManager.h中提供保存记录的成员函数
void saveRecord();
  • 在speechManager.cpp中实现
void SpeechManager::saveRecord()
{
	ofstream ofs;
	ofs.open("speech.csv", ios::out | ios::app); 

	//将每个人数据写入文件中
	for (auto it = vVictory.begin(); it != vVictory.end(); it++)
	{
		ofs << *it << "," << m_Speaker[*it].m_Score[1] << endl;
	}
	ofs << endl;

	ofs.close();
	cout << "记录已经保存" << endl;
}

2.5.9 开始比赛成员函数实现

void SpeechManager::startSpeech()
{
	//第一轮比赛
	  //1、抽签
	this->speechDraw();
	  //2、比赛
	this->speechContest();
	  //3、显示晋级结果
	this->showScore();
	//第二轮比赛
	this->m_Index++;
	  //1、抽签
	this->speechDraw();
	  //2、比赛
	this->speechContest();
	  //3、显示最终结果
	this->showScore();
	  //4、保存分数
	this->saveRecord();

	cout << "本届比赛完毕!" << endl;
	system("pause");
	system("cls");
}

2.6 获取往届分数

2.6.1 读取记录分数

  • 在speechManager.h中添加读取记录的成员函数void loadRecord();
  • 添加判断文件是否为空的标志 bool fileIsEmpty;
  • 添加往届记录的容器map<int, vector<string>> m_Record;

其中m_Record中的key代表第几届,value记录具体的信息

//往届记录
map<int, vector<string>> m_Record;
//文件为空的标志
bool fileIsEmpty;
//读取往届记录的成员函数
void loadRecord();
  • 在speechManager.cpp中实现成员函数void loadRecord();
void SpeechManager::loadRecord()
{
	ifstream ifs("speech.csv", ios::in);
	//文件不存在
	if (!ifs.is_open())
	{
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}
	//文件为空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//cout << "文件为空!" << endl;
		this->fileIsEmpty = true;
		ifs.close();
		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; //找不到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();
}

2.6.2 显示往届得分

  • 在speechManager.h中添加保存记录的成员函数void showRecord();
void showRecord();
  • 在speechManager.cpp中实现
void SpeechManager::showRecord()
{
	if (this->fileIsEmpty)
	{
		cout << "文件不存在,或记录为空!" << endl;
	}
	else
	{
		for (int i = 0; i < this->m_Record.size(); i++)
		{
			cout << "第" << i+1 << "届:" << endl;
			cout << "冠军编号:" << this->m_Record[i][0] << " 得分:" << this->m_Record[i][1] << endl;
			cout << "亚军编号:" << this->m_Record[i][2] << " 得分:" << this->m_Record[i][3] << endl;
			cout << "季军编号:" << this->m_Record[i][4] << " 得分:" << this->m_Record[i][5] << endl;
			cout << endl;
		}
	}
	
	system("pause");
	system("cls");
}

2.7 解决程序中的bug

目前程序中有几处bug未解决:

  1. 若文件为空或不存在,比完赛后依然提示记录为空
    解决方式:saveRecord中更新文件为空的标志

在这里插入图片描述
2. 比完赛后查不到本届比赛的记录,没有实时更新
解决方式:比赛完毕后,所有数据重置
在这里插入图片描述

  1. 在初始化时,没有初始化记录容器
    解决方式:initSpeech 中添加 初始化记录容器

在这里插入图片描述
4. 每次记录都是一样的
解决方式:在main函数开始, 添加随机数种子
在这里插入图片描述

2.8 清空文件功能实现

  • 在speechManager.h中添加成员函数void clearRecord();
void clearRecord();
  • 在speechManager.cpp中实现
void SpeechManager::clearRecord()
{
	cout << "确认清空?" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int choice = 0;
	cin >> choice;

	if (choice == 1)
	{
		ofstream ofs("speech.csv", ios::trunc); //如果存在文件 删除文件并重新创建
		ofs.close();

		this->initSpeech();
		this->createSpeaker();
		this->loadRecord();

		cout << "清空成功!" << endl;
	}
	system("pause");
	system("cls");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值