#include "speechManager.h"
SpeechManager::SpeechManager()
{
this->intiSpeech();
this->createSpeaker();
this->read();
}
void SpeechManager::showManu()
{
cout << "************************************************" << endl;
cout << "*********** 1.开始演讲比赛 ********************" << endl;
cout << "*********** 2.查看往届比赛 ********************" << endl;
cout << "*********** 3.清空比赛记录 ********************" << endl;
cout << "*********** 0.退出系统 ********************" << endl;
cout << "************************************************" << endl;
cout << "请输入你的选项:" << endl;
}
void SpeechManager::exitSystem()
{
cout << "欢迎下次继续使用!" << endl;
system("pause");
exit(0);
}
void SpeechManager::startSpeech()
{
this->speakerDraw();
this->speechContest();
this->showScore();
this->index++;
this->speakerDraw();
this->speechContest();
this->showScore();
this->save();
this->intiSpeech();
this->createSpeaker();
this->read();
cout << "本届比赛结束!" << endl;
system("pause");
system("cls");
}
void SpeechManager::clearSpeech()
{
cout << "请确认清空数据:" << endl;
cout << "1.确认" << endl;
cout << "2.取消" << endl;
int choice;
cin >> choice;
if (choice == 1)
{
ofstream ofs;
ofs.open(FILENAME, ios::trunc);
ofs.close();
this->intiSpeech();
this->createSpeaker();
this->read();
cout << "清除成功!" << endl;
}
system("pause");
system("cls");
}
void SpeechManager::intiSpeech()
{
this->v1.clear();
this->v2.clear();
this->victory.clear();
this->m_Speaker.clear();
this->index = 1;
this->recond.clear();
}
void SpeechManager::createSpeaker()
{
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 < (sizeof(sp.m_Score) / sizeof(sp.m_Score[0])); j++)
{
sp.m_Score[j] = 0;
}
int id = 10001 + i;
this->v1.push_back(id);
this->m_Speaker.insert(make_pair(id, sp));
}
}
void SpeechManager::speakerDraw()
{
cout << "开始第" << this->index << "轮抽签!" << endl;
cout << "--------------------------------------" << endl;
cout << "抽签结果如下:" << endl;
if (this->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->index << "轮比赛!" << endl;
multimap <double, int,greater<double>> groupScore;
vector<int> src;
int num = 0;
if (this->index == 1)
{
src = this->v1;
}
else
{
src = this->v2;
}
for (vector<int> ::iterator it = src.begin(); it != 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_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[this->index - 1] = avg;
groupScore.insert(make_pair(avg, *it));
if (num % 6 == 0)
{
cout << "第" << num / 6 << "组比赛结果为:" << endl;
for (multimap<double, int, greater<double>>::iterator it2 = groupScore.begin(); it2 != groupScore.end(); it2++)
{
cout << "编号:" << it2->second << "\t姓名:" << this->m_Speaker[it2->second].m_Name << "\t分数:" << it2->first << endl;
}
int count = 0;
for (multimap<double, int, greater<double>> ::iterator it3 = groupScore.begin(); it3 != groupScore.end() && count < 3; it3++, count++)
{
if (this->index == 1)
{
this->v2.push_back(it3->second);
}
else
{
this->victory.push_back(it3->second);
}
}
groupScore.clear();
cout << endl;
}
}
cout <<"第"<<this->index <<"轮比赛结束!" <<endl;
system("pause");
}
void SpeechManager::showScore()
{
cout << "第" << this->index << "轮比赛晋级选手为:" << endl;
vector<int> v;
if (this->index == 1)
{
v = this->v2;
}
else
{
v = this->victory;
}
for (vector<int> ::iterator it = v.begin(); it != v.end(); it++)
{
cout << "选手编号:" << *it << "\t姓名:" << this->m_Speaker[*it].m_Name << "\t分数:" << this->m_Speaker[*it].m_Score[this->index - 1] << endl;
}
cout << endl;
system("pause");
system("cls");
this->showManu();
}
void SpeechManager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out | ios::app);
for (vector<int> ::iterator it = this->victory.begin(); it != this->victory.end(); it++)
{
ofs << *it << "," << this->m_Speaker[*it].m_Name << "," << this->m_Speaker[*it].m_Score[1]<<",";
}
ofs << endl;
ofs.close();
cout << "记录已保存!" << endl;
this->fileIsEmpty = false;
}
void SpeechManager::read()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
this->fileIsEmpty = true;
//cout << "文件不存在!" << endl;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
this->fileIsEmpty = true;
//cout << "文件为空!" << endl;
ifs.close();
return;
}
this->fileIsEmpty = false;
ifs.putback(ch);
string data;
int index = 0;
while (ifs >> data)
{
int pos = -1;
int start = 0;
vector<string> vv;
while (true)
{
pos = data.find(",", start);
if (pos == -1)
{
break;
}
string temp = data.substr(start, pos - start);
vv.push_back(temp);
start = pos + 1;
}
this->recond.insert(make_pair(index, vv));
vv.clear();
index++;
}
ifs.close();
}
void SpeechManager::showRecord()
{
if (this->fileIsEmpty)
{
cout << "文件为空或文件不存在!" << endl;
}
else
{
for (int i = 0; i < this->recond.size(); i++)
{
cout << "第" << i + 1 << "届"
<< " 冠军编号:" << this->recond[i][0]
<< "姓名:" << this->recond[i][1]
<< "分数:" << this->recond[i][2]
<< " 亚军编号:" << this->recond[i][3]
<< "姓名:" << this->recond[i][4]
<< "分数:" << this->recond[i][5]
<< " 季军编号:" << this->recond[i][6]
<< "姓名:" << this->recond[i][7]
<< "分数:" << this->recond[i][8]
<< endl;
}
}
system("pause");
system("cls");
}
SpeechManager::~SpeechManager()
{
}
评论0