面向对象编程练习(自己敲的代码,参考黑马c++课程)

#include <iostream>
#include <vector>
#include"speech_manage_sys.h"
#include <algorithm>
int main()
{
	while (1)
	{
		//菜单选择框架
		speech_manage_sys sms;
		sms.showmaue();
		int select;
		cout << "请输入您的选择" << endl;
		cin >> select;
		cout << "您的选择是:" << select << endl;
		/*cout << "     **************" << endl
			<< "  ***欢迎您参加比赛***" << endl;
		cout << " ****1.开始演讲比赛****                       *" << endl
			<< "*****2.查看往届记录******************************" << endl
			<< " ****3.清空比赛记录****                       *" << endl
			<< "  ***4.退出演讲比赛***" << endl;
		cout << "     **************" << endl;*/
		switch (select)
		{
		case 1:
			sms.start_speech();
			break;
		case 2:
			sms.show_record();
			break;
		case 3:
			sms.clear_record();
			break;
		case 4:
			cout << "退出系统,欢迎下次使用!" << endl;
			return 0;
			break;
		default:
			system("cls");//清屏功能
			cout << "您的选择是无效的!请您重新输入。" << select << endl;
			break;
		}
	}
	system("pause");
	return 0;
}
#pragma once
#include<iostream>
#include<vector>
#include<map>
#include"speaker.h"
using namespace std;

//演讲比赛管理
class speech_manage_sys {
public:
	vector<int> v1;//第一轮的speaker12个
	vector<int> v2;//第二轮的speaker6个
	vector<int> v_Victor;//胜出的speaker3个
	map<int, speaker> m_speaker;//编号和选手对应关系
	int m_index;//轮数
	speech_manage_sys();
	~speech_manage_sys();
	void showmaue();//显示菜单
	void init_speech();//初始化容器
	void create_speaker();//创建选手
	void start_speech();//开始比赛
	void draw_speech();//抽签
	void contest_speech();//比赛
	void showscore();//显示比赛结果
	void save_record();//保存比赛记录
	void read_record();//读取比赛记录
	map<int, vector<string>> msp;//用于记录往届获胜信息
	void show_record();//显示往届比赛记录
	void clear_record();//清空比赛记录
};


#include<iostream>
#include<algorithm>
#include"speech_manage_sys.h"
#include<vector>
#include<deque>
#include<numeric>
#include<functional>
#include<fstream>
#include<map>
using namespace std;
speech_manage_sys::speech_manage_sys()
{
	this->init_speech();//初始化容器
	create_speaker();//创建演讲者speaker
}
speech_manage_sys::~speech_manage_sys()
{

}
void speech_manage_sys::init_speech()
{
	v1.clear();//第一轮的speaker12个
	v2.clear();//第二轮的speaker6个
	v_Victor.clear();//胜出的speaker3个
	m_speaker.clear();//编号和选手对应关系
    m_index=1;//轮数
}
void speech_manage_sys::showmaue()
{
	cout<< "     **************" << endl
		<< "  ***欢迎您参加比赛***" <<endl;
	cout<< " ****1.开始演讲比赛****" << endl
		<< "*****2.查看往届记录*****" << endl
		<< " ****3.清空比赛记录****" << endl
		<< "  ***4.退出演讲比赛***" << endl;
	cout<< "     **************" << endl;
}
void speech_manage_sys::create_speaker()//创建选手
{
	//vector<int> v1;//第一轮的speaker12个编号
	//vector<int> v2;//第二轮的speaker6个编号
	//vector<int> v_Victor;//胜出的speaker3个编号
	//map<int, speaker> m_speaker;//编号和选手对应关系
	string nameseed = "ABCDEFGHIJKL";
	for (int i = 0; i < 12; i++)
	{

		string name = "选手";
		name += nameseed[i];
		speaker sp;
		sp.name = name;
		int speakerid = 10001 + i;
		v1.push_back(speakerid);
		m_speaker.insert(make_pair(speakerid, sp));
	}

}
//比赛
void  speech_manage_sys::contest_speech()
{
	cout << "第"<<this->m_index <<"轮比赛正式开始!" << endl;
	cout << endl;
	//准备临时容器,存放小组成绩
	multimap<double, int, greater<double>>  groupscore;
	int num=0;//人员个数

	vector<int> vec;//比赛人员容器
	if (this->m_index == 1)
	{
		vec = v1;
	}
	else
	{
		vec = v2;
	}
	for (vector<int> ::iterator it = vec.begin(); it != vec.end(); it++)//遍历,给每位选手打分
	{
		num++;
		//创建deque用于评委打分的存储
		deque<int> d_score;
		//打分
		for (int i = 0; i < 10; i++)
		{
			double score = (rand() % 401 + 600) / 10.f;//生成分数
			d_score.push_back(score);
		}
		//排序
		sort(d_score.begin(), d_score.end(),greater<double>());
		//去除最低分和最高分
		d_score.pop_back();
		d_score.pop_front();
		//求总分
		double total_score = accumulate(d_score.begin(), d_score.end(),0.0f);
		//求平均分
		double everage_score = total_score/(double)d_score.size();
		this->m_speaker[*it].secore[this->m_index - 1] = everage_score;
		//将打分的数据放入临时小组中
		groupscore.insert(make_pair(everage_score, *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].name 
					<< "分数:"<< this->m_speaker[it->second].secore[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
				{
					v_Victor.push_back((*it).second);
				}
			}

			groupscore.clear();
			cout << endl;
		}
	}
	cout << endl;
}
//用于遍历容器
void printspeak(int val)
{
	cout << val<<" ";
}
void speech_manage_sys::draw_speech()
{
	cout << "第" << this->m_index << "轮比赛的选手正在抽签" << endl;
	cout << "----------------------------------------------"  << endl;
	if (this->m_index == 1)
	{
	random_shuffle(v1.begin(),v1.end());
	cout << "抽签后的比赛次序为:" << "  ";
	for_each(v1.begin(), v1.end(),printspeak);
	}
	else
	{
	random_shuffle(v2.begin(), v2.end());
	for_each(v2.begin(), v2.end(), printspeak);
	}
	cout << endl;
	cout << "----------------------------------------------" << endl;
	cout << endl;
}
void  speech_manage_sys::showscore()//显示得分
{
	cout << "第" <<this->m_index<< "轮晋级的选手如下:" << endl;
	vector<int> v;
	if (this->m_index == 1)
	{
		v = this->v2;
	}
	else
	{
		v = this->v_Victor;
	}
	//遍历
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "选手编号为:" << *it << "选手姓名为:" << this->m_speaker[*it].name<< "选手得分为:" 
			<< this->m_speaker[*it].secore[this->m_index-1] << endl;
	}
	cout << endl;
	system("pause");
	system("cls");
	this->showmaue();
}
void speech_manage_sys::start_speech()
{
	//第一轮比赛*****************
	cout << "开始第一轮比赛" << endl;
	//抽签
	this->draw_speech();
	//比赛
	this->contest_speech();
	//显示晋级结果
	this->showscore();
	//第二轮比赛*****************
	this->m_index++;
	cout << "开始第二轮比赛" << endl;
	//抽签
	this->draw_speech();
	//比赛
	this->contest_speech();
	//显示获胜结果
	this->showscore();
	//保存分数
	this->save_record();
}
void speech_manage_sys::save_record()//保存比赛记录
{
	//文件操作
	//创建
	ofstream ofs;
	ofs.open("contest_record.csv", ios::out | ios::app);
	//写入数据
	for (vector<int>::iterator v_it = this->v_Victor.begin(); v_it != this->v_Victor.end(); v_it++)
	{
		ofs << *v_it << ","
			  << this->m_speaker[*v_it].secore[1] << ",";
	}
	ofs << endl;
	ofs.close();
}
void speech_manage_sys::read_record()//读取比赛记录
{
	ifstream ifs;
	ifs.open("contest_record.csv", ios::in);
	if (ifs.is_open())
	{
		//如何读取数据
		char ch;
		ifs >> ch;
		if (ifs.eof())
		{
			cout << "比赛记录文件为空!" << endl;
			ifs.close();
		}
		else
		{
			ifs.putback(ch);
			string data;
			int index = 0;
			while (ifs >> data)
			{
				vector<string> v;
				int pos = -1;
				int start = 0;
				while (1)
				{
					pos = data.find(",", start);
					if (pos == -1)
					{
						break;
					}
					string temp = data.substr(start, pos - start);
					v.push_back(temp);
					start = pos + 1;

				}
				this->msp.insert(make_pair(index, v));
				index++;

			}
			ifs.close();
		}
	}
	else
	{
		cout << "无比赛记录文件!" << endl;
		ifs.close();
	}
}
void speech_manage_sys::show_record()//显示往届比赛记录
{
	this->read_record();
	for (map<int, vector<string>>::iterator m_it = msp.begin(); m_it != msp.end(); m_it++)
	{
		cout << (*m_it).first <<" "
			<< (*m_it).second[0] <<" "<< (*m_it).second[1] << " " << (*m_it).second[2] << " "
			<<(*m_it).second[3] << " " << (*m_it).second[4] << " " << (*m_it).second[5] << " "
			<<endl;
	}
}
void speech_manage_sys::clear_record()//清空比赛记录
{
	//文件操作
//创建
	ofstream ofs;
	ofs.open("contest_record.csv", ios::out);
	//写入数
	ofs << " ";
	ofs.close();
}
#pragma once
#include <iostream>
#include <string>
using namespace std;

class speaker
{
public:
	string name;
	double secore[2];
	speaker();
	~speaker();
};
#include "speaker.h"
#include <iostream>

speaker::speaker()
{
	this->name = "*";
	secore[0] = secore[1] = 0;
}
speaker::~speaker()
{
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiZhi_BUAA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值