【C++课程设计——演讲比赛系统】

发布于:2024-07-03 ⋅ 阅读:(16) ⋅ 点赞:(0)


前言

在学习完C++的stl容器后,我们来写一下小项目对其进行应用!
项目名称为:演讲比赛系统

一、演讲比赛程序需求

  1. 比赛规则
    学校举行一场演讲比赛,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。
    比赛方式:分组比赛,每组6个人;选手每次要随机分组,进行比赛
    每名选手都有对应的编号,如 10001 ~ 10012
    第一轮分为两个小组,每组6个人。 整体按照选手编号进行抽签后顺序演讲。
    当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。
    第二轮为决赛,前三名胜出
    每轮比赛过后需要显示晋级选手的信息

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

二、每个功能模块的实现

1. 创建管理类(.h文件)

功能描述:
提供菜单界面与用户交互
对演讲比赛流程进行控制
与文件的读写交互

#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<map>
#include"Speaker.h"
#include<time.h>
#include<algorithm>
#include<numeric>
#include<fstream>
using namespace std;

class speechManager {
public:

	//构造函数
	speechManager();

	//菜单函数
	void Show_Menu();

	//初始化函数
	void initSperker();

	//创建12位初始选手
	void creatSpeaker();


	//开始进行比赛
	void startSpeech();

	//比赛抽签
	void speechDraw();

	//开始比赛
	void speechContest();

	//显示晋级结果
	void showScore();
	
	//保存每届记录
	void saveRecord();
	
	//退出系统
	void gameOver();
	
	//析构函数
	~speechManager();

	//读取记录
	void loadRecord();
	//展示记录
	void showRecord();

	//清空文件
	void clearRecord();
	//文件为空的标志
	bool fileIsEmpty;
	//往届记录
	map<int, vector<string>> _Record;

	vector<int> v1;//存放开始所有参赛选手

	vector<int> v2;//存放第一轮结束后晋级选手

	vector<int> vVictory;//存放最终前三名

	map<int, Speaker> _Speaker;

	int index;//记录该次比赛轮数
};

2.1. 创建管理类(.cpp文件)

#include"SpeechManager.h"

//构造函数
speechManager::speechManager()
{
	initSperker();
	creatSpeaker();
	this->loadRecord();
}

//菜单函数
void speechManager::Show_Menu()
{
	cout << "************************************************" << endl;
	cout << "*************欢迎参加演讲比赛*******************" << endl;
	cout << "*************1.开始演讲比赛*********************" << endl;
	cout << "*************2.查看往届比赛记录*****************" << endl;
	cout << "*************3.清空比赛记录*********************" << endl;
	cout << "*************0.退出比赛系统*********************" << endl;
	cout << "************************************************" << endl;
}


//退出系统
void speechManager::gameOver()
{
	cout << "欢迎下次再来!" << endl;
	system("pause");
	system("cls");
}



//初始化函数
void speechManager::initSperker()
{
	v1.clear();
	v2.clear();
	vVictory.clear();
	_Speaker.clear();
	index = 1;
	this->_Record.clear();
}


//创建12位初始选手
void speechManager::creatSpeaker()
{
	string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < nameSeed.size(); i++)
	{
		string name("选手");
		name += nameSeed[i];
		Speaker sp;
		sp._name = name;
		for (int i = 0; i < 2; i++)
		{
			sp._score[i] = 0;
		}
		v1.push_back(10001 + i);
		_Speaker.insert(make_pair(10001 + i, sp));
	}
}


//比赛抽签
void speechManager::speechDraw()
{
	cout << "第 " << index << " 轮选手正在抽签" << endl;
	cout << "-----------------------------" << endl;
	cout << "抽签后的演讲顺序如下" << endl;

	if (index == 1)
	{
		random_shuffle(v1.begin(), v1.end());

		for (auto it = v1.begin(); it != v1.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}
	else
	{
		random_shuffle(v2.begin(), v2.end());

		for (auto it = v2.begin(); it != v2.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}
	cout << "-----------------------------------" << endl;
	system("pause");
}

//开始比赛
void speechManager::speechContest()
{
	cout << "-------------第" << index << "轮比赛正式开始---------------" << endl;
	multimap<double, int, greater<double>> groupScore;//临时容器,可排出分数由高到低的选手编号
	//并且此容器遍历为出来为降序

	int num = 0;
	vector<int> v_src;
	if (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++)//10个评委打分
		{
			double score = (rand() % 401 + 600) / 10.0f;
			d.push_back(score);
		}

		谁有争议,可以查看分数
		//for (auto e : d)
		//{
		//	cout << e << " ";
		//}
		//cout << endl;

		sort(d.begin(), d.end());
		d.pop_back();
		d.pop_front();
		double sum = accumulate(d.begin(), d.end(), 0.0f);

		//double avg = sum / (double)d.size();
		double avg = sum /d.size();

		_Speaker[*it]._score[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->_Speaker[it -> second]._name 
					<< " 成绩: " << this->_Speaker[it->second]._score[this->index - 1] <<endl;
			}

			int count = 0;
			//取前三名
			for (auto it = groupScore.begin(); it!= groupScore.end() && count < 3; it++, count++)
			{
				if (this->index == 1)
				{
					v2.push_back((*it).second);
				}
				else
				{
					vVictory.push_back((*it).second);
				}
			}
			groupScore.clear();
			cout << endl;
		}
	}
	cout << "---------第" << index << "轮比赛完毕-----------" << endl;
	system("pause");
}

//显示晋级结果
void speechManager::showScore()
{
	cout << "第" << index << "轮晋级选手如下" << endl;
	vector<int> v;
	if (index == 1)
	{
		v = v2;
	}
	else
	{
		v = vVictory;
	}
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << "选手编号:" << *it << " 姓名: " << _Speaker[*it]._name << " 得分: " <<
			_Speaker[*it]._score[this->index - 1] << endl;
	}
	cout << endl;
	//system("cls");
	//this->Show_Menu();
}


//开始进行比赛
void speechManager::startSpeech()
{
	//第一轮抽签
	speechDraw();
	//第一轮比赛
	speechContest();
	//显示晋级结果
	showScore();
	index++;
	//第二轮抽签
	speechDraw();
	//第二轮比赛
	speechContest();
	//显示最终结果
	showScore();
	//保存结果
	saveRecord();
	//重置属性
	initSperker();
	creatSpeaker();
	this->loadRecord();
	system("cls");
}

//保存每届记录
void speechManager::saveRecord()
{
	ofstream ofs;
	ofs.open("speech.csv", ios::out | ios::app);//csv格式需要添加逗号分隔开

	for (auto it = vVictory.begin(); it != vVictory.end(); it++)
	{
		ofs << *it << "," << _Speaker[*it]._score[1] << ",";
	}
	ofs << endl;

	ofs.close();

	//cout << "记录已保存" << endl;
	this->fileIsEmpty = false;
	system("pause");
}


//读取记录
void speechManager::loadRecord()
{
	ifstream ifs("speech.csv", ios::in); //输入流对象 读取文件

	/*ifstream ifs;
	ifs.open("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;
		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); //从0开始查找 ','
			if (pos == -1)
			{
				break; //找不到break返回
			}
			string tmp = data.substr(start, pos - start); //找到了,进行分割 参数1 起始位置,参数2 截取长度
			v.push_back(tmp);
			start = pos + 1;
		}
		this->_Record.insert(make_pair(index, v));
		index++;
	}
	ifs.close();
}


void speechManager::showRecord()
{
	if (this->fileIsEmpty)
	{
		cout << "文件为空或不存在!" << endl;
	}
	else
	{
		for (int i = 0; i < this->_Record.size(); i++)
		{
			cout << "第" << i + 1 << "届 " <<
				"冠军编号:" << this->_Record[i][0] << " 得分:" << this->_Record[i][1] << " "
				"亚军编号:" << this->_Record[i][2] << " 得分:" << this->_Record[i][3] << " "
				"季军编号:" << this->_Record[i][4] << " 得分:" << this->_Record[i][5] << endl;
		}
	}
	
	system("pause");
	system("cls");
}


void speechManager::clearRecord()
{
	cout << "确认清空?" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;
	int select = 0;
	cin >> select;
	if (select == 1)
	{
		//打开模式 ios::trunc 如果存在删除文件并重新创建
		ofstream ofs("speech.csv", ios::trunc);
		ofs.close();
		//初始化属性
		this->initSperker();
		//创建选手
		this->creatSpeaker();
		//获取往届记录
		this->loadRecord();
		cout << "清空成功!" << endl;
	}
	system("pause");
	system("cls");
}

//析构函数
speechManager::~speechManager()
{

}

3.创建参赛选手类(.h)

#include<string>
#include<iostream>
using namespace std;
class Speaker {
public:
	string _name;

	double _score[2];

 };

4.将整体逻辑进行封装

#include"SpeechManager.h"

int main0()
{
	speechManager pm;
	srand((unsigned int)(time(NULL)));
	int choice = 1;
	do 
	{
		pm.Show_Menu();
		cout << "请输入你的选项:" << endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			pm.startSpeech();
			break;
		case 2:
			pm.showRecord();
			break;
		case 3:
			pm.clearRecord();
			break;
		case 0:
			pm.gameOver();
			break;
		default:
			cout << "输入有误,请按任意键后重新输入!" << endl;
			system("pause");
			system("cls");
			break;
		}

	} while (choice);

	return 0;
}

测试

开始进行比赛,进行抽签

在这里插入图片描述

第一轮比赛结束

在这里插入图片描述

第二轮比赛开始抽号

在这里插入图片描述

第二轮比赛结束

在这里插入图片描述

第二轮比赛得出结果

在这里插入图片描述

查看往届比赛成绩

在这里插入图片描述

在这里插入图片描述

清空往届比赛成绩

在这里插入图片描述

在这里插入图片描述

项目总结

本案例主要是对C++的stl容器和文件操作进行使用,基本涵盖了容器常用接口的使用,对大家对于stl的进一步熟悉能起较大左右作用!!!