B站黑马c++学习笔记 —— 演讲比赛管理系统案例

1 比赛规则

在这里插入图片描述

2 系统功能

在这里插入图片描述

3 实现过程

3.1 speechManager.h

#pragma once
#include<iostream>
#include<vector>
#include<deque>
#include<map>
#include<algorithm>
#include<functional>
#include<numeric>
#include<ctime>
#include<string>
#include<fstream>
#include"speaker.h"
using namespace std;

class speechManager {
public:
	//构造函数
	speechManager();
	//菜单
	void show_menu();

	//初始化
	void initSpeech();
	//第一轮选手编号
	vector<int> v1;
	//第二轮选手编号
	vector<int> v2;
	//获胜者选手编号
	vector<int> v3;
	//编号及选手保存
	map<int, speaker> m;
	//比赛轮数
	int index;

	//创建选手
	void creatSpeaker();

	//开始比赛
	void startspeech();
	//抽签
	void speechchouqian();
	//比赛
	void speeching();
	//显示结果
	void showScore();
	//保存结果
	void saveScore();

	//读取记录
	void loadScore();
	//判断往届是否有数据
	bool fileIsEmpty;
	//往届记录
	map<int, vector<string>> Score;
	//显示往届记录
	void showAllScore();

	//清空记录
	void clearScore();

	//退出
	void exitSystem();

	//析构函数
	~speechManager();
};

3.2 speaker.h

#pragma once
#include<iostream>
using namespace std;

class speaker {
public:
	string m_name;
	double m_score[2];//两轮比赛
};

3.3 speechManager.cpp

#include"speechManager.h"

speechManager::speechManager() {
	this->initSpeech();
	this->creatSpeaker();
	this->loadScore();
};
//菜单
void speechManager::show_menu() {
	cout << "**********************" << endl;
	cout << "**********************" << endl;
	cout << "***演讲比赛管理系统***" << endl;
	cout << "*** 1 开始演讲比赛 ***" << endl;
	cout << "*** 2 往届比赛记录 ***" << endl;
	cout << "*** 3 清空比赛记录 ***" << endl;
	cout << "*** 4 退出管理系统 ***" << endl;
	cout << "**********************" << endl;
	cout << "**********************" << endl;
}

//初始化
void speechManager::initSpeech() {
	this->v1.clear();
	this->v2.clear();
	this->v3.clear();
	this->m.clear();
	this->index = 1;
}

//创建选手
void speechManager::creatSpeaker() {
	string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < nameSeed.size(); i++) {
		speaker sp;
		sp.m_name = nameSeed[i];

		sp.m_score[0] = 0;
		sp.m_score[1] = 0;

		this->v1.push_back(10001 + i);

		this->m.insert(make_pair(10001 + i, sp));
	}
}

//开始比赛
void speechManager::startspeech() {
	//第一轮
	//1、抽签
	this->speechchouqian();
	//2、比赛
	this->speeching();
	//3、结果
	this->showScore();
	//第二轮
	this->index++;
	//1、抽签
	this->speechchouqian();
	//2、比赛
	this->speeching();
	//3、结果
	this->showScore();
	//4、保存
	this->saveScore();
	cout << "本届比赛已结束" << endl;
	system("pause");
}

//抽签
void speechManager::speechchouqian() {
	cout << "--------------- 第" << index << "轮抽签结果如下 ---------------" << endl;
	if (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;
	}
	system("pause");
	cout << endl;
}

//比赛
void speechManager::speeching() {
	cout << "--------------- 第" << index << "轮比赛结果如下 ---------------" << endl;
	//比赛选手容器
	vector<int> v_src;
	srand((unsigned int)time(NULL));
	if (this->index == 1) {
		v_src = v1;
	}
	else {
		v_src = v2;
	}
	//start
	multimap<double, int,greater<double>> groupScore;//临时成绩容器
	int num = 0;//记录人数 六人一组
	for (vector<int>::iterator it = v_src.begin(); it != v_src.end(); it++) {
		//评委打分
		deque<double> d;
		for (int i = 0; i < 10; i++) {
			double score = (rand() % 401 + 600) / 10.f;
			d.push_back(score);
		}
		//排序
		sort(d.begin(), d.end(), greater<double>());
		d.pop_back();
		d.pop_front();
		//求平均分
		double sum = accumulate(d.begin(), d.end(), 0.0f);
		double avg = sum / (double)d.size();
		//将平均分放入容器
		this->m[*it].m_score[this->index - 1] = avg;
		groupScore.insert(make_pair(avg, *it));
		num++;
		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[it->second].m_name
				    << " 成绩:" << this->m[it->second].m_score[this->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->index == 1) {
					v2.push_back(it->second);
				}
				else {
					v3.push_back(it->second);
				}
			}
			groupScore.clear();//避免下次打印累加
		} 
	}
	cout << "--------------- 第" << this->index << "轮比赛结束 ---------------" << endl;
	system("pause");
}

//显示结果
void speechManager::showScore() {
	cout << "--------------- 第" << this->index << "轮晋级结果如下 ---------------" << endl;
	vector<int> v;
	if (this->index == 1) {
		v = v2;
	}
	else {
		v = v3;
	}
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "编号:" << *it
			<< " 姓名:" << this->m[*it].m_name
			<< " 成绩:" << this->m[*it].m_score[this->index-1] << endl;
	}
	system("pause");
	system("cls");
	this->show_menu();
}

//保存结果
void speechManager::saveScore() {
	ofstream ofs;
	ofs.open("speechScore.csv", ios::out | ios::app);//用追加的方式添加
	for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++) {
		ofs << *it << "," << this->m[*it].m_score[1] << ",";
	}
	ofs << endl;
	ofs.close();
	cout << "--------------- 成绩已保存 ---------------" << endl;
	this->fileIsEmpty = false;
}

//读取记录
void speechManager::loadScore() {
	ifstream ifs;
	ifs.open("speechScore.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;
	}
	//文件不为空
	this->fileIsEmpty = false;
	ifs.putback(ch);	//将上面读取的单个字符放回来
	string data;
	int s_index = 0;	//第几届
	while (ifs >> data) {
		vector<string> v;
		int pos = -1;
		int start = 0;
		while (true) {
			pos = data.find(",", start);
			if (pos == -1) {
				break;
			}
			//截取 start 到 pos 之间的字符
			string temp = data.substr(start, pos - start);
			v.push_back(temp);
			start = pos + 1;
		}
		//保存
		this->Score.insert(make_pair(s_index, v));
		s_index++;
	}
	ifs.close();
}

//显示往届记录
void speechManager::showAllScore() {
	if (this->fileIsEmpty) {
		cout << "文件为空或不存在" << endl;
		system("pause");
		return;
	}
	for (int i = 0; i < this->Score.size(); i++) {
		cout << "第" << i + 1 << "届:" << endl;
		cout << "冠军  编号:" << this->Score[i][0] << "\t得分:" << this->Score[i][1] << ""
			<< " 亚军  编号:" << this->Score[i][2] << "\t得分:" << this->Score[i][3] << ""
			<< " 季军  编号:" << this->Score[i][4] << "\t得分:" << this->Score[i][5] << endl << endl;
	}
	system("pause");
}

//清空记录
void speechManager::clearScore() {
	cout << "确认清空吗" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int choice = 0;
	cin >> choice;
	if (choice == 1) {
		ofstream ofs("speechScore.csv", ios::trunc);
		ofs.close();

		this->initSpeech();
		this->creatSpeaker();
		this->loadScore();
		cout << "清空成功" << endl;
		system("pause");
	}
}

//退出
void speechManager::exitSystem() {
	cout << "欢迎下次使用" << endl;
	exit(0);
}
speechManager::~speechManager() {};

3.4 演讲比赛管理系统.cpp

#include<iostream>
#include<algorithm>
#include<string>
#include"speechManager.h"

using namespace std;

int main() {
	while (true) {
		speechManager sm;

		sm.show_menu();

		int func;
		cout << "请选择功能:" << endl;
		cin >> func;
		switch (func)
		{
		case 1://开始比赛
			sm.startspeech();
			system("cls");
			break;

		case 2://往届记录
			sm.showAllScore();
			system("cls");
			break;

		case 3://清空记录
			sm.clearScore();
			system("cls");
			break;

		default://退出系统
			sm.exitSystem();
			break;
		}	
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值