speechManager.h
#pragma once
#include <iostream>
#include <vector>
#include <map>
#include <functional>//greater头文件
#include <deque>
#include <numeric>//accumulate头文件
#include "speaker.h"
#include <algorithm>
#include <fstream>//ofs头文件
#include <time.h>
using namespace std;
//设计一个演讲比赛管理的类
class SpeechManager
{
public:
SpeechManager();
void show_Menu();//菜单功能
void exitSystem();//退出功能
~SpeechManager();
//初始化容器和属性
void initSpeech();
//成员属性
//保存参加比赛的12名选手的编号
vector<int>v1;
//保存初赛晋级的6名选手编号
vector<int>v2;
//保存获奖的3名选手的容器
vector<int>vVictory;
//存放编号以及对应具体选手容器
map<int, speaker>m_Speaker;
//存放举行比赛的次数
int m_Index;
//抽签
void speechDraw();
//初始化12名选手
void creatSpeaker();
//开始比赛,比赛整个流程控制函数
void startSpeech();
//比赛
void speechContest();
//显示结果
void showScore();
//保存记录
void saveRecord();
//读取记录
void loadRecord();
//文件为空的标志
bool fileIsEmpty;
//存放往届记录的容器
map<int, vector<string>>m_Record;
//显示往届信息
void showRecord();
//清空往届信息
void clearRecord();
};
speaker.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class speaker
{
public:
string m_Name;//选手姓名
double m_Score[2];//分数 最多有两轮得分
};
基于STL的演讲比赛流程管理系统.cpp
#include<iostream>
using namespace std;
#include "speechManager.h"
int main()
{
SpeechManager sm;
int choice = 0;//用来存储用户数据的选项
srand((unsigned)time(NULL));
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 4: //退出系统
sm.exitSystem();
break;
default:
break;
}
}
system("pause");
return 0;
}
speechManager.cpp
#include "SpeechManager.h"
SpeechManager::SpeechManager()
{
//初始化容器和属性
this->initSpeech();
//创建12名选手
this->creatSpeaker();
//获取往届记录
this->loadRecord();
}
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::creatSpeaker()//初始化选手的实现
{
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < nameSeed.size(); 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+1);
//将选手以及对应的编号放入map容器中
this->m_Speaker.insert(make_pair(i + 1, sp));
}
}
void SpeechManager::startSpeech()//比赛流程控制函数的实现
{
//第一轮比赛开始
//1.抽签
speechDraw();
//2.比赛
speechContest();
//3.显示比赛结果
showScore();
this->m_Index++;//准备进入第二轮比赛
//第二轮比赛开始
//1.抽签
speechDraw();
//2.比赛
speechContest();
//3.显示比赛最终结果
showScore();
//4.保存到文件中
saveRecord();
//重置比赛
//初始化属性
this->initSpeech();
//创建选手
this->creatSpeaker();
//获取往届记录
this->loadRecord();
}
void SpeechManager::speechDraw()//抽签功能的实现
{
cout << "第--" << this->m_Index << "--轮选手抽签开始!" << endl;
cout << "--------------------------------------" << 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");
}
void SpeechManager::speechContest()//比赛功能的实现
{
cout << "-------第" << this->m_Index << "轮比赛正式开始!--------" << endl;
//greater从大到小排序
multimap<double, int, greater<double>>groupScore;//临时容器,保存key分数,value选手编号
int num = 0;//记录人员个数,6人一组
vector<int>v_Src;//比赛人员容器
//根据比赛轮数,来判断比赛人数
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++;
//评委打分
deque<double>d;
for (int i = 0; i < 10; i++)
{
double score = (rand() % 100) / 10.f;//0--10
d.push_back(score);
}
sort(d.begin(), d.end(), greater<double>());
d.pop_back();//去除最低分
d.pop_front();//去除最高分
//求平均分 accumulate求和,求总
double sum = accumulate(d.begin(), d.end(), 0.0f);
double avg = sum / (double)d.size();
//每个人的平均分
this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;
//6个人一组,用临时容器保存
groupScore.insert(make_pair(avg, *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 << "-----------第" << 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 << " 姓名" << m_Speaker[*it].m_Name << " 成绩:" << 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 << "," << m_Speaker[*it].m_Score[1] << ",";
}
ofs << endl;
//关闭文件
ofs.close();
cout << "记录已经保存" << endl;
//有记录了,文件不为空
this->fileIsEmpty = false;
}
void SpeechManager::show_Menu()//菜单功能的实现
{
cout << "******************************************" << endl;
cout << "************ 欢迎参加演讲比赛 ************" << endl;
cout << "************ 1.开始演讲比赛 ************" << endl;
cout << "************ 2.查看往届记录 ************" << endl;
cout << "************ 3.清空比赛记录 ************" << endl;
cout << "************ 4.退出比赛程序 ************" << endl;
cout << endl;
}
void SpeechManager::loadRecord()//读取记录
{
ifstream ifs("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 temp = data.substr(start, pos - start);//找到了,进行分割 参数1:起始位置 参数2:截取长度
v.push_back(temp);
start = pos + 1;
}
this->m_Record.insert(make_pair(index, v));
index++;
}
ifs.close();
}
void SpeechManager::showRecord()//显示往届记录
{
if (this->fileIsEmpty)
{
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()
{
error://创建一个入口,如果用户输入的数不是1也不是2,那么重新输入
cout << "确认清空?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
int cr = 0;
cin >> cr;
switch (cr)
{
case 1:
{
//打开模式 ios::trunc 如果存在删除文件并重新创建
ofstream ofs("speech.csv", ios::trunc);
ofs.close();
//初始化属性
this->initSpeech();
//创建选手
this->creatSpeaker();
//获取往届记录
this->loadRecord();
cout << "记录清空成功!" << endl;
system("pause");
system("cls");
break;
}
case 2:
break;
default:
cout << "输入有误,请重新输入!" << endl;
goto error;
}
}
void SpeechManager::exitSystem()//退出功能的实现
{
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}
SpeechManager::~SpeechManager()//析构函数
{
}
成果演示
欢迎评论区大佬的交流!