基于c++的演讲比赛案例部分代码

基于c++的一个演讲比赛的案例(不完整,只实现部分功能,有能力者可以完善一下,不胜感激),由于#include<>内的内容显示不出来,故在旁边列出,大家可以自行添加进括号内。
Speak.h头文件内容:
#pragma once
#include<>iostream
using namespace std;
class Speaker
{
public:
string name;
double score[2];
};
speechManger.h头文件内容:
#pragma once
#include<>iostream
#include<>vector
#include<>map
#include<>deque
#include<>functional
#include<>numeric
#include"Speak.h"
#include<>string
#include<>ctime
#include<>fstream
using namespace std;
class SpeechManger
{
public:
SpeechManger();
void showMenu();
void exitSystem();
~SpeechManger();
void initSpeech();
void creatSpeaker();
void startSpeech();
void speechDraw();
void speechContest();
void showScore();
void saveRecord();
void loadRecord();
bool fileIsEmpty;
map<int, vector>record;
vectorv1;
vectorv2;
vectorv3;
map<int, Speaker>speaker;
int index;
};
speechManger.cpp文件内容:
#include"speechManger.h"
#include<>vector
#include<>algorithm
SpeechManger::SpeechManger()
{
this->initSpeech();
this->creatSpeaker();
this->loadRecord();

}
void SpeechManger::showMenu()
{
cout << “*************************" << endl;
cout << "
欢迎参加演讲比赛 " << endl;
cout << "
1.开始演讲比赛 " << endl;
cout << "
2.查看往届成绩 " << endl;
cout << "
3.清空比赛记录 " << endl;
cout << "
4.退出比赛程序 " << endl;
cout << "
” << endl;
cout << endl;
}
void SpeechManger::exitSystem()
{
cout << “欢迎下次使用” << endl;
system(“pause”);
exit(0);
}
void SpeechManger::initSpeech()
{
this->v1.clear();
this->v2.clear();
this->v3.clear();
this->speaker.clear();
this->index = 1;
}
void SpeechManger::creatSpeaker()
{
string nameSeed = “ABCDEFGHIJKL”;
for (int i = 0; i < nameSeed.size(); i++)
{
string name = “选手”;
name += nameSeed[i];
Speaker speaker;
speaker.name = name;
for (int j = 0; j < 2; j++)
{
speaker.score[j] = 0;
}
this->v1.push_back(i + 10001);
this->speaker.insert(make_pair(i + 10001, speaker));

}

}
void SpeechManger::startSpeech()
{
this->speechDraw();
this->speechContest();
this->showScore();
this->index++;
this->speechDraw();
this->speechContest();
this->showScore();
this->saveRecord();
cout << “本届比赛结束” << endl;
system(“pause”);
system(“cls”);
}
void SpeechManger::speechDraw()
{
cout << “第” << this->index << “轮正在抽签” << endl;
cout << “--------------------------------------------” << endl;
cout << “抽签后顺序” << endl;
if (this->index == 1)
{
random_shuffle(v1.begin(), v1.end());
for (vector::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
else
{
random_shuffle(v2.begin(), v2.end());
for (vector::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
cout << “--------------------------------------------” << endl;
system(“pause”);
cout << endl;
}
void SpeechManger::speechContest()
{
cout << “-----------第” << this->index << “轮正式开始-----------” << endl;
multimap<double, int, greater>groupScore;
int num = 0;
vectorv_Src;
if (this->index == 1)
{
v_Src = v1;
}
else
{
v_Src = v2;
}
for (vector::iterator it = v_Src.begin(); it != v_Src.end(); it++)
{
//评委打分
num++;
dequed;
for (int i = 0; i < 10; i++)
{
double m_score = (rand() % 401 + 600)/10.f;
//cout << m_score << " ";
d.push_back(m_score);
}
//cout << endl;
sort(d.begin(), d.end(), greater());//排序
d.pop_back();
d.pop_front();
double sum = accumulate(d.begin(), d.end(), 0.0f);
double avg = sum / (double)d.size();
//打印平均分
//cout << *it << " " << this->speaker[*it].name << " " << avg << endl;
this->speaker[*it].score[this->index - 1] = avg;
groupScore.insert(make_pair(avg, *it));//对组
if (num % 6 == 0)
{
cout << “第” << num / 6 << “小组比赛名次:” << endl;
for (multimap<double, int, greater>::iterator it = groupScore.begin(); it != groupScore.end(); it++)
{
cout << it->second << " " << this->speaker[it->second].name << " " << this->speaker[it->second].score[this->index - 1] << endl;
}
//取走前三名
int count = 0;
for (multimap<double, int, greater>::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 SpeechManger::showScore()
{
cout << “------------第” << this->index << “轮晋级选手信息如下:------------” << endl;
vectorv;
if (this->index == 1)
{
v = v2;
}
else
{
v = v3;
}
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << “选手编号 " << *it <<” "<<"姓名 " << this->speaker[*it].name << " "<<"得分 " << this->speaker[*it].score[this->index - 1] << endl;

}
cout << endl;
system("pause");
system("cls");
this->showMenu();

}
void SpeechManger::saveRecord()
{
ofstream ofs;
ofs.open(“speech.csv”, ios::out | ios::app);
for (vector::iterator it = v3.begin(); it != v3.end(); it++)
{
ofs << *it << “,” << this->speaker[*it].score[1] << “,”;
}
cout << endl;
ofs.close();
cout << “记录已保存” << endl;
}
void SpeechManger::loadRecord()
{
ifstream ifs(“speech.csv”, ios::in);
if (!ifs.is_open())
{
this->fileIsEmpty = true;
cout << “文件不存在!” << endl;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << “文件为空” << endl;
this->fileIsEmpty = true;
ifs.close();
return;
}
this->fileIsEmpty = false;
ifs.putback(ch);
string data;
while (ifs >> data)
{
cout << data << endl;
}
ifs.close();
}
SpeechManger::~SpeechManger()
{
}
main.cpp主函数内容:
#include<>iostream
#include<>map
#include<>string
#include<>ctime
using namespace std;
#include"speechManger.h"

int main()
{

srand((unsigned int)time(NULL));
/*for (map<int, Speaker>::iterator it = sm.speaker.begin(); it != sm.speaker.end(); it++)
{
	cout << it->first << "  "<<it->second.name << "  " << it->second.score[0] << endl;
}*/
int count = 0;
while (true)
{
	SpeechManger sm;
	sm.showMenu();
	cout << "请输入您的选择" << endl;
	cin >> count;
	switch (count)
	{
	case 1:
		sm.startSpeech();
		break;
	case 2:
		//sm.loadRecord();
		break;
	case 3:
		break;
	case 4:
		sm.exitSystem();
		break;
	default:
		system("cls");
		break;
	}
}
system("pause");
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值