C++练习题1:演讲比赛程序

C++练习题1:演讲比赛程序,第1张

C++练习题1:演讲比赛程序

规则:

学校举行一场演讲比赛,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。

比赛方式:分组比赛,每组6个人;选手每次要随机分组,进行比赛

每名选手都有对应的编号,如 10001 ~ 10012

第一轮分为两个小组,每组6个人。 整体按照选手编号进行抽签后顺序演讲。

当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。

第二轮为决赛,前三名胜出

每轮比赛过后需要显示晋级选手的信息

具体的视频讲解链接:

黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili

 建议可以先分三段听,然后记思路在纸上,然后自己不看示例自己按思路编出来,大约耗时1day下边是我按照讲解的思路写的,不必看我的,因为没注释,可以按照视频中的自己写

 Speaker.hpp

#pragma once
#include
#include
using namespace std;

class Speaker
{
public:
	Speaker();
	~Speaker();
	string c_name;
	double c_score[2];


};

Speaker::Speaker()
{
	for (int i = 0; i < 2; i++)
	{
		this->c_score[i] = 0;
	}
}

Speaker::~Speaker()
{

}

 SpeechManger.hpp

#pragma once
#include
#include 
#include 
#include
#include
#include
#include
#include
#include"Speaker.hpp"
using namespace std;

class SpeechManger
{
public:
	SpeechManger();
	void ShowMenu();
	void exitSystem();
	void initSpeech();
	void creatSpeaker();
	void startSpeech();//比赛环节

	void speechDraw();//抽签
	void speechContest();//比赛过程
	void showScore();//显示赛程赛果
	void saveRecord();//存储冠亚季军

	void loadRecord();
	void showRecord();
	void clearRecord();

	bool fileIsEmpty;
	map> m_Record;

	vector v1;
	vector v2;
	vector victory;
	map m_Speaker;
	int m_Index;


	~SpeechManger();
};


SpeechManger::SpeechManger()
{
	this->initSpeech();
	this->creatSpeaker();
	this->loadRecord();
	
}
void SpeechManger::ShowMenu()
{
	cout << "********************************************" << endl;
	cout << "*************  欢迎参加演讲比赛 ************" << endl;
	cout << "*************  1.开始演讲比赛  *************" << endl;
	cout << "*************  2.查看往届记录  *************" << endl;
	cout << "*************  3.清空比赛记录  *************" << endl;
	cout << "*************  0.退出比赛程序  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;
}
void SpeechManger::exitSystem()
{
	cout << "谢谢使用!" << endl;
	system("pause");
	exit(0);
}
void SpeechManger::initSpeech()
{
	this->v1.clear();
	this->v2.clear();
	this->victory.clear();
	this->m_Record.clear();
	this->m_Index = 1;
}
void SpeechManger::creatSpeaker()
{
	string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i != nameSeed.size(); i++)
	{
		string name = "选手";
		name += nameSeed[i];
		Speaker s1;
		s1.c_name = name;

		v1.push_back(1001 + i);
		this->m_Speaker.insert(make_pair(1001+i,s1));
	}
}
void SpeechManger::startSpeech()
{
	//1.抽签
	this->speechDraw();
	//2.开始比赛
	this->speechContest();
	//3.显示第一轮比赛成果
	this->showScore();
	//4.抽签
	this->m_Index++;
	this->speechDraw();
	//5.开始比赛
	this->speechContest();
	//6.显示前三名成绩
	this->showScore();
	//7.保存分数
	this->saveRecord();
	this->initSpeech();
	this->creatSpeaker();
	this->loadRecord();
	
}
void SpeechManger::speechDraw()
{
	if (this->m_Index == 1)//第一轮抽签
	{
		random_shuffle(this->v1.begin(), this->v1.end());
		cout << "第1轮抽签顺序如下:" << endl;
		for (vector::const_iterator it = this->v1.begin(); it != this->v1.end(); it++)
		{
			cout << (*it) << "  ";
		}
		cout << endl;
	}
	else//第二轮抽签
	{
		random_shuffle(this->v2.begin(), this->v2.end());
		cout << "第2轮抽签顺序如下:" << endl;
		for (vector::const_iterator it = this->v2.begin(); it != this->v2.end(); it++)
		{
			cout << (*it) << "  ";
		}
		cout << endl;
	}
	system("pause");
}
void SpeechManger::speechContest()
{
	vector v_Src;
	deque d;
	multimap> groupScore;
	int num = 0;
	if (this->m_Index == 1)
		v_Src =this-> v1;
	else
		v_Src = this->v2;
	for (vector::iterator it = v_Src.begin(); it != v_Src.end(); it++)
	{
		num++;
		for (int j = 0; j < 10; j++)
		{
			double score = (rand() % 401 + 600) / 10.f;
			d.push_back(score);
		}
		sort(d.begin(), d.end(), greater());
		d.pop_front(); 
		d.pop_back();
		double sum = accumulate(d.begin(), d.end(), 0.0);//从0开始累加
		double avg = sum / (double)d.size();
		this->m_Speaker[(*it)].c_score[this->m_Index - 1] = avg;
		groupScore.insert(make_pair(avg, (*it)));

		if (num % 6 == 0)
		{
			cout << "-----------第" << num/6<< "组的成绩结果---------------" << endl;
			for (multimap>::iterator it =groupScore.begin();it!= groupScore.end();it++)
			{
				cout << "序号:" << it->second << "   " << "姓名:" << this->m_Speaker[it->second].c_name << "   " << "本轮成绩:" << this->m_Speaker[it->second].c_score[this->m_Index - 1] << endl;
			}
			cout << endl;
			int count = 0;
			for (multimap>::iterator it = groupScore.begin(); it != groupScore.end() && count != 3; it++, count++)
			{
				if (this->m_Index == 1)
					v2.push_back(it->second);
				else
					victory.push_back(it->second);
			}
			groupScore.clear();
		}
	}

}
void SpeechManger::showScore()
{
	vector v3;
	if (this->m_Index == 1)
	{
		v3 = v2;
		cout << "---------------晋级第二轮名单-------------------------" << endl;
	}
	else
	{
		v3 = victory;
		cout << "---------------冠亚季军名单--------------------------" << endl;
	}
	for (vector::const_iterator it = v3.begin(); it != v3.end(); it++)
	{
		cout<< "序号:" << (*it) << "   " << "姓名:" <m_Index-1]<< endl;
	}
	system("pause");
	system("cls");
	this->ShowMenu();
}
void SpeechManger::saveRecord()
{
		ofstream ofs;
		ofs.open("speech.csv",ios::out|ios::app);
		ofs << endl;
		for (vector::iterator it = victory.begin(); it!=victory.end();it++)
		{
			ofs << (*it) << "," << this->m_Speaker[*it].c_score[1] << ",";
		}
		ofs << endl;
		cout << "保存成功!" << endl;
		ofs.close();
		this->fileIsEmpty = false;
		
}
void SpeechManger::loadRecord()
{
	ifstream ifs;
	ifs.open("speech.csv",ios::in);
	//判断文件是否存在
	if (!ifs.is_open())
	{
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}
	
	ifs.putback(ch);//putback函数调用形式为cin.putback(ch),其作用是将前面用get或者getline函数从输入流中读取的字符ch返回到输入流,插入到当前指针的位置,供后面读取。定义:
		//直观一点就是把输入流里的内容看做是一个字符串的列队 里面存放的都是一个一个的字符 而这里的putback函数就相当于列队里的push函数。
		//吃的给吐出来
	this->fileIsEmpty = false;
	string buf;
	vector r;
	int index = 0;
	while(ifs>>buf)
	{
		int pos = -1,start=0;
		while (true)
		{
			pos = buf.find(",",start);//要继续往下找
			if (pos== -1)
			{
				break;
			}
			r.push_back(buf.substr(start, pos - start));
			start = pos + 1;
		}
		m_Record.insert(make_pair(index, r));
		r.clear();
		index++;
	}
	ifs.close();
}
void SpeechManger::showRecord()
{
	if (this->fileIsEmpty)
	{
		cout << "文件为空或不存在" << endl;
		return;
	}
	else
	{
		for (map>::iterator it = m_Record.begin(); it != m_Record.end(); it++)
		{
			cout << "第" << (*it).first + 1 << "届t冠军编号:" << m_Record[(*it).first][0] << "得分:" << m_Record[(*it).first][1] << "t"
				<< "亚军编号:" << m_Record[(*it).first][2] << "得分:" << m_Record[(*it).first][3] << "t"
				<< "季军编号:" << m_Record[(*it).first][4] << "得分:" << m_Record[(*it).first][5] << endl;
		}
	}
}
void SpeechManger::clearRecord()
{
	cout << "请确认是否清除:" << endl;
	cout << "1.狠心删除t2.容我想想" << endl;
	int choice = 0;
	cin >> choice;
	if (choice == 1)
	{
		ofstream ofs;
		ofs.open("speech.csv", ios::trunc);
		ofs.close();
		this->initSpeech();
		this->creatSpeaker();
		this->loadRecord();
		cout << "内容已清空!" << endl;
	}
	system("pause");
	system("cls");
}

SpeechManger::~SpeechManger()
{

}

 主程序:

#include
#include
#include
#include"SpeechManger.hpp"
#include"Speaker.hpp"
using namespace std;

int main()
{
	srand((unsigned int)time(NULL));
	SpeechManger s;
	
	while (true)
	{
		s.ShowMenu();
		cout << "请输出业务编号:" ;
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 1:
			//比赛环节
			s.startSpeech();
			break;
		case 2:
			//查看往届信息
			s.showRecord();
			break;
		case 3:
			//清空比赛记录
			s.clearRecord();
			break;
		case 0:
			//退出比赛环节
			s.exitSystem();
			break;
		default:
			system("cls");
			break;
		}
	}

	return 0;
	system("pause");
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4994451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存