演讲比赛管理系统

1.1 比赛规则

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

1.2 程序功能

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

程序效果图:
在这里插入图片描述

直接上代码
//
speaker.h

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

class Speaker {
public:
	string name;
	double score[2];
};

//
speechManager.h

#pragma once
#include<iostream>
#include<vector>
#include<string>
#include"speaker.h"
#include<map>
#include<algorithm>
#include<deque>
#include<numeric>
#include<fstream>
using namespace std;

class speechManager {
public:

	//构造函数
	speechManager();

	//展示菜单
	void showMenu();

	//比赛选手 12人
	vector<int> v1;
	
	//第一轮晋级 6人
	vector<int> v2;
	
	//胜利者前三名
	vector<int> vVictory;
	
	//存放编号以及对应的选手
	map<int, Speaker> speaker;

	//比赛轮数
	int index;


	//初始化属性
	void initSpeech();

	//创建选手
	void createSpeaker();

	//抽签
	void speechDraw();

	//比赛
	void speechContest();

	//显示分数 
	void showScore();

	//开始比赛
	void startSpeech();

	//保存成绩到文件
	void saveRecord();

	//加载记录
	void loadRecord();

	//查看记录功能
	void showRecord();

	//文件为空标志
	bool fileIsEmpty;
	
	//往届记录
	map<int, vector<string>>record;

	//清空记录
	void clearRecord();

	//退出功能
	void exitSystem();

	//析构函数
	~speechManager();
};

//
speechManager.cpp

#include"speechManger.h"

speechManager::speechManager() {
	this->initSpeech();//初始化属性
	
	this->createSpeaker();//创建选手
	
	this->loadRecord();//加载往届记录
}
//显示菜单
void speechManager::showMenu() {
	cout << "********************************************" << endl;
	cout << "*************  欢迎参加演讲比赛 ************" << endl;
	cout << "*************  1.开始演讲比赛  *************" << endl;
	cout << "*************  2.查看往届记录  *************" << endl;
	cout << "*************  3.清空比赛记录  *************" << endl;
	cout << "*************  0.退出比赛程序  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;
}
//初始化属性
void speechManager::initSpeech() {
	//将容器全部清空
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->speaker.clear();
	//初始化比赛轮数
	this->index = 1;
	//往届记录清空
	this->record.clear();
}
//创建选手
void speechManager::createSpeaker() {
	string nameSeed="ABCDEFGHIJKL";//选手编号便于赋值
	for (int i = 0; i  < nameSeed.size(); i++)
	{
		string name = "选手";
		name += nameSeed[i];
		Speaker sp;
		sp.name = name;
		sp.score[0] = 0;
		sp.score[1] = 0;
		this->v1.push_back(i + 10001);
		this->speaker.insert(pair<int,Speaker>(i+10001,sp));
	}
}
//抽签
void speechManager::speechDraw() {
	cout << "第" << this->index << "轮选手正在抽签" << endl;
	cout << "---------------------------" << endl;
	cout << "抽签后顺序如下:" << endl;
	//第一届选手抽签
	if (this->index == 1) {
		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;

	}
	cout << "---------------------------" << endl;
	system("pause");
	system("cls");
	cout << endl;
}
void speechManager::speechContest() {
	cout << "第" << this->index << "轮比赛正式开始" << endl;
	int num=0;//人员个数
	multimap<double, int, greater<int>>groupScore;
	vector<int>v_src;//临时容器存储选手代号

	if (this->index == 1) {
		v_src = v1;
	}
	else {
		v_src = v2;
	}
	for (vector<int>::iterator it = v_src.begin(); it != v_src.end(); it++)
	{
		num++;
		
		//评委打分
		deque<double>d;//deque存储打分便于对最大值最小值去除


		for (int i = 0; i < 10; i++)
		{
			double score = (rand() % 401 + 600) / 10.f;
			/*cout << score << " " ;*/
			d.push_back(score);
		}

		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();
		//测试输出平均分
		/*cout << "编号" << *it << " 选手:" << this->speaker[*it].name << "  分数" << avg << endl;*/
		
		//将平均值赋给选手
		this->speaker[*it].score[this->index - 1] = avg;

		groupScore.insert(make_pair(avg, *it));
		//6人一组 用容器保存
		if (num % 6 == 0) {
			cout << "第" << num / 6 << "小组比赛名次" << endl;
			for (multimap<double, int, greater<int>>::iterator it = groupScore.begin(); it != groupScore.end(); it++) {
				cout << "编号:" << it->second << " 姓名:" << this->speaker[it->second].name << " 分数" << this->speaker[it->second].score[this->index-1] << endl;
			}
			cout << endl;
			int count = 0;
			for (multimap<double, int, greater<int>>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; count++, it++)
			{
				if (this->index == 1) {
					v2.push_back(it->second);
				}
				else {
					vVictory.push_back(it->second);

				}
			}
			groupScore.clear();
		}
		


	}
	cout << "第" << this->index << "轮比赛结束" << endl;
	system("pause");
	
}
//显示分数 
void speechManager::showScore() {
	cout << "-------第" << this->index << "轮晋级选手信息如下:-------" << endl;
	vector<int>v;//临时容器存放晋级选手成绩
	if (this->index == 1) {//判断第几轮比赛
		v = this->v2;
	}
	else {
		v = this->vVictory;
	}
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "编号:" << *it << " 姓名:" << this->speaker[*it].name << " 得分" << this->speaker[*it].score[this->index-1] << endl;
	}
	cout << endl;
	system("pause");
	system("cls");
	this->showMenu();
}
//保存成绩到文件
void speechManager::saveRecord() {
	ofstream ofs;
	ofs.open("Speech.csv", ios::out | ios::app);
	for (vector<int>::iterator it = vVictory.begin(); it !=vVictory.end(); it++)
	{
		ofs << *it << "," << speaker[*it].score[1] << ",";
	}
	ofs << endl;
	ofs.close();
	cout << "记录已保存" << endl;

	this->fileIsEmpty = false;
}
//加载往届记录
void speechManager::loadRecord() {
	ifstream ifs("Speech.csv", ios::in);

	if (!ifs.is_open()) {
		/*cout << "文件不存在" << endl;*/
		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 date;
	int index = 0;
	while (ifs>>date){
		vector<string> v;
		int pos = -1;
		int start = 0;
		while (true) {
			pos = date.find(",", start);
			if (pos == -1) {
				break;
			}
			string tmp = date.substr(start, pos - start);
			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;
	}
	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::startSpeech() {
	//第一轮比赛
	//抽签
	speechDraw();
	//比赛
	speechContest();
	//显示晋级结果
	showScore();
	//索引加一进行第二轮比赛
	this->index++;
	//第二轮比赛
	//抽签
	speechDraw();
	//比赛
	speechContest();
	//显示最终结果
	showScore();
	//保存成绩
	saveRecord();


	this->initSpeech();

	this->createSpeaker();

	this->loadRecord();
	cout << "比赛结束" << endl;



	system("pause");
	system("cls");
}

//清空记录
void speechManager::clearRecord() {
	cout << "确认清空?" << endl;
	cout << "1.确认  2.返回" << endl;
	int select;
	cin >> select;
	if (select == 1) {
		ofstream ofs("Speech.csv",ios::trunc);
		ofs.close();
		//初始化属性

		this->initSpeech();

		//创建选手
		this->createSpeaker();

		//获取往届记录
		this->loadRecord();
		cout << "清空完成!" << endl;
	}
	system("pause");
	system("cls");
}

//退出功能
void speechManager::exitSystem() {
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}


speechManager::~speechManager() {

}

//
演讲比赛系统.cpp

#include<iostream>
#include"speechManger.h"
#include<string>
using namespace std;
#include<ctime>
int main() {
	speechManager sm;

	//for (map<int,Speaker>::iterator it = sm.speaker.begin(); it != sm.speaker.end(); it++)
	//{
	//	cout << "编号 " << it->first << "姓名" << it->second.name << "分数" << it->second.score[0]<<endl;
	//}
	srand((unsigned int)time(NULL));//随机数种子
	while (true) {
		sm.showMenu();
		int select;
		cout << "请输入选择:";
		cin >> select;
		switch (select) {
		case 1://开始比赛
			sm.startSpeech();
			break;
		case 2://查看记录
			sm.showRecord();
			break;
		case 3://清空记录
			sm.clearRecord();
			break;
		case 0://退出系统
			sm.exitSystem();
			break;
		default:
			system("cls");
			break;
		}
	}

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值