项目练习三

这两天做了第三个项目练习——演讲比赛。

整个系统主要是四个功能:

  1. 开始比赛
  2. 查看往届记录
  3. 清空记录
  4. 退出系统

整个比赛的流程是:抽签——开始演讲比赛——显示第一轮比赛结果——抽签——开始演讲比赛——显示前三名结果——保存分数。

最难的点肯定是比赛流程的设计,后面会详细讲一下,因为这个项目练习已经是进阶版了,所以后续没有做优化版的提升(主要还是我太菜了,觉得目前能优化的已经差不多了,而且最近有别的新的事儿要做,就稍稍摆烂一下,做这些练习的目的是因为后续想自己写一下关于老板项目的c++原型系统,到时候也会记录一下)。

首先肯定是创建管理类(speechManager)用来串联所有功能,成员变量有:第一轮选手编号(vector<int> v1),第二轮选手编号(vector<int> v2),胜出前三名选手编号(vector<int> victory),存放选手编号和对应的两轮比赛的成绩(map<int, 选手类> m_Speaker),比赛轮次(int index),判断记录是否为空(bool fileIsEmpty),存放往届记录(map<int, vector<string>> m_Record),成员函数有:构造函数(里面涉及初始化容器、初始化选手(新建十二个选手)、初始化记录),菜单显示函数,比赛函数(里面涉及抽签函数、比赛函数(也可以说是打分函数)、结果显示函数,保存分数函数),查看往届记录函数(里面涉及加载记录函数),清空数据函数(两次确认 + 构造函数里的三个初始化函数)。

其次是选手类(speaker),ps:其实这个应该在最前面,因为选手不涉及什么操作,主要就是两个成员变量,一个是姓名,一个是存放两次比赛的vector,(可能会疑惑这里不需要编号吗?不需要嗷,原因是在我们的管理类当中有一个map容器是用来存放选手编号和对应的姓名+分数的)。

最后就是main函数,创建一个管理类(sm),读入选择(int select),根据select的选择去执行相应的成员函数。

下面详细分析一下各函数的代码逻辑:

  • 构造函数

初始化容器:所有容器clear,比赛轮次置为1。

初始化选手:姓名+编号,放在第一轮的选手容器(v1),同步更新到m_Speaker。

初始化记录:判断一下文件是否存在,是否为空,不为空的话需要整条读出,然后用substr去分割记录,将分割好的记录放进m_Record中。

  • 菜单显示函数(没啥难度,不写了)
  • 比赛函数

抽签函数:用random_shuffle函数对容器中的选手进行随机排序。

比赛函数:每六个人更新一次,用一个multimap<double, int, greater<double>> groupScore来临时存放6个人的成绩,以他们的成绩作为key,方便排序,当然也可以用名字作为key,不过要重写排序算法。因为这个函数要同时供第一轮和第二轮比赛使用,所以用一个临时容器vector<int> v来存放这一轮的选手编号。每个选手的分数(有10名评委打分)存放在容器deque<double>中,排序完以后pop掉头和尾(pop掉头和尾的原因是去掉最高和最低分),然后计算sum和avg,得到的avg存入选手的m_score对应的轮次分数,并且把分数放到小组分数容器中(groupScore),并取前三名放到下一轮比赛的选手容器中。

结果显示函数:没啥难度,就用临时变量来获取下轮选手,然后遍历cout。

保存分数函数:用ofstream的ios::app追加写文件的方式记录到文件中,以“,”分割,只记录前三名的序号和成绩,要记得保存以后要立即把fileIsEmpty置false。

  • 查看往届记录函数

加载记录函数:在构造函数中已经说过,不具体再讲了。

  • 清空文件函数

也没啥难度,就是两次确认,然后以ios::trunc的方式打开文件,然后关闭文件,来实现对于文件内容的清空,然后重新调用构造函数里的三个初始化函数,实现对成员变量的清空重置。

最后放上完整代码

//头文件部分
********************************************************************************
//speaker.h
********************************************************************************
#pragma once
#include<iostream>
using namespace std;

class Speaker {
public:
	string m_Name;
	double m_Score[2];
};
********************************************************************************
//speechManager.h
********************************************************************************
#pragma once
#define FileName "speech.csv"
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<deque>
#include<functional>
#include<numeric>
#include<string>
#include<fstream>
#include"speaker.h"
using namespace std;

//设计演讲管理类
class SpeechManager {
public:
	//成员属性
	//保存第一轮比赛选手编号容器
	vector<int> v1;
	//第一轮晋级选手编号容器
	vector<int> v2;
	//胜出前三名选手编号容器
	vector<int> victory;
	//存放编号以及对应具体选手容器 
	map<int, Speaker> m_Speaker;
	//存放比赛轮数
	int m_Index;
	//判断容器是否为空
	bool fileIsEmpty;
	//存放往届记录
	map<int, vector<string>> m_Record;
	
	//容器初始化
	void InitSpeech();

	//选手初始化
	void CreateSpeaker();

	//构造函数
	SpeechManager();

	//菜单功能
	void Show_Menu();

	//开始比赛,整个比赛流程的控制函数
	void StartSpeech();

	//抽签函数
	void SpeechDraw();

	//比赛函数
	void SpeechContest();

	//显示得分
	void ShowScore();

	//保存记录
	void SaveRecord();

	//读取记录
	void LoadRecord();

	//显示往届记录
	void ShowRecord();

	//清空数据
	void ClearRecord();

	//退出系统
	void Exit_System();

	~SpeechManager();
};


********************************************************************************
//头文件部分
********************************************************************************
//speechManager.cpp
********************************************************************************
#include"speechManager.h"

void SpeechManager::InitSpeech() {
	this->v1.clear();
	this->v2.clear();
	this->victory.clear();
	this->m_Speaker.clear();

	this->m_Index = 1;
	//将记录容器也清空一下
	this->m_Record.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 < 2; j++) {
			sp.m_Score[j] = 0;
		}

		//创建的选手编号放入到第一轮的选手容器中
		this->v1.push_back(i + 10001);

		//将创建的选手编号放入到map容器中
		this->m_Speaker.insert(make_pair(i + 10001, sp));
	}
}

SpeechManager::SpeechManager() {
	this->InitSpeech();

	this->CreateSpeaker();

	this->LoadRecord();
}

void SpeechManager::Show_Menu() {
	cout << "欢迎参加演讲比赛!" << endl;
	cout << "1、开始演讲比赛" << endl;
	cout << "2、查看往届记录" << endl;
	cout << "3、清空比赛记录" << endl;
	cout << "0、退出比赛程序" << endl;
}

void SpeechManager::StartSpeech() {
	//第一轮
	//1、抽签
	this->SpeechDraw();
	//2、比赛
	this->SpeechContest();
	//3、显示比赛结果
	this->ShowScore();
	//第二轮
	this->m_Index++;
	//1、抽签
	this->SpeechDraw();
	//2、比赛
	this->SpeechContest();
	//3、显示最终结果
	this->ShowScore();
	//4、保存结果到文件
	this->SaveRecord();

	//重置比赛,获取记录
	//初始化容器
	this->InitSpeech();
	//创建十二个选手
	this->CreateSpeaker();
	//加载往届记录
	this->LoadRecord();//很关键啊,每次获得新纪录后都要更新一下m_Record,这样立刻查看所有记录就会有最新的记录在里面了

	cout << "本届比赛完毕,感谢参与!" << endl;
	system("pause");
	system("cls");


}

void SpeechManager::SpeechDraw() {
	cout << "第" << this->m_Index  << "轮比赛选手正在抽签" << endl;
	cout << "----------------------------------------" << endl;
	cout << "抽签后演讲顺序如下:" << endl;

	if (this->m_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->m_Index << "轮比赛正式开始------------" << endl;

	//用临时容器去存放小组成绩
	multimap<double, int, greater<double>> groupScore;
	//记录人员个数,满6更新一次
	int num = 0;

	vector<int> v_Src;//代表比赛选手的容器

	if (this->m_Index == 1) {
		v_Src = v1;
	}
	else {
		v_Src = v2;
	}
	for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {
		num++;
		deque<double> d;
		for (int i = 0; i < 10; i++) {
			double score = (rand() % 401 + 600) / 10.f;//随即一个600~1000(余数范围在0~400,然后 + 600,范围就是600~1000)
			//cout << score << " ";
			d.push_back(score);
		}
		//cout << endl;
		sort(d.begin(), d.end(), greater<double>());
		d.pop_front();
		d.pop_back();

		double sum = accumulate(d.begin(), d.end(), 0.0f);//总分
		double avg = sum / (double)d.size();//平均分

		//获取平均分
		//cout << "编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 平均分:" << avg << endl;
		//将平均分放入map中
		this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;

		//将打分数据放到临时小组数据中
		groupScore.insert(make_pair(avg, *it));

		//每六人取出前三名
		if (num % 6 == 0) {
			cout << "第" << num / 6 << "小组的比赛名次:" << endl;
			for (multimap<double, int, greater<double>> ::iterator it = groupScore.begin(); it != groupScore.end(); it++) {
				cout << "编号:" << it->second << " 姓名:" << this->m_Speaker[it->second].m_Name << " 成绩:" << this->m_Speaker[it->second].m_Score[this->m_Index - 1] << endl;
			}

			//取走前三名,放入下一个容器中
			int count = 0;
			for (multimap<double, int, greater<double>>::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();//小组容器清空
			cout << endl;
		}
	}
	cout << "------------第" << this->m_Index << "轮比赛完毕!------------" << endl;
	system("pause");
}

void SpeechManager::ShowScore() {
	cout << "------------第" << this->m_Index << "轮晋级选手信息如下:------------" << endl;
	vector<int> v;
	if (this->m_Index == 1) {
		v = v2;
	}
	else {
		v = victory;
	}

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "选手编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 得分:" << this->m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
	}
	cout << endl;

	system("pause");
	system("cls");
	this->Show_Menu();
}

void SpeechManager::SaveRecord() {
	ofstream ofs;
	ofs.open(FileName, ios::out | ios::app);//以追加的方式续写文件

	//将每个选手的数据写入文件中
	for (vector<int>::iterator it = victory.begin(); it != victory.end(); it++) {
		ofs << *it << "," << this->m_Speaker[*it].m_Score[1] << ",";
	}
	//冠亚季军的结果算是一条记录,因为是按一次一次的比赛来算的,所以是记录完一次比赛成绩之后再输出回车
	ofs << endl;

	ofs.close();
	cout << "记录已经保存!" << endl;

	//更改文件状态,及时更新
	this->fileIsEmpty = false;
}

void SpeechManager::LoadRecord() {
	ifstream ifs(FileName, ios::in);
	//文件不存在
	if (!ifs.is_open()) {
		this->fileIsEmpty = true;
		cout << "文件不存在!" << endl;
		ifs.close();
	}

	//文件清空
	char ch;
	ifs >> ch;
	if (ifs.eof()) {
		cout << "文件为空!" << endl;
		this->fileIsEmpty = true;
		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> v;
		while (true) {
			pos = data.find(",", start);
			if (pos == -1) {
				break;//如果没找到就直接break出来
			}
			string tmp = data.substr(start, pos - start);
			v.push_back(tmp);
			start = pos + 1;
		}
		this->m_Record.insert(make_pair(index, v));
		index++;
	}
	ifs.close();

	/*for (map<int, vector<string>>::iterator it = m_Record.begin(); it != m_Record.end(); it++) {
		cout << it->first << "冠军编号:" << it->second[0] << "冠军得分:" << it->second[1] << endl;
	}*/
}

void SpeechManager::ShowRecord() {
	if (this->fileIsEmpty == true) {
		cout << "文件为空或者文件不存在!" << endl;
	}
	for (int i = 0; i < this->m_Record.size(); i++) {
		cout << "第" << i + 1 << "届"
			<< "冠军编号:" << this->m_Record[i][0] << "得分:" << this->m_Record[i][1] << " "
			<< "亚军编号:" << this->m_Record[i][2] << "得分:" << this->m_Record[i][3] << " "
			<< "季军编号:" << this->m_Record[i][4] << "得分:" << this->m_Record[i][5] << endl;
	}
	system("pause");
	system("cls");
}

void SpeechManager::ClearRecord() {
	cout << "是否确定清空文件?" << endl;
	cout << "1、是" << endl;
	cout << "2、否" << endl;

	int select = 0;
	cin >> select;
	if (select == 1) {
		//确认清空
		ofstream ofs(FileName, ios::trunc);//通过删掉重新打开的方式清空文件记录
		ofs.close();

		//重置所有参数
		this->InitSpeech();

		this->CreateSpeaker();

		this->LoadRecord();

		cout << "清空成功!" << endl;
	}
	system("pause");
	system("cls");
}

void SpeechManager::Exit_System() {
	cout << "欢迎下次使用!" << endl;
	system("pause");
	exit(0);//主要是这个退出函数
}
SpeechManager::~SpeechManager() {

}

********************************************************************************
//speechmanager.cpp
********************************************************************************
#include<iostream>
#include"speechManager.h"
#include<string>
#include<ctime>
using namespace std;

int main() {
	//随机数种子
	srand((unsigned int)time(NULL));
	SpeechManager sm;
	
	/*for (map<int, Speaker> ::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++) {
		cout << "选手编号:" << it->first << "\t姓名:" << it->second.m_Name << "分数:" << it->second.m_Score[0] << endl;
	}*/
	int choice = 0;//存储用户输入

	while (true) {
		sm.Show_Menu();
		cout << "请输入您的选择" << endl;
		cin >> choice;
		switch (choice) {
		case 1 ://开始比赛
			sm.StartSpeech();
			break;
		case 2 ://查看往届记录
			sm.ShowRecord();
			break;
		case 3 ://清屏
			sm.ClearRecord();
			break;
		case 0 ://退出
			sm.Exit_System();
			break;
		default:
			system("cls");
			break;
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值