目录
功能介绍
代码结构部分
查看一下类图
1.Speaker.h
2.speechManager.h
3.speechManager.cpp
4.演讲比赛流程关系系统.cpp
功能介绍
speechManager.h函数包含演讲比赛流程的所有功能如下:
开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键进入下一个比赛阶段
查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀保存
清空比赛记录:将文件中数据清空
退出比赛程序:可以退出当前程序
//构造函数
SpeechManager();
//菜单功能
void show_Menu();
//退出系统
void exitSystem();
//析构
~SpeechManager();
//创建12名选手
void creatSpeaker();
//开始比赛 比赛流程的控制函数
void startSpeech();
//抽签
void speechDraw();
//比赛函数
void speechContest();
//显示得分
void showScore();
//保存分数
void saveScore();
//读取往届信息
void loadRecord();
//文件为空标志位
bool fileIsEmpty;
//往届记录
map<int, vector<string>>m_Record;
//查看往届得分
void showRecord();
//清空记录
void clearRecord();
//初始化容器和属性
void initSpeech();
//成员属性
//保存第一轮选手编号的容器
vector<int> v1;
//保存第二轮选手编号的容器
vector<int>v2;
//保存前三名选手编号容器
vector<int>vVictory;
//存放编号对应选手
map<int, Speaker> m_Speaker;
//存放比赛轮数
int m_Index;
代码结构部分
查看一下类图
分析类图:
Speaker类创建选手,选手信息包括姓名、得分
变量名 | 变量类型 | 范围 | 描述 |
m_Name | string | - | 选手姓名 |
m_Score | double | 0-100 | 选手得分 |
SpeechManager类
变量名 | 变量类型 | 范围 | 描述 |
v1 | vector<int> | - | 保存第一轮选手编号的容器 |
v2 | vector<int> | - | 保存第二轮选手编号的容器 |
vVictory | vector<int> | - | 保存前三名选手编号容器 |
m_Index | int | - | 存放比赛轮数 |
m_Record | map<int,vector<string>> | - | |
m_Speaker | map<int,Speaker> | - | 存放编号对应选手 |
fileIsEmpty | bool | - | 文件为空标志位 |
的字段v1、V2、vVictory用于分别对应存储:第一轮选手编号姓名和得分、第二轮编号姓名和得分、前三名选手编号姓名和得分 ;字段m_Index存储历史记录的届数,m_Record
1.Speaker.h
#pragma once
#include<iostream>
using namespace std;//选手类
class Speaker
{
public:string m_Name;double m_Score[2];//最多两轮得分
};
2.speechManager.h
#pragma once
#include<iostream>
using namespace std;
#include<vector>
#include<map>
#include"Speaker.h"
#include<algorithm>
#include<deque>
#include<functional> //内建函数greater<double>()
#include<numeric> //accumulate
#include<fstream>
//设计演讲管理类
class SpeechManager
{
public://构造函数SpeechManager();//菜单功能void show_Menu();//退出系统void exitSystem();//析构~SpeechManager();//创建12名选手void creatSpeaker();//开始比赛 比赛流程的控制函数void startSpeech();//抽签void speechDraw();//比赛函数void speechContest();//显示得分void showScore();//保存分数void saveScore();//读取往届信息void loadRecord();//文件为空标志位bool fileIsEmpty;//往届记录map<int, vector<string>>m_Record;//查看往届得分void showRecord();//清空记录void clearRecord();//初始化容器和属性void initSpeech();//成员属性//保存第一轮选手编号的容器vector<int> v1;//保存第二轮选手编号的容器vector<int>v2;//保存前三名选手编号容器vector<int>vVictory;//存放编号对应选手map<int, Speaker> m_Speaker;//存放比赛轮数int m_Index;};
3.speechManager.cpp
#include"speechManager.h"//构造函数
SpeechManager::SpeechManager()
{//初始化容器和属性this->initSpeech();//创建12名选手this->creatSpeaker();//加载往届记录this->loadRecord();
}void SpeechManager::show_Menu()
{cout << "**************************************" << endl;cout << "***********欢迎参加演讲比赛***********" << endl;cout << "************1.开始演讲比赛************" << endl;cout << "************2.查看往届记录************" << endl;cout << "************3.清空比赛记录************" << endl;cout << "************0.退出比赛程序************" << endl;cout << "**************************************" << endl;}void SpeechManager::exitSystem()
{cout << "欢迎下次使用" << endl;system("pause");exit(0);
}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()
{cout << "是否清空?" << endl;cout << "1.确认" << endl;cout << "2.返回" << endl;int select;cin >> select;if (select == 1){//确认清空ofstream ofs("speech.csv", ios::trunc);//trunc删除并重新创建新文件ofs.close();//初始化容器和属性this->initSpeech();//创建12名选手this->creatSpeaker();//加载往届记录this->loadRecord();}system("pause");system("cls");
}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 + 10001);//选手编号+选手 放入map容器中this->m_Speaker.insert(make_pair(i + 10001, sp));}}//开始比赛 比赛整个流程控制函数
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->saveScore();//重置比赛并获取记录//初始化容器和属性this->initSpeech();//创建12名选手this->creatSpeaker();//加载往届记录this->loadRecord();cout << "本届比赛完毕!" << endl;system("pause");system("cls");
}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");cout << endl;
}void SpeechManager::speechContest()
{cout << "--------第<<" << this->m_Index << ">>轮比赛正式开始--------" << endl;//准备临时容器 存放小组成绩multimap<double, int, greater<double>> groupScore;int num = 0;// 记录人员个数vector<int>v_Src;//比赛选手容器if (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() % 401 + 600) / 10.f; //600~1000//cout << score << " ";d.push_back(score);}//cout << endl;sort(d.begin(), d.end(), greater<double>());//排序 从大到小d.pop_front();//去除最高分d.pop_back();//去除最低分double sum = accumulate(d.begin(), d.end(), 0); //分数求和double avg = sum / (double)d.size(); //平均分//打印平均分//cout << "编号: " << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 平均分" << avg;//将平均分放入map容器this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;//cout << endl;//将打分数据放入临时小组容器中groupScore.insert(make_pair(avg, *it));//key是得分,value是具体选手编号//六人取出前三名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::saveScore()
{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->fileIsEmpty = false;}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);//将上面读取的单个字符再放回int index = 0;string data;while (ifs >> data){//cout << data << endl;//10002,86.375,10009,81,10010,78,vector<string>v;//存放6个string字符串int pos = -1;//查到","位置的变量int start = 0;while (true){pos = data.find(",", start);if (pos == -1){//没有找到的情况break;}string temp = data.substr(start, pos - start);//cout << temp << endl;v.push_back(temp);start = pos + 1;}this->m_Record.insert(make_pair(index, v));index++;}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;}*/
}//析构函数
SpeechManager::~SpeechManager()
{}
4.演讲比赛流程关系系统.cpp
#include<iostream>
using namespace std;
#include"speechManager.h"
#include<ctime>int main()
{//添加随机数种子srand((unsigned int)time(NULL));//创建管理类的对象SpeechManager sm;测试12名选手创建//for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++)//{// cout << "选手编号:" << it->first << " 姓名:" << it->second.m_Name << " 分数" << it->second.m_Score[0] << endl;//}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:system("cls"); //清屏break;}}system("pause");return 0;
}