contest.h

#pragma once
#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <map>
#define PNUM 12
using namespace std;

class Person
{
	//Person(string name, double score)
	//{
	//	this->M_name = name;
	//	this->M_score = score;
	//}
public:
	int M_num;
	string M_name;
	double M_score;
};
//管理流程
class Contest
{
public:
	vector<Person>M_vp;
	int M_cnum;//记录比赛轮数
	map<int, vector<Person>> M_mp;
	int M_ses;
public:
	Contest();
	~Contest();
	//菜单
	void Showmenu();
	//开始
	void StartContest();


	//遍历容器
	void showvector();
};

contest.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "contest.h"

Contest::Contest()
{
	this->M_ses = 0;
}
Contest::~Contest()
{
}
void Contest::showvector()
{
	for (auto it = M_vp.begin(); it != M_vp.end(); it++)
		cout << "\t编号:" << (*it).M_num << "  姓名:" << (*it).M_name
		<< "  分数:" << (*it).M_score << endl;
}
void Contest::Showmenu()
{
	cout << endl;
	cout << "     欢迎参加 <第"<<M_ses+1<<"届> "<<"演讲比赛\t\t" << endl;
	cout << "\t  1 开始本届比赛\t" << endl;
	cout << "\t  2 往届比赛记录\t" << endl;
	cout << "\t  3 清空比赛记录\t" << endl;
	cout << endl;
	cout << "\t  0 退出比赛程序\t" << endl;
	cout << endl;
}

void initvector(Contest &contest)
{
	//初始化容器和比赛轮数
	contest.M_vp.clear();
	contest.M_cnum = 1;
}
void insertPerson(vector<Person> &v)
{
	string nneed = "ABCDEFJHIJKL";
	for (int i = 0; i < PNUM; i++)
	{
		Person p;
		p.M_num = i + 1001;
		p.M_name = "选手";
		p.M_name += nneed[i];
		p.M_score = 0;
		v.push_back(p);
	}
}
bool SortPred(Person p1, Person p2)
{
	return p1.M_score > p2.M_score;
}
void Group(Contest &contest)
{
	cout << "->"<<"第"<<contest.M_cnum<<"轮"<<"  抽签前:" << endl;
	contest.showvector();
	system("pause");
	//1.抽签
	random_shuffle(contest.M_vp.begin(), contest.M_vp.end());
	cout << "->" << "第" << contest.M_cnum << "轮" << "  抽签后:" << endl;
	contest.showvector();
	//2.打分
	for (auto it = contest.M_vp.begin(); it != contest.M_vp.end(); it++)
	{
		(*it).M_score = rand() % 20 +
			80 + rand() % 11 / 10.0;
	}
	system("pause");
	//3.排序
	sort(contest.M_vp.begin(), contest.M_vp.end(), SortPred);
	cout << "->第" << contest.M_cnum << "轮比赛成绩打印:" << endl;
	contest.showvector();
	system("pause");
	//4.淘汰最后一半
	contest.M_vp.erase(contest.M_vp.begin() + contest.M_vp.size() / 2, contest.M_vp.end());
}
void RandGroup(Contest &contest)
{
	while (true)
	{
		//第一轮
		if (1 == contest.M_cnum)
		{
			//重置分数
			for (auto it = contest.M_vp.begin(); it != contest.M_vp.end(); it++)
			{
				(*it).M_score = 0;
			}
			cout << "\t\t《第1轮比赛开始》" << endl;
			Group(contest);
			//5.打印晋级选手分数
			cout << "\t\t->以下选手晋级:" << endl;
			contest.showvector();
			contest.M_cnum++;
		}
		//第二轮
		else if (2 == contest.M_cnum)
		{
			//重置分数
			for (auto it = contest.M_vp.begin(); it != contest.M_vp.end(); it++)
			{
				(*it).M_score = 0;
			}
			cout << "\t\t《第2轮比赛开始》" << endl;
			Group(contest);
			cout << "\t************************" << endl;
			cout << "\t\t比赛结束" << endl;
			cout << "\t冠军  姓名:" << contest.M_vp.at(0).M_name
				<< "  成绩:" << contest.M_vp.at(0).M_score << endl;
			cout << "\t亚军  姓名:" << contest.M_vp.at(1).M_name
				<< "  成绩:" << contest.M_vp.at(1).M_score << endl;
			cout << "\t季军  姓名:" << contest.M_vp.at(2).M_name
				<< "  成绩:" << contest.M_vp.at(2).M_score << endl;
			cout << "\t************************" << endl;
			contest.M_cnum++;
			contest.M_ses++;
		}
		else
			return;
	}
}
void Contest::StartContest()
{
	//初始化
	initvector(*this);
	//插入比赛人员到map
	insertPerson(this->M_vp);
	//分组比赛开始
	RandGroup(*this);
}

file.h

#include "contest.h"

class File
{
public:
	bool WriteFile(vector<Person> &v,int ses);
	bool ReadFile(int &ses,map<int, vector<Person>> &m);
	void showmap(map<int, vector<Person>> &m);
	bool dorpFile(Contest &cons);
};

file.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include "file.h"

bool File::dorpFile(Contest &cons)
{
	cout << "输入 yes 清空记录:>";
	string input;
	cin >> input;
	if (input == "yes")
	{
		cons.M_mp.clear();
		ofstream ofs("data.csv", ios::out | ios::trunc);
		if (!ofs.is_open())
			return false;
		ofs << "";
		cons.M_ses = 0;
		cout << "\t->记录已清空" << endl;
		ofs.close();
	}
	else
		cout << "\t->操作取消!" << endl;
	return true;
}
bool File::WriteFile(vector<Person> &v,int ses)
{
	ofstream ofs("data.csv", ios::out | ios::app);
	if (!ofs.is_open())
		return false;
	ofs << "第" << ses << "届,";
	for (auto it = v.begin(); it != v.end(); it++)
	{
		ofs <<(*it).M_name << "," << (*it).M_score<<",";
	}
	ofs << endl;
	ofs.close();
	return true;
}
bool File::ReadFile(int &ses,map<int, vector<Person>> &m)
{
	ifstream ifs("data.csv", ios::in);
	if (!ifs.is_open())
		return false;
	string data;
	while (ifs >> data)
	{
		int start = 0;
		int pos = -1;
		int seb = data.find("第", 0); int see = data.find("届", 0);
		if (see < 1)
			return false;
		ses = stoi(data.substr(seb + 2, see));
		data = data.substr(see + 3, data.size() - 1);

		start = 0;
		vector<Person> v;
		while (true)
		{
			pos = data.find(",", start);
			if (pos == -1)
				break;
			Person p;
			p.M_name = data.substr(start, pos - start);
			start = pos + 1;

			pos = data.find(",", start);
			p.M_score = stold(data.substr(start, pos - start));
			v.push_back(p);
			start = pos + 1;
						if (pos == -1)
				break;
		}
		m.insert(make_pair(ses, v));
	}
	ifs.close();
	return true;
}

void File::showmap(map<int, vector<Person>> &m)
{
	if (m.empty())
	{
		cout << "往届记录为空!" << endl;
		return;
	}
	system("cls");
	cout << "往届纪录" << endl;
	for (auto it = m.begin(); it != m.end(); it++)
	{
		cout << "\t->第" << (*it).first << "届" << endl<<"\t  <冠军>姓名:" << (*it).second[0].M_name
			<< "  成绩:" << (*it).second[0].M_score << endl << " \t  <亚军>姓名:" << (*it).second[1].M_name
			<< "  成绩:" << (*it).second[1].M_score << endl << " \t  <季军>姓名:" << (*it).second[2].M_name
			<< "  成绩:" << (*it).second[2].M_score << endl;
	}
}

test.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include <ctime>
#include "contest.h"
#include "file.h"
int main()
{
	srand((unsigned int)time(NULL));
	int choose=1;
	Contest contest;
	File file;
	file.ReadFile(contest.M_ses, contest.M_mp);
	//contest.showvector();//打印测试
	while (choose)
	{
		contest.Showmenu();
		cout << "请选择:>";
		cin >> choose;
		cin.clear(); cin.ignore();
		switch (choose)
		{
		case 1://开始比赛
			contest.StartContest();
			file.WriteFile(contest.M_vp,contest.M_ses);
			system("pause");
			system("cls");
			break;
		case 2://往届记录
			file.ReadFile(contest.M_ses, contest.M_mp);
			file.showmap(contest.M_mp);
			system("pause");
			system("cls");
			break;
		case 3://清空记录
			file.dorpFile(contest);
			system("pause");
			system("cls");
			break;
		case 0://退出程序
			cout << "\t->欢迎下次使用!" << endl;
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}