设为首页 加入收藏

TOP

C++项目实战之演讲比赛流程管理系统(二)
2023-08-26 21:10:35 】 浏览:294
Tags:项目实 程管理
pragma once #include <iostream> class Speaker { public: std::string m_Name; //选手姓名 double m_Score[2]; //选手分数,至多存放两轮 };

5.3 比赛

5.3.1 成员属性添加

speechManager.h 中添加属性

	//容器1: 存放12位选手的编号
	std::vector<int> v1;

	//容器2: 存放6位选手的编号
	std::vector<int> v2;

	//容器3: 存放3位选手的编号
	std::vector<int> v_Victory;

	//容器4: 存放Speaker的编号和对应的选手
	std::map<int, Speaker> m_Speaker;

	//比赛轮数
	int m_Round;

5.3.2 初始化属性

speechManager.h 中提供开始比赛的的成员函数 void initSpeech();

void initSpeaker();

speechManager.cpp 中实现开始比赛的的成员函数 void initSpeech();

void SpeechManager::initSpeaker()
{
	//清空容器
	this->v1.clear();
	this->v2.clear();
	this->v_Victory.clear();
	this->m_Speaker.clear();

	//第一轮比赛
	this->m_Round = 1;
}

SpeechManager 构造函数中调用void initSpeech();

SpeechManager::SpeechManager()
{
	//初始化
	this->initSpeaker();
}

5.3.3 创建选手

speechManager.h 中提供开始比赛的的成员函数 void createSpeaker();

//创建选手
void createSpeaker();

speechManager.cpp 中实现开始比赛的的成员函数 void createSpeaker();

void SpeechManager::createSpeaker()
{
	std::string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < nameSeed.size(); i++)
	{
		//初始化选手中的属性
		Speaker sp;
		sp.m_Name = "选手";
		sp.m_Name += nameSeed[i];
		for (int j = 0; j < 2; j++)
		{
			sp.m_Score[j] = 0;
		}

		//将初始化后的选手放入容器中
		this->v1.push_back(i + 1000);
		this->m_Speaker.insert(std::pair<int, Speaker>(i + 1000, sp));
	}
}

SpeechManager类的 构造函数中调用 void createSpeaker();

SpeechManager::SpeechManager()
{
	//初始化
	this->initSpeaker();

	//创建选手
	this->createSpeaker();
}

测试 在main函数中,可以在创建完管理对象后,使用下列代码测试12名选手初始状态

for (std::map<int, Speaker>::iterator it = spm.m_Speaker.begin(); it != spm.m_Speaker.end(); it++)
{
    std::cout
        << it->first << " "
        << it->second.m_Name << " "
        << (*it).second.m_Score[0] << std::endl;
}

5.3.4 开始比赛成员函数添加

speechManager.h 中提供开始比赛的的成员函数 void startSpeech();

该函数功能是主要控制比赛的流程

//开始比赛 - 比赛流程控制
void startSpeech();

speechManager.cpp 中将 startSpeech 的空实现先写入

我们可以先将整个比赛的流程 写到函数中

void SpeechManager::startSpeech()
{
	//第一轮比赛
	//1、抽签

	//2、比赛

	//3、显示晋级结果

	//第二轮比赛

	//1、抽签

	//2、比赛

	//3、显示最终结果

	//4、保存分数
}

5.3.5 抽签

功能描述:

正式比赛前,所有选手的比赛顺序需要打乱,我们只需要将存放选手编号的容器 打乱次序即可

speechManager.h 中提供抽签的的成员函数 void speechDraw();

//抽签
void speechDraw();

speechManager.cpp 中实现成员函数 void speechDraw();

void SpeechManager::speechDraw()
{
	std::cout << "第" << this->m_Round << "轮比赛开始抽签" << std::endl;
	std::cout << "=*=*=*=*=*=*=*=*=" << std::endl;
	std::cout << "抽签后演讲顺序如下: " << std::endl;
	if (this->m_Round == 1)
	{
		std::random_shuffle(this->v1.begin(), this->v1.end());
		for (std::vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
		{
			std::cout << *it << " ";
		}
		std::cout << std::endl;
	}
	else
	{
		std::random_shuffle(this->v2.begin(), this->v2.end());
		for (std::vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
		{
			std::cout << *it << " ";
		}
		std::cout << std::endl;
	}
	std::cout << &
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Qt6】QWidgetAction 的使用 下一篇9.1 C++ STL 排序、算数与集合

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目