设为首页 加入收藏

TOP

C++项目实战之演讲比赛流程管理系统(五)
2023-08-26 21:10:35 】 浏览:292
Tags:项目实 程管理
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");
}

运行代码,测试效果

image-20230817212724575

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、保存分数
}
image-20230818005010269

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();
}
image-20230818011155645

至此,整个演讲比赛功能制作完毕!

6. 查看记录

6.1 读取记录分数

  1. speechManager.h 中添加保存记录的成员函数 void loadRecord();

  2. 添加判断文件是否为空的标志 bool fileIsEmpty;

  3. 添加往届记录的容器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::
首页 上一页 2 3 4 5 6 7 8 下一页 尾页 5/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Qt6】QWidgetAction 的使用 下一篇9.1 C++ STL 排序、算数与集合

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目