C++ 演讲比赛流程管理系统设

1.功能分析

要求有12个人参加比赛,将12个人分为两组,进行第一轮比赛,然后每个参赛人员有10个评委打分,去掉最高和最低分后,求出每个人的平均分,然后去出每组中的分数最高的三个人. 接着6人又进行第二轮比赛,前三名取得胜利.

2.具体实现部分

首先建立一个speechManage.h头文件,在里面建立一个的类,里面存放演讲比赛流程实现的各个函数的申明.

#pragma once
#include <iostream>
using namespace std;
#include <vector>
#include "speechWorker.h"
#include <map>
#include <algorithm>
#include <functional>
#include <deque>
#include <numeric> 
#include <fstream>
class SpeechManage
{
public:
	//构造函数
	SpeechManage();

	//显示菜单
	void showmenu();

	//退出比赛
	void ExitMatch();

	//初始化属性
	void initSpeech();
	
	//创建12名选手
	void createMatcher();

	//开始比赛
	void startMatch();

	//抽签
	void speechDraw();

	//比赛评委打分
	void SpeechContest();

	//显示晋级人的分数
	void showScore();

	//将获胜者信息存入文件中
	void saveFile();

	//加载记录
	void loadRecord();

	//显示往届记录
	void showRecord();

	//清空文件
	void clearRecord();


	//文件存在标志
	bool FileFlage;

	//存放往届记录信息
	map<int, vector<string>>m_Record;

	//析构函数
	~SpeechManage();

	int index = 1;//记录比赛轮数

	vector<int>v1;//存放第一轮比赛选手编号

	vector<int>v2;//存放第二轮选手编号

	vector<int>vWinner;//存放胜利者选手编号

	map<int, speechWorker>m_Speaker;

};

创建一个参赛者的类,来存放参赛者的信息

class speechWorker
{
public:
	string m_Name; //参赛者姓名
	double m_Score[2];//存放两轮比赛的分数
};

首先是构造函数的实现,构造函数主要是做一些元素的初始化的,里面函数具体的实现在后面会将.

//构造函数
SpeechManage::SpeechManage()
{
	this->initSpeech();
	this->createMatcher();
	this->loadRecord();
}

接下里是菜单的设置,主要有以下四个功能.

//显示菜单
void SpeechManage::showmenu()
{
	cout << "**************************************" << endl;
	cout << "**************************************" << endl;
	cout << "*********** 欢迎参加演讲比赛 *********" << endl;
	cout << "*********** 1.开始演讲比赛 ***********" << endl;
	cout << "*********** 2.查看往届记录 ***********" << endl;
	cout << "*********** 3.清空比赛记录 ***********" << endl;
	cout << "*********** 0.退出比赛程序 ***********" << endl;
	cout << "**************************************" << endl;
	cout << "**************************************" << endl;
}

下面是一个初始化成员属性的函数的实现过程

//初始化属性
void SpeechManage::initSpeech()
{
	this->v1.clear();
	this->v2.clear();
	this->vWinner.clear();
	this->m_Speaker.clear();
	this->index = 1;
	this->m_Record.clear();

}

接下里是来创建12名选手的标号(10001–10012),并把它们放到一个vector容器中,并创建一个map容器来让每位选手对应一个标号(10001–10012)

//创建12名选手
void SpeechManage::createMatcher()
{
	string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < nameSeed.size(); i++)
	{
		string name = "选手";
		name += nameSeed[i];
		
		speechWorker sp;
		sp.m_Name = name;
		for (int j = 0; j < 2; j++)
		{
			sp.m_Score[j] = 0;
		}
		this->v1.push_back(i + 10001);

		this->m_Speaker.insert(make_pair(i + 10001, sp));
	}
	

}

接下来是比赛流程的设计过程,主要分为两轮抽签,和评委打分,最后将获胜者的信息存入文档中.

//开始比赛
void SpeechManage::startMatch()
{
	//1.开始第一轮抽签
	this->speechDraw();

	//2.开始第一轮比赛 评委打分
	this->SpeechContest();
	//3.显示第一轮得分
	this->showScore();

	//第二轮比赛开始
	this->index++;

	//4.开始第二轮抽签
	this->speechDraw();

	//5.开始第二轮比赛 评委打分
	this->SpeechContest();

	//6.显示第二轮得分
	this->showScore();

	//将获胜者信息存入文件中
	this->saveFile();

	this->initSpeech();
	this->createMatcher();
	this->loadRecord();
	
	cout << "*****本届比赛结束**********" << endl;
	system("pause");
	system("cls");
}

//抽签
void SpeechManage::speechDraw()
{
	cout << "-----第" << this->index << "轮比赛抽签开始-------" << endl;
	cout << "抽签后演讲顺序为:" << endl;
	if (this->index == 1) //表示第一轮比赛
	{
		random_shuffle(v1.begin(), v1.end()); //将12名选手的标号顺序打乱
		//将打乱后的演讲顺序打印出来
		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;
	}
	system("pause");
	cout << endl;

}


//比赛评委打分
void SpeechManage::SpeechContest()
{
	cout << "------第" << this->index << "轮比赛正式开始:---------" << endl;
	cout << "-----------------------------" << endl;
	multimap<double, int, greater<double>>groupScore; //记录平均分
	int num = 0;
	vector<int>vSrc;//存放两轮比赛中的人员信息
	if (this->index == 1)
	{
		vSrc = v1;
	}
	else
	{
		vSrc = v2;
	}
	
	for (vector<int>::iterator it = vSrc.begin(); it != vSrc.end(); it++)
	{
		num++; //记录是第几个人
		deque<int>d; //存放评委分数
		for (int i = 0; i < 10; i++)  //10个评委随机打分
		{
			double score = (rand() % 401 + 600) / 10.f;
			d.push_back(score);
		}
		sort(d.begin(), d.end(), greater<double>());  //将分数降序排序
		d.pop_front(); //除去最高分
		d.pop_back();  //除去最低分
		double sum = accumulate(d.begin(), d.end(), 0.0f); //就算剩余分数的总和
		double avg = sum / (double)d.size(); //取平均分

		this->m_Speaker[*it].m_Score[this->index - 1] = avg;//将平均分放入对应轮数的分数中

		groupScore.insert(make_pair(avg, *it)); //将平均分和每个人标号对应

		if (num % 6 == 0) //判断是第几组人 num=6表示第一轮中的第一组 num=12表示第一轮中的第二组
		{
			cout << "第" << this->index << "轮参赛选手"<<"第"<<num/6<<"组的" << "分数为:" << endl;
			for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++)
			{
				cout << "编号:" << it->second <<" 姓名:"<<this->m_Speaker[it->second].m_Name << " 分数为:" << it->first << endl;
			}
			
			cout << "------------------------------" << endl;
			cout << endl;
			//取出每组前三名
			int count = 0;
			for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++)
			{
				if (this->index == 1)
				{
					v2.push_back((*it).second); //取出第一轮比赛中的两组的前三名放入v2容器中
				}
				else
				{
					vWinner.push_back((*it).second);//取出第二轮比赛中的的前三名放入vWinner容器中
				}
			}

			groupScore.clear();

		}
		
	}
	cout << "----------第"<<this->index<<"轮比赛结束-------------" << endl;
	system("pause");
	cout << endl;
}



//显示晋级人的分数
void SpeechManage::showScore()
{

	if (this->index == 1)
	{
		cout << "-------第一轮比赛后晋级名单---------" << endl;
		for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
		{
			cout << "编号为:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 分数为:" << this->m_Speaker[*it].m_Score[this->index-1] << endl;
		}
	}
	else
	{
		cout << "---------获胜者名单---------" << endl;
		for (vector<int>::iterator it = vWinner.begin(); it != vWinner.end(); it++)
		{
			cout << "编号为:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 分数为:" << this->m_Speaker[*it].m_Score[this->index-1] << endl;
		}
	}

	system("pause");
	system("cls");
	this->showmenu();

}


//将获胜者信息保存到文件中
void SpeechManage::saveFile()
{
	ofstream ofs;
	ofs.open("speech.csv", ios::out | ios::app);//以追加的方式输入
	for (vector<int>::iterator it = vWinner.begin(); it != vWinner.end(); it++)
	{
	//将编号和分数写入文档中
		ofs << *it << "," << this->m_Speaker[*it].m_Score[1]<< ",";
	}
	ofs << endl;

	ofs.close();
	cout << "文件记录保存" << endl;
	this->FileFlage = false; //标记文档不为空

}



//加载记录
void SpeechManage::loadRecord()
{
	ifstream ifs;
	ifs.open("speech.csv", ios::in);
	if (!ifs.is_open())//判断文件时否打开成功
	{
		this->FileFlage = true;  //标志文件为空
		//cout << "文件不存在" << endl;
		ifs.close();
		return;
	}

	char ch;
	ifs >> ch;//读取一个字符
	if (ifs.eof())//判断是否读到文件末尾
	{
		//cout << "文件为空" << endl;
		this->FileFlage = true;  //标志文件为空
		ifs.close();
		return;
	}
	
	this->FileFlage = false;
	ifs.putback(ch);  //将上面读取的单个字符放回去
	vector<string>v;
	string date; 
	int num = 0; //记录第几届
	while (ifs >> date)//每次读取一行数据
	{
		int pos = -1;
		int start = 0;
		while (true)
		{
			pos = date.find(",", start);//查找逗号
			if (pos == -1)
			{
				break;
			}
			string temp = date.substr(start, pos - start);//将两个逗号之间的数据取出来
			v.push_back(temp); //存放最后获胜人的编号和分数
			start = pos + 1;
		}
		this->m_Record.insert(make_pair(num, v));//将每届人的信息存放到一个map容器中
		v.clear();
		num++;
	}
	
	ifs.close();

	/*for (map<int, vector<string>>::iterator it = m_Record.begin(); it != m_Record.end(); it++)
	{
		cout << it->first << "冠军编号:" << it->second[0] << " 冠军分数" << it->second[1] << endl;
	}*/
}

//显示往届记录
void SpeechManage::showRecord()
{
	
	if (this->FileFlage)//判断文件是否为空
	{
		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;
		}
	}
	system("pause");
	system("cls");

}


//清空文件
void SpeechManage::clearRecord()
{
	cout << "是否确定清空文件" << endl;
	cout << "1.确定" << endl;
	cout << "2.取消" << endl;
	int select = 0;
	cin >> select;
	if (select == 1)
	{
		ofstream ofs;
		ofs.open("speech.csv", ios::trunc); //以重建的方式打开文件并清空文件
		ofs.close();
		//清空文件后,要对文件中的内容进行初始化
		this->initSpeech();
		this->createMatcher();
		this->loadRecord();
		cout << "清空成功" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		system("cls");
	}
}

最后是主函数部分

#include "speechManage.h"
#include "speechWorker.h"
#include <ctime>
int main()
{
	srand((unsigned int)time(NULL));
	int choice = 0;
	SpeechManage sm;
	while (true)
	{
		sm.showmenu();

		cout << "请输入选择:" << endl;
		cin >> choice;
		switch (choice)
		{
		case 1: //开始比赛
			sm.startMatch();
			break;
		case 2:  //查看记录
			sm.showRecord();
			break;
		case 3:  //清空比赛记录
			sm.clearRecord();
			break;
		case 0: //退出比赛
			sm.ExitMatch();
			break;
		default:
			system("cls");
			break;
		}
	}

	return 0;
}

3.程序源码

链接:https://pan.baidu.com/s/19Nkt8cK76sHKPVpyeecJLw
提取码:2323

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不倒翁*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值