入门级c++演讲比赛管理系统,来源黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难
感谢Netfishless大佬提供的讲义,《黑马》——C++提高编程
#pragma once
#include"speechManager.h"
SpeechManager::SpeechManager()
{
InitSpeech();
createSpeaker();
loadRecord();
}
void SpeechManager::show_Menu()
{
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::startMatch()
{
//第一轮开始
//1.抽签
this->draw();
//2.比赛
this->speech();
//3.显示结果
this->showScore();
//第二轮开始
//1.抽签
this->draw();
//2.比赛
this->speech();
//3.显示结果
this->showScore();
//4.保存结果到文件
this->saveScore();
cout << "本届比赛完毕!" << endl;
//重置比赛状态
this->InitSpeech();
this->createSpeaker();
this->loadRecord();
}
void SpeechManager::createSpeaker()
{
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < 12; i++)
{
string name = "选手[";
name += nameSeed[i];
name += "]";
Speaker sp;
sp.m_Name = name;
for (int j = 0; j < 2; j++)
{
sp.m_Score[j] = 0;
}
v1.push_back(i+101);
m_Speaker.insert(make_pair(i+101, sp));
}
}
void SpeechManager::draw()
{
this->m_Index++;
cout << "第 <" << m_Index << ">论正在抽签" << endl;
cout << "--------------------------" << endl;
cout << "抽签后的演讲顺序如下:" << endl;
if (m_Index == 1)
{
random_shuffle(v1.begin(), v1.end());
for (vector<int>::iterator it=v1.begin(); it != v1.end(); it++)
{
cout << *it << " ";
}
}
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()
{
cout << "--------------------第 < " << this->m_Index << " >轮比赛开始--------------------" << endl;
multimap<double, int, greater<double>>groupScore;//存放平均分和编号,用来找每一组的前三名
int num=0;//记录到第几个人了
vector<int>vSpe;
if (this->m_Index == 1)
{
vSpe = this->v1;
}
else
{
vSpe = this->v2;
}
//遍历参赛选手,评委打分
for (vector<int>::iterator it = vSpe.begin(); it != vSpe.end(); it++)
{
deque<double>d;
for (int i = 0; i < 10; i++)
{
double score = (rand() % 401 + 600) / 10.f;
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.0f);
double avg = sum / double(d.size());
this->m_Speaker[*it].m_Score[m_Index - 1] = avg;//把分数放进去
num++;
groupScore.insert(make_pair(avg, (*it)));
if (num % 6 == 0)
{
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++)
{
cout << "编号:" << (*it).second << " 姓名:" << this->m_Speaker[it->second].m_Name << " 成绩:"
<< (*it).first << " 成绩:" << this->m_Speaker[it->second].m_Score[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
{
vV.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 = vV;
}
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");
}
void SpeechManager::saveScore()
{
ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app);
for (vector<int>::iterator it = vV.begin(); it != vV.end(); it++)
{
ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << "," ;
}
ofs << endl;
ofs.close();
this->fileIsEmpty = false;
cout << "保存完毕" << endl;
system("pause");
}
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);
if (pos == -1)
{
break;
}
string strT = data.substr(start, pos - start);
v.push_back(strT);
start = pos + 1;
//cout << strT << endl;
}
this->m_Recdord.insert(make_pair(index,v));
index++;
}
ifs.close();
}
void SpeechManager::showRecord()
{
if (this->fileIsEmpty)
{
cout << "文件不存在或文件为空" << endl;
return;
}
for (int i = 0; i < this->m_Recdord.size(); i++)
{
cout << "-------------------- 往届记录 --------------------" << endl;
cout << "第 < " << i+1 << " > 届"<<endl;
cout << "冠军编号:" << this->m_Recdord[i][0] << " 冠军得分:" << this->m_Recdord[i][1] << endl;
cout << "亚军编号:" << this->m_Recdord[i][2] << " 亚军得分:" << this->m_Recdord[i][3] << endl;
cout << "季军编号:" << this->m_Recdord[i][4] << " 季军得分:" << this->m_Recdord[i][5] << endl;
}
}
void SpeechManager::clearRecord()
{
cout << "确定要清空吗?\n1.确定\n2.取消" << endl;
int choice = 0;
cin >> choice;
if (choice == 1)
{
ofstream ofs("speech.csv",ios::trunc);
ofs.close();
this->InitSpeech();
this->createSpeaker();
this->loadRecord();
}
}
SpeechManager::~SpeechManager()
{
}
void SpeechManager::InitSpeech()
{
this->v1.clear();
this->v2.clear();
this->vV.clear();
this->m_Speaker.clear();
this->m_Index = 0;
this->m_Recdord.clear();
}