[基于STL的演讲比赛管理系统]c++面向对象小项目

头文件:
SpeechManager.h

#pragma once
#include<iostream>
using namespace std;
#include<vector>
#include<map>
class Speaker
{
public:
	string m_Name;
	double m_Score[2];
};
class SpeechManager
{
public:
	SpeechManager();
	void show_Menu();//显示菜单
	void exitSystem();//退出菜单
	void initSpeech();//初始化
	vector<int>v1;//比赛选手 容器  12人
	vector<int>v2;//第二轮晋级选手 6人
	vector<int>vVictory;//获胜选手 3人
	map<int, Speaker>m_Speaker;//存放编号+选手的容器。
	int m_Index;//比赛轮数
	void createSpeaker();//创建演讲人
	void startSpeech();//比赛全程
	void speechDraw();//抽签
	void speechContest();//比赛
	void showScore();//显示分数
	void saveRecord();
	void loadRecord();
	bool filesEmpty;
	map<int, vector<string>>m_Record;
	void showRecord();
	void clearRecord();//清空!
	~SpeechManager();
};

SpeechManager.cpp

#include<iostream>
using namespace std;
#include"speechManager.h"
#include<algorithm>
#include<map>
#include<deque>
#include<functional>
#include<numeric>
#include<fstream>
#include <time.h>
SpeechManager::SpeechManager()
{
	this->initSpeech();
	this->createSpeaker();
	this->loadRecord();
}
SpeechManager::~SpeechManager()
{

}
void SpeechManager::show_Menu()
{
	cout << "********************************************" << endl;
	cout << "*************  欢迎参加演讲比赛 ************" << endl;
	cout << "*************  1.开始演讲比赛  *************" << endl;
	cout << "*************  2.查看往届记录  *************" << endl;
	cout << "*************  3.清空比赛记录  *************" << endl;
	cout << "*************  0.退出比赛程序  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;
}
void SpeechManager::exitSystem()
{
	cout << "欢迎下次使用!" << endl;
	system("pause");
	exit(0);
}
void SpeechManager::initSpeech()//初始化
{
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->m_Speaker.clear();
	this->m_Index = 1;
	this->m_Record.clear();
}
void SpeechManager::createSpeaker()
{
	string NameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < NameSeed.size(); 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);//12名选手编号
		//选手编号 以及对应的选手 存放到map容器中
		this->m_Speaker.insert(make_pair(i + 10001, sp));
	}
}
void SpeechManager::speechDraw()
{
	cout << "第" << this->m_Index << "轮比赛抽签顺序如下:" << endl;
	cout << "-------------------------------------------------" << endl;
	if (this->m_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");
	cout << endl;
}
void SpeechManager::speechContest()
{
	cout << "第" << this->m_Index << "轮比赛正式开始 :" << endl;
	multimap<double, int, greater<double>>groupScore;
	int num = 0;
	vector<int>Src;
	if (this->m_Index == 1)
	{
		Src = v1;
	}
	else {
		Src = v2;
	}
	for (vector<int>::iterator it = Src.begin(); it != Src.end(); it++)
	{
		num++;
		//评委打分
		deque<double>d;
		for (int i = 0; i < 10; i++) {
			double score = (rand() % 401 + 600)/10.f;//60.00---100.00
			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.2f);
		double avr = sum / (double)d.size();
		this->m_Speaker[*it].m_Score[m_Index - 1] = avr;
		//6个人一组,用临时容器保存
		groupScore.insert(make_pair(avr, *it));
		if (num % 6 == 0)
		{
			cout << "第" << 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 << " 成绩:" << this->m_Speaker[(*it).second].m_Score[this->m_Index-1] << endl;
			}
			int count = 0;
			for (multimap<double, int, greater<double>>::iterator 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();
			cout << endl;
		}
	}
	cout << "第" << this->m_Index << "轮比赛结束---------" << endl;
	system("pause");
}
void SpeechManager::showScore()
{
	cout << "---第" << this->m_Index << "轮比赛分数如下:---" << endl;
	vector<int>v;
	if (this->m_Index == 1)
	{
		v = v2;
	}
	else {
		v = vVictory;
	}
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "编号:" << *it << " 姓名 : " << this->m_Speaker[*it].m_Name << " 成绩 :" << this->m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
	}
	cout << endl;
	system("pause");
	system("cls");
	this->show_Menu();
}
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 << "," << this->m_Speaker[*it].m_Score[1] << ",";
	}
	ofs << endl;
	ofs.close();
	cout << "记录已经保存" << endl;
	this->filesEmpty = false;
}
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();
	this->initSpeech();
	this->createSpeaker();
	this->loadRecord();
}
void SpeechManager::loadRecord()
{
	ifstream ifs("speech.csv", ios::in); //输入流对象 读取文件

	if (!ifs.is_open())
	{
		this->filesEmpty = true;
		cout << "文件不存在!" << endl;
		ifs.close();
		return;
	}

	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		cout << "文件为空!" << endl;
		this->filesEmpty = true;
		ifs.close();
		return;
	}

	//文件不为空
	this->filesEmpty = false;

	ifs.putback(ch); //读取的单个字符放回去

	string data;
	int index = 0;
	while (ifs >> data)
	{
		//cout << data << endl;
		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->m_Record.insert(make_pair(index, v));
		index++;
	}

	ifs.close();
}
void SpeechManager::showRecord()
{
	if (this->filesEmpty)
	{
		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 SpeechManager::clearRecord()
{
	cout << "确定清空?" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;
	int choice;
	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");
}
int main()
{
	srand((unsigned int)time(NULL));
	SpeechManager sm;
	int choice = 0;
	
	while (true)
	{
		sm.show_Menu();
		cout << " 请输入您的选择: " << endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			sm.startSpeech();
			break;
		case 2:
			sm.showRecord();
			break;
		case 3:
			sm.clearRecord();
			break;
		case 0:
			sm.exitSystem();
			break;
		default:
			break;
		}
	}
	
	system("pause");
	return 0;
}

在这里插入图片描述
打个小广告,欢迎关注我的公众号“麦香E站”,分享人工智能,信通学习,数学提高,外语学习,生活理财等多方面知识以及我多年积累的学习资源分享~
公众号后台回复 “编程” 获取我多年来积攒的海量编程电子书~
后台回复“latex”获取我倾力所作latex快速入门手册~
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值