SpeechManager.h头文件创建
其中的接口是一步一步在后面实现的功能时候加的
一开始从菜单show_menu接口开始
#pragma once
#include<iostream>
#include<vector>
#include<map>
#include"speaker.h"
using namespace std;
class SpeechManager {
public:
SpeechManager();
void show_menu();
void exit_system();
void init_Speech();
void create_speaker();
void start_speech();
void speech_Draw();
void speech_Contest();
void showScore();
void saveRecord();
void loadRecord();
void showRecord();
void clearRecord();
~SpeechManager();
vector<int> v1;//第一轮编号
vector<int> v2;//第二轮编号
vector<int> victory;//冠亚季军编号
map<int, Speaker> m_Speaker;//存放所有选手信息
map<int, vector<string>> m_Record;//用于读取文件内容,每届冠亚季军信息
bool fileIsEmpty = false;//用来判断文件是否为空
int s_index = 1;//用来判断轮数
};
演讲比赛STL.cpp源文件
这是测试文件
#include <bits/stdc++.h>
#include "speechManager.h"
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
//创建管理类对象
SpeechManager s;
int choice;
while (true)
{
s.show_menu();
cin >> choice;
switch (choice)
{
case 1://开始比赛
s.start_speech();
break;
case 2://查看往届信息
s.showRecord();
break;
case 3://清空往届信息
s.clearRecord();
break;
case 4://退出
s.exit_system();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
然后是封装speecher对象类
头文件
#pragma once
#include<iostream>
using namespace std;
class Speaker
{
public:
void setName(string dname);
string getName();
void setScore1(double dscore);
double getScore1();
void setScore2(double dscore);
double getScore2();
private:
string name;
double score[2];
};
源文件
score1是第一轮成绩,score2是第二轮成绩
#include "speaker.h"
void Speaker::setScore1(double dScore)
{
this->score[0] = dScore;
}
void Speaker::setScore2(double dScore)
{
this->score[1] = dScore;
}
void Speaker::setName(string dname)
{
this->name = dname;
}
string Speaker::getName()
{
return this->name;
}
double Speaker::getScore1()
{
return this->score[0];
}
double Speaker::getScore2()
{
return this->score[1];
}
最后就是最最最重要的一部分具体功能实现源码
#include "speechManager.h"
#include<bits/stdc++.h>
SpeechManager::SpeechManager()
{
this->init_Speech();
this->create_speaker();
this->loadRecord();
}
void SpeechManager::show_menu()
{
cout << "========================================" << endl;
cout << "***********欢迎来到演讲比赛**********" << endl;
cout << "***********1.开始比赛****************" << endl;
cout << "***********2.查看往届记录************" << endl;
cout << "***********3.清空比赛记录************" << endl;
cout << "***********4.退出比赛程序************" << endl;
cout << "========================================" << endl;
}
void SpeechManager::exit_system()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
void SpeechManager::init_Speech()
{
this->v1.clear();
this->v2.clear();
this->victory.clear();
this->m_Speaker.clear();
this->s_index = 1;
this->m_Record.clear();
}
void SpeechManager::create_speaker()
{
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < 12; i++)
{
string name = "选手";
name += nameSeed[i];
Speaker sp;
sp.setName(name);
sp.setScore1(0);
sp.setScore2(0);
this->v1.push_back(i + 10001);
this->m_Speaker.insert(make_pair(i + 10001, sp));
}
}
void SpeechManager::start_speech()
{
//第一轮;
//抽签
this->speech_Draw();
//比赛
this->speech_Contest();
//晋级名单;
this->showScore();
this->s_index++;
//第二轮
this->speech_Draw();
//比赛
this->speech_Contest();
//晋级名单;
this->showScore();
//保存记录
this->saveRecord();
//重新初始化
this->init_Speech();
this->create_speaker();
this->loadRecord();
cout << "本届比赛结束" << endl;
system("pause");
system("cls");
}
void SpeechManager::speech_Draw()
{
cout << "第"<<this->s_index<<"轮抽签" << endl;
cout << "---------===---------" << endl;
cout << "抽签结果如下:" << endl;
if (this->s_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::speech_Contest()
{
cout << "第" << this->s_index << "轮比赛开始" << endl;
cout << "----!!!!!!!!!!!!!!!!!!!!!!!!!-----" << endl;
vector<int>v_Src;//临时存放小组编号
//临时容器 存放小组成绩
multimap<double, int, greater<double>> groupScore;
if (this->s_index == 1)
{
v_Src = v1;
}
else
v_Src = v2;
int num = 0;//num当变成6的倍数为一组
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;
//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.0f);//累加
const double avg = sum / d.size();
if (this->s_index == 1)
this->m_Speaker[*it].setScore1(avg);
else
this->m_Speaker[*it].setScore2(avg);
//cout << "编号: " << *it << " 姓名: " << this->m_Speaker[*it].getName() << " 成绩: " << this->m_Speaker[*it].getScore1() << endl;
//pair<double,int> p(avg, *it);
//groupScore.insert(p);
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].getName();
if (this->s_index == 1)
cout << " 成绩: " << this->m_Speaker[it->second].getScore1() << endl;
else
cout << " 成绩: " << this->m_Speaker[it->second].getScore2() << endl;
}
int count = 0;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++)
{
if (this->s_index == 1)
v2.push_back(it->second);
else
victory.push_back(it->second);
}
groupScore.clear();
cout << endl;
}
}
cout << "第" << this->s_index << "轮比赛结束" << endl;
system("pause");
}
void SpeechManager::showScore()
{
cout << "第" << this->s_index << "轮的晋级结果" << endl;
vector<int> v;
if (this->s_index == 1)
{
v = v2;
}
else
v = victory;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{//*it取出编号
cout << "姓名:" << this->m_Speaker[*it].getName();
if (this->s_index == 1)
cout<< " 成绩:" << this->m_Speaker[*it].getScore1() << endl;
else
cout << " 成绩:" << this->m_Speaker[*it].getScore2() << 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 = victory.begin(); it != victory.end(); it++)
{
ofs << *it << "," << this->m_Speaker[*it].getScore2() << ",";
}//输入文件内容ofs<<
ofs<< endl;
ofs.close();
cout << "比赛记录已保存" << endl;
this->fileIsEmpty = false;
}
void SpeechManager::loadRecord()
{//这个接口实现的作用是读取csv文件,方便后边的展示历届结果
ifstream ifs;
ifs.open("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 = 1;
while (ifs >> data)
{
vector<string> v222;
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 <<" ";
v222.push_back(temp);
start = pos + 1;
}
cout << endl;
this->m_Record.insert(make_pair(index, v222));//把文件内容一届一届的分开,用map来存储vector,一个vector存储一届的信息
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 << "届结果:"//因为存储是从1开始,所以访问从1开始
<< "冠军编号:" << this->m_Record[i+1][0] << " 得分:" << this->m_Record[i + 1][1]<<" "
<< "亚军编号:" << this->m_Record[i + 1][2] << " 得分:" << this->m_Record[i + 1][3] << " "
<< "季军编号:" << this->m_Record[i + 1][4] << " 得分:" << this->m_Record[i + 1][5] << endl;
}
system("pause");
system("cls");
}
void SpeechManager::clearRecord()
{
cout << "是否清除记录" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
int select = 0;
cin >> select;
ofstream ofs;
if (select==1)
{
ofs.open("speech.csv", ios::trunc);
this->init_Speech();
this->create_speaker();//可以加个load验证一下是否为空
cout << "清空成功" << endl;
ofs.close();
}
system("pause");
system("cls");
}
SpeechManager::~SpeechManager()
{
}
rush