speech contest

speaker.h

#pragma once
#include<iostream>

using namespace std;



class Speaker
{
public:
	string m_Name;
	double m_Score[2];
};

speechManager.h

#pragma once
#include<iostream>

using namespace std;
#include<vector>
#include<map>
#include"speaker.h"
#include<algorithm>
#include<deque>
#include<numeric>
#include<functional>
#include<fstream>

class SpeechManager
{
public:
	// construction
	SpeechManager();

	// show menu
	void showMenu();

	// init speech
	void initSpeech();

	// create speaker
	void createSpeaker();

	// start speech
	void startSpeech();

	// speech draw
	void speechDraw();

	// speech contest
	void speechContest();

	// show score
	void showScore();

	// save record
	void saveRecord();

	// load record
	void loadRecord();

	// show record
	void showRecord();

	// clear record
	void clearRecord();

	// file flag
	bool fileIsEmpty;
	// map
	map<int, vector<string>> m_Record;

	// exit system
	void exitProcedure();

	// deconstruction
	~SpeechManager();

	// member property

	// 12
	vector<int> v1;
	// 6
	vector<int> v2;
	// 3
	vector<int> vVictory;
	// map
	map<int,Speaker> mSpeaker;
	// count
	int m_Index;
};

main.cpp

#include<iostream>

using namespace std;
#include"speechManager.h"


int main()
{
	SpeechManager sm;

	 demo code
	//for (map<int,Speaker>::iterator it = sm.mSpeaker.begin(); it != sm.mSpeaker.end(); it++)
	//{
	//	cout << "id: " << it->first << " name: " << it->second.m_Name << " score: " << it->second.m_Score[0] << endl;
	//}

	int choice = 0;
	while (true)
	{
		sm.showMenu();
		cin >> choice;
		switch (choice)
		{
		case 1: // start a speech contest
			sm.startSpeech();
			break;
		case 2: // view record of contest
			sm.showRecord();
			break;
		case 3: // clear the contest record
			sm.clearRecord();
			break;
		case 0: // exit the speech procedure
			sm.exitProcedure();
			break;
		default:
			system("cls");
			break;
		}
	}
	
	system("pause");
	return 0;
}

speechManager.cpp

#include"speechManager.h"




// construction
SpeechManager::SpeechManager()
{
	// init 
	initSpeech();
	// create speaker
	createSpeaker();
	//load record
	loadRecord();
}


// show menu
void SpeechManager::showMenu()
{
	cout << "**********************************************************" << endl;
	cout << "***************welcom to the speech contest***************" << endl;
	cout << "***************1.start a speech contest    ***************" << endl;
	cout << "***************2.view record of contest    ***************" << endl;
	cout << "***************3.clear the contest record  ***************" << endl;
	cout << "***************0.exit the speech procedure ***************" << endl;
	cout << "**********************************************************" << endl;
}

// init speech
void SpeechManager::initSpeech()
{
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->mSpeaker.clear();
	this->m_Index=1;
}

// create speaker
void SpeechManager::createSpeaker()
{
	string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < nameSeed.size(); i++)
	{
		string name = "player_";
		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);
		this->mSpeaker.insert(make_pair(i + 10001, sp));
	}
}


// start speech
void SpeechManager::startSpeech()
{
	// round one
	// draw
	speechDraw();
	// contest
	speechContest();
	// score
	showScore();
	// round two
	m_Index++;
	// draw
	speechDraw();
	// contest
	speechContest();
	// score
	showScore();
	// save
	saveRecord();
	// reset the record
	// init 
	initSpeech();
	// create speaker
	createSpeaker();
	//load record
	loadRecord();
}

// speech draw
void SpeechManager::speechDraw()
{
	cout << "----round <<" << m_Index << ">> draw----" << endl;
	cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
	if (m_Index == 1)
	{
		cout << "draw result as follows" << endl;
		random_shuffle(v1.begin(), v1.end());
		for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}
	else
	{
		cout << "draw result as follows" << endl;
		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");
}


// speech contest
void SpeechManager::speechContest()
{
	srand((unsigned int)time(NULL));
	cout << "round " << m_Index << " contest" << endl;
	vector<int> vSrc;
	int num = 0;
	// temp container
	multimap<double,int,greater<double>> groupScore;
	if (m_Index == 1)
	{
		vSrc = v1;
	}
	else
	{
		vSrc = v2;
	}
	for (vector<int>::iterator it = vSrc.begin(); it != vSrc.end(); it++)
	{
		num++;
		// score
		deque<double> d;
		for (int i = 0; i < 10; i++)
		{
			double score = (double)(rand() % 401 + 600) / 10.f;
			//cout << score << " ";
			d.push_back(score);
		}
		//cout << endl;
		// sort
		sort(d.begin(), d.end(), greater<double>());

		// pop max score and min score
		d.pop_back();
		d.pop_front();

		// sum
		double sum = accumulate(d.begin(), d.end(), 0.0f);
		// avg
		double avg = sum / (double)d.size();
		groupScore.insert(make_pair(avg, *it));
		this->mSpeaker[*it].m_Score[m_Index-1] = avg;
		if (num % 6 == 0)
		{
			cout << "group "<< num/6<<" rank" << endl;
			for (multimap<double, int, greater<double>>::iterator mit = groupScore.begin(); mit != groupScore.end(); mit++)
			{
				cout << "id: " << mit->second << " name: " << this->mSpeaker[mit->second].m_Name << " score: " << this->mSpeaker[mit->second].m_Score[m_Index-1] << endl;
			}
			// take the top three
			int count = 0;
			for (multimap<double, int, greater<double>>::iterator mit = groupScore.begin(); count<3; mit++,count++)
			{
				if (m_Index == 1)
				{
					v2.push_back(mit->second);
				}
				else
				{
					vVictory.push_back(mit->second);
				}
			}
			groupScore.clear();
			cout << "round " << num / 6 << "contest over" << endl;
			cout << endl;
		}

	}
}


// show score
void SpeechManager::showScore()
{
	vector<int> v;
	if (m_Index == 1)
	{
		v = v2;
	}
	else
	{
		v = vVictory;
	}
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "round " << m_Index << " contest score" << endl;
	for (vector<int>::iterator it = v.begin(); it != v.end() ; it++)
	{
		cout << "id: " << *it << " name: " << this->mSpeaker[*it].m_Name <<"score: "<<
			this->mSpeaker[*it].m_Score[m_Index - 1] << endl;
	}
	system("pause");
}

// save record
void SpeechManager::saveRecord()
{
	ofstream ofs;
	ofs.open("speech.csv", ios::out | ios::app);

	for (vector<int>::iterator it = vVictory.begin(); it != vVictory.end(); it++)
	{
		ofs << *it << "," <<this->mSpeaker[*it].m_Score[1]<<",";
	}
	ofs << endl;
	ofs.close();
	this->fileIsEmpty = false;
	cout << "the speech contest is over" << endl;
	system("pause");
}


// load record
void SpeechManager::loadRecord()
{
	ifstream ifs;
	ifs.open("speech.csv", ios::in);
	if (!ifs.is_open())
	{
		//cout << "no such file" << endl;
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}
	char ch = {};
	ifs >> ch;
	if (ifs.eof())
	{
		//cout << "the file is null" << endl;
		this->fileIsEmpty = true;
		ifs.close();
		return;
	}

	this->fileIsEmpty = false;
	ifs.putback(ch);

	string data;
	int index = 0;
	while (ifs >> data)
	{
		vector<string> v;
		//cout << data << endl;
		int pos = -1;
		int start = 0;
		while (true)
		{
			pos = data.find(",", start);
			if (pos == -1)
			{
				break;
			}
			string temp = data.substr(start,pos-start);
			v.push_back(temp);
			start = pos + 1;
		}
		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 << "round: " << it->first + 1 << " champion: " << it->second[0] << " score: " << it->second[1] << endl;
	//}
}


// show record
void SpeechManager::showRecord()
{
	if (fileIsEmpty)
	{
		cout << "no such file or file  is null" << endl;
	}
	else
	{
		for (int i = 0; i < m_Record.size(); i++)
		{
			cout << "round: " << i + 1 << " champion    name: " << m_Record[i][0] << " score: " << m_Record[i][1] << " "
				<< "round: " << i + 1 << " runner-up   name: " << m_Record[i][2] << " score: " << m_Record[i][3] << " "
				<< "round: " << i + 1 << " third-place name: " << m_Record[i][4] << " score: " << m_Record[i][5] << endl;
		}
	}
	
	system("pause");
	system("cls");
}

// clear record
void SpeechManager::clearRecord()
{
	cout << "are you sure clear the file" << endl;
	cout << "1.yes" << endl;
	cout << "2.no" << endl;
	int select = 0;
	cin >> select;
	if (select == 1)
	{
		ofstream ofs;
		ofs.open("speech.csv", ios::trunc);
		ofs.close();
		cout << "clear successfully" << endl;
		// init 
		initSpeech();
		// create speaker
		createSpeaker();
		//load record
		loadRecord();
	}
	system("pause");
	system("cls");
}

// exit system
void SpeechManager::exitProcedure()
{
	cout << "welcom to back" << endl;
	system("pause");
	exit(0);
}

// deconstruction
SpeechManager::~SpeechManager()
{

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值