ndl; }
}
}
std::cout << "==========第" << this->m_Round << "轮比赛结束==========" << std::endl;
++this->m_Round;
system("pause");
}
在 startSpeech
比赛流程控制的函数中,调用比赛函数
void SpeechManager::startSpeech()
{
//第一轮比赛
//1、抽签
this->speechDraw();
//2、比赛
this->speechContest();
//3、显示晋级结果
//第二轮比赛
//1、抽签
//2、比赛
//3、显示最终结果
//4、保存分数
}
5.3.7 显示比赛分数
在 speechManager.h
中提供比赛的的成员函数 void showScore();
//显示分数
void showScore();
在 speechManager.cpp
中实现成员函数 void showScore();
void SpeechManager::showScore()
{
std::cout << "==========第" << this->m_Round << "轮比赛名次==========" << std::endl;
std::vector<int> v;
if (this->m_Round == 1)
{
v = v2;
}
else
{
v = v_Victory;
}
for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
std::cout
<< "选手编号: " << *it << " "
<< "选手姓名: " << this->m_Speaker[*it].m_Name << " "
<< "选手分数: " << this->m_Speaker[*it].m_Score[this->m_Round - 1]
<< std::endl;
}
system("pause");
system("cls");
}
运行代码,测试效果
5.3.8 第二轮比赛
第二轮比赛流程同第一轮,只是比赛的轮是+1,其余流程不变
在 startSpeech
比赛流程控制的函数中,加入第二轮的流程
void SpeechManager::startSpeech()
{
//第一轮比赛
//1、抽签
this->speechDraw();
//2、比赛
this->speechContest();
//3、显示晋级结果
this->showScore();
//第二轮比赛
++this->m_Round;
//1、抽签
this->speechDraw();
//2、比赛
this->speechContest();
//3、显示最终结果
this->showScore();
//4、保存分数
}
5.4 保存分数
功能描述:将每次演讲比赛的得分记录到文件中
功能实现:
在 speechManager.h
中添加保存记录的成员函数 void saveRecord();
//保存比赛分数
void saveRecord();
在 speechManager.cpp
中实现成员函数 void saveRecord();
void SpeechManager::saveRecord()
{
std::ofstream ofs("speech.csv", std::ios::out | std::ios::app);
for (std::vector<int>::iterator it = v_Victory.begin(); it != v_Victory.end(); it++)
{
ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << ",";
}
ofs << std::endl;
std::cout << "保存分数完成" << std::endl;
std::cout << "本届比赛正式结束!" << std::endl;
this->fileIsEmpty = false;
system("pause");
system("cls");
}
在 startSpeech
比赛流程控制的函数中,最后调用保存记录分数函数
void SpeechManager::startSpeech()
{
//第一轮比赛
//1、抽签
this->speechDraw();
//2、比赛
this->speechContest();
//3、显示晋级结果
this->showScore();
//第二轮比赛
++this->m_Round;
//1、抽签
this->speechDraw();
//2、比赛
this->speechContest();
//3、显示最终结果
this->showScore();
//4、保存分数
this->saveRecord();
}
至此,整个演讲比赛功能制作完毕!
6. 查看记录
6.1 读取记录分数
-
在 speechManager.h
中添加保存记录的成员函数 void loadRecord();
-
添加判断文件是否为空的标志 bool fileIsEmpty;
-
添加往届记录的容器map<int, vector<string>> m_Record;
其中 m_Record
中的 key
代表第几届,value
记录具体的信息
在 SpeechManager
构造函数中调用获取往届记录函数
//读取记录
void loadRecord();
//文件为空的标志
bool fileIsEmpty;
//往届记录
map<int, vector<string>> m_Record;
在 speechManager.cpp
中实现成员函数 void loadRecord();
void SpeechManager::loadRecord()
{
std::