功能要求
思路分析
具体代码
演讲比赛流程管理系统.cpp
#include"SpeehManager.h"
#include<ctime>
#include<iostream>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));//?????????????
SpeehManager sm;//创建一个管理类的实例
int choice = 0;
//一个死循环的主界面
while (1)
{
//显示主目录内容
sm.Show_Menu();
cout << "请输入您的选择:" << endl;
//接收我们选项
cin >> choice;
switch (choice)
{
//开始比赛
case 1:
sm.StartSpeech();
break;
//查看往届的比赛记录
case 2:
sm.ShowRecord();
//清空比赛记录
case 3:
sm.ClearRecord();
//退出系统
case 0:
sm.ExitSystem();
break;
default:
system("cls");//输入有误,清屏
break;
}
}
system("pause");
return 0;
}
Speaker.h
#pragma once//保证文件程序只被编译一次
#include<iostream>
using namespace std;
#include<string>
class Speaker
{
public:
//选手姓名
string m_Name;
//比赛分数,一个人最多两轮分数
double m_Score[2];
};
SpeechManager.h
#pragma once
#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <map>
#include "speaker.h"
#include <algorithm>//算法的头文件
#include <deque>
#include <numeric>//数值计算
#include <functional>//官方仿函数的一些重构函数
#include <fstream>//文件操作
#include"Speaker.h"
#include<string>
using namespace std;
//设计演讲管理类
class SpeehManager
{
public:
SpeehManager();//构造函数
void Show_Menu();//菜单
void ExitSystem();//退出系统
void InitSpeech();//初始化
void CreatSpeaker();//创建选手
void SpeechDraw(); //抽签
void StartSpeech();//开始比赛
void SpeechContest();//比赛
void ShowScore();//显示分数
void SaveRecord();//保存文件
void LoadRecord();//读取文件
void ShowRecord();//显示往届的数据
bool FileIsEmpty;//文件是否为空
~SpeehManager();//析构函数
map<int, vector<string>>m_Record;//存放往届的记录的容器
void ClearRecord();//清空记录
vector<int>v1; //第一轮比赛选手12名编号(第一轮参赛人员)
vector<int>v2; //第一轮晋级选手6人(第二轮参赛人员)
vector<int>vVictory;//存放最终胜出前三名选手编号容器
map<int, Speaker>m_Speaker;//存放12名选手(编号以及对应具体选手实例,我们后面得到的数据都放在这里面,这样我们可以根据编号直接访问选手的所有数据)
int m_Index; //存放比赛轮数
};
SpeechManager.cpp
#include"SpeehManager.h"
SpeehManager::SpeehManager()//重写构造函数,里面有一些必要的容器
{
//初始化容器和属性
this->InitSpeech();
//创建12名选手
this->CreatSpeaker();
//加载往届的记录
this->LoadRecord();
}
//菜单目录
void SpeehManager::Show_Menu()
{
cout <<"**********************************" << endl;
cout << "**********欢迎参加演讲比赛*********" << endl;
cout << "*************1、开始比赛*************" << endl;
cout << "*************2、查看记录*************" << endl;
cout << "*************3、清空记录*************" << endl;
cout << "*************0、退出程序*************" << endl;
cout << "*************************************" << endl;
}
//退出系统
void SpeehManager::ExitSystem()
{
cout << "欢迎下次使用,再见" << endl;
system("pause");
exit(0);
}
//初始化
void SpeehManager::InitSpeech()
{
//容器置空
this->v1.clear();
this->v2.clear();
this->vVictory.clear();
this->m_Speaker.clear();
//初始化比赛轮数
this->m_Index = 1;
//将记录的容器清空
this->m_Record.clear();
}
//实例化对象赋给姓名和初始化两轮分数,最后在v1容器中插入一个数据作为编号,然后把编号,与实例对象一起插入到m_Speaker中
void SpeehManager::CreatSpeaker()
{
string NameSeed = "ABCDEFGHIJKL";
int len = NameSeed.size();
//创建12名实例选手对象,虽然他们都叫sp但是里面具体内容不同
for (int i = 0; i < len; i++)
{
//赋给姓名
string name = "选手";
name += NameSeed[i];
Speaker sp;
sp.m_Name = name;
for (int j = 0; j < 2; j++)
{
sp.m_Score[j] = 0;
}
//创建一个编号,放到容器v1中
this->v1.push_back(i + 10001);
//将选手编号与选手一起放到m_Speaker容器中去
this->m_Speaker.insert(make_pair(i + 10001, sp));
}
}
// 抽签
void SpeehManager::SpeechDraw()
{
cout << "第" << this->m_Index << "轮抽签正在进行" << endl;
cout << "————————————————————————" << endl;
//显示抽签的结果
if (this->m_Index == 1)//第一轮就是显示v1里面的12名选手编号
{
random_shuffle(v1.begin(),v1.end());//随机排序v1中的数据
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
else
{
//第二轮就是把v2l数据显示
random_shuffle(v2.begin(), v2.end());//随机排序v2中的数据
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
}
//开始比赛
void SpeehManager::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->CreatSpeaker();//实例化对象赋给姓名和两轮分数,最后在v1容器中插入一个数据作为编号,然后把编号,与实例对象一起插入到v2中
this->LoadRecord();//获取往届的记录
cout << "本届比赛已经结束了!" << endl;
system("pause");
system("cls");
}
//比赛
void SpeehManager::SpeechContest()
{
cout << "第" << this->m_Index << "轮比赛正式开始" << endl;
//创建临时groupScore键值降序存放容器来放小组成绩
multimap<double, int, greater<double>>groupScore;//临时容器 保存key分数,选手编号
int num = 0;//六个人为一组
vector<int>v_Src;//比赛选手临时专用容器v_Srrc
if (this->m_Index == 1)//第一轮比赛
{
v_Src = v1;
}
else
{
v_Src = v2;
}
// 遍历所有的选手编号进行比赛
for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++)
{
num++;//每六个一组,选择排序后的前面三个晋级传入v2容器中
//评委打分
deque<double>d;//这个容器可以实现排序功能
for (int i = 0; i < 10; i++)//10个评委对一个人的打分
{
double score = (rand() % 401 + 600) / 10.f;//随机分数
d.push_back(score);//将评委的打分插入到deque容器中
}
sort(d.begin(), d.end(), greater<double>());//降序排列greater代表降序
//去除最低分和最高分
d.pop_front();
d.pop_back();
double sum = accumulate(d.begin(), d.end(), 0.0f);//利用算法求出sum
double avg = sum / (double)d.size();//平均分
//将平均分放到map容器中
this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;//减一是因为下标是从零开始
//打印平均分
//cout << "编号" << ":" << *it << "姓名" << ":" << this->m_Speaker[*it].m_Name << "平均分 " << ":" << this->m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
//将打分数据按照从大到小放到小组groupscore中
groupScore.insert(make_pair(avg, *it));//key是得分 value是具体选手编号
//每6个人取出前三名,我们每六个人数据的数据处理了,就把他们清空(我们只需要获得
if (num % 6 == 0)//v_Src每遍历六个数据,就找前三个数据
{
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);//将选手编号插入v2容器
}
else
{
vVictory.push_back((*it).second);
}
}
groupScore.clear();//小组容器清空
}
}
cout << "第" << this->m_Index << "轮比赛完毕" << endl;
system("pause");
}
//显示分数
void SpeehManager::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;
}
system("pause");
system("cls");
this->Show_Menu();
}
//保存文件
void SpeehManager::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] << ",";//先是编号,然后通过编号在m_speaker容器中找到分数
}
ofs << endl;//换行
ofs.close();//关闭文件
cout << "记录已经完成" << endl;
//更改文件不为空状态
this->FileIsEmpty = false;
cout << this->FileIsEmpty << endl;
}
//读取文件
void SpeehManager::LoadRecord()
{
ifstream ifs("speech.csv", ios::in);
if (!ifs.is_open())//文件不存在
{
this->FileIsEmpty = true;
ifs.close();
return;
}
//文件内容为空
char ch;
ifs >> ch;//读走一个字符放到ch中
if (ifs.eof())//读到了文件尾部的话(也就是说是一个空文件)
{
cout << "文件空" << endl;
this->FileIsEmpty = true;
ifs.close();
return;
}
//文件内容不为空
this->FileIsEmpty = false;
ifs.putback(ch);//将我们之前读走的ch放回来
string data;
int index = 0;//第几届数据
while (ifs >> data)
{
vector<string>v;//存放冠军数据
int pos = -1;
int start = 0;
while(1)
{
pos = data.find(",", start);
if (pos == -1)
{
//没找到
break;
}
string temp = data.substr(start, pos - start);//截取的字符范围为(0,,找到逗号的位置减去初始位置)
v.push_back(temp);
start = pos + 1;//加一是因为有逗号占了位置
}
this->m_Record.insert(make_pair(index, v));
index++;
}
ifs.close();
}
//显示往届的数据
void SpeehManager::ShowRecord()
{
if (this->FileIsEmpty)
{
cout << "文件为或者文件不存在" << endl;
}
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 SpeehManager::ClearRecord()
{
cout << "是否确定清空文件?" << endl;
cout << "1.是、2.否" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
ofstream ofs("speech.csv", ios::trunc);//删除文件并且重新创建一个新的
ofs.close();
//初始化容器和属性
this->InitSpeech();
//创建选手
this->CreatSpeaker();
//加载往届记录
this->LoadRecord();
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
SpeehManager::~SpeehManager()
{
}