演讲比赛程序需求
比赛规则
- 学校举行一场演讲比赛,共有12个人参加。比赛共两轮,第一轮为淘汰赛,第二轮为决赛。
- 比赛方式:分组比赛,每组6个人;选手每次要随机分组,进行比赛
- 每名选手都有对应的编号,如 10000 ~ 10011
- 第一轮分为两个小组,每组6个人。 整体按照选手编号进行抽签后顺序演讲。
- 当小组演讲完后,淘汰组内排名最后的三个选手,前三名晋级,进入下一轮的比赛。
- 第二轮为决赛,前三名胜出
- 每轮比赛过后需要显示晋级选手的信息
程序功能
- 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段
- 查看往届记录:查看之前比赛前三名结果,每次比赛都会记录到文件中,文件用.csv后缀名保存
- 清空比赛记录:将文件中数据清空
- 退出比赛程序:可以退出当前程序
#include <iostream>
using namespace std;
#include <string>
#include<algorithm>
#include<deque>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<vector>
#include <algorithm>
#include<fstream>
#define FILENAME "speech.txt"
vector<int> v3;//存放第一轮晋级的6个选手的编号
vector<int> v4;//存放第二轮晋级的3个选手的编号
int epoch = 1;//记录第几届比赛
class person
{
public:
string p_name = "选手";//姓名
int p_id;//编号
int score[2];//两轮分数
};
//保存到文件中
void save_file(vector<person>& v)
{
ofstream ofs;
ofs.open(FILENAME, ios::out|ios::app);//加ios::app在写入文件时不会覆盖原有内容
ofs << "第" << epoch << "届比赛成绩记录:" << endl;
for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
{
ofs << "姓名:" << it->p_name << " 编号:" << it->p_id << " 淘汰赛成绩:" << it->score[0] << " 决赛成绩:" << it->score[1] << endl;
}
ofs << "-——————————————————————" << endl;
epoch++;
ofs.close();
}
//显示菜单功能
void showmenu()
{
cout << "请选择你需要的功能:" << endl;
cout << "1、开始演讲比赛" << endl;
cout << "2、查看往届记录" << endl;
cout << "3、清空比赛记录" << endl;
cout << "4、退出比赛程序" << endl;
}
//显示元素
class myprint {
public:
void operator()(int val)
{
cout << val << " ";
}
};
//改变排序规则——由大到小
class MyCompare {
public:
bool operator()(int v1, int v2)const {
return v1 > v2;
}
};
//显示容器内数据
void allprint(vector<person>&v,int choice)
{
if (choice == 1)
{
for (vector<person>::iterator jt = v.begin(); jt != v.end(); jt++)
{
cout << "姓名:" << (*jt).p_name << " 编号:" << (*jt).p_id << " 第一轮成绩:" << (*jt).score[0] << endl;
}
}
if (choice == 2)
{
for (vector<person>::iterator jt = v.begin(); jt != v.end(); jt++)
{
cout << "姓名:" << (*jt).p_name << " 编号:" << (*jt).p_id << " 第一轮成绩:" << (*jt).score[0] << " 第二轮成绩:" << (*jt).score[1] << endl;
}
}
}
void creatmember(vector<person>&v)
{
v.resize(12);
string str = "ABCDEFGHIJKL";
int i = 0;
for (vector<person>::iterator it=v.begin();it!=v.end()&&i<12;it++)
{
(*it).p_name += str[i];
(*it).p_id = 10000 + i;// 10000 ~10011
i++;
}
}
//打分
//epoch是第几轮,size是一次性给几个选手打分
void rand_score(vector<person>&v,int size,int epoch)
{
srand((unsigned int)time(NULL));//随机数种子
vector<person>::iterator it;
for (int i = 0; i < size; i++,it++)
{
(*it).score[epoch] = rand() % 100 + 1;//分数在1-100
}
}
//第一轮比赛
void firstspee(vector<person>&v)
{
cout << "第一轮比赛正式开始!" << endl;
srand((unsigned int)time(NULL));//随机数种子
vector<int> v1;//存放12个选手编号
for (int i = 0; i < 12; i++)
{
v1.push_back(10000 + i);
}
//打乱顺序
random_shuffle(v1.begin(), v1.end());
cout << "淘汰赛抽签顺序为:" << endl;
for_each(v1.begin(), v1.end(), myprint());
cout << endl;
int group = 0;
multimap<int, person> mulm;//放第一组
multimap<int, person> mulm2;//放第二组
//进行打分,先让评委对十个人进行打分取得平均值,然后把分数赋值给每个人
for (vector<int>::iterator it=v1.begin(); it!=v1.end(); it++)
{
for (vector<person>::iterator jt = v.begin(); jt != v.end(); jt++)
{
if ((*jt).p_id == *it)
{
deque<int>v2;
for (int i = 0; i < 10; i++)
{
//十位评委打分
int k = rand() % 100 + 1;
v2.push_back(k);
}
sort(v2.begin(), v2.end());
v2.pop_back();//去掉最高分
v2.pop_front();//去掉最低分
int sum = 0;
for (int i = 0; i < v2.size(); i++)
{
sum += v2[i];
}
int ave = sum / v2.size();//平均分
(*jt).score[0] = ave;
//把信息放到可以根据分数自动排序的容器中
//分成两组
if (group / 6 == 0)//第一组
{
mulm.insert(pair<int, person>(ave, *jt));//将平均分和选手信息放上去
}
if (group / 6 == 1)//第二组
{
mulm2.insert(pair<int, person>(ave, *jt));//将平均分和选手信息放上去
}
group++;
}
}
}
cout << "所有人的成绩如下:" << endl;
allprint(v, 1);//显示第一轮成绩
//分组显示成绩
cout << "第一组的成绩为:" << endl;
for (multimap<int, person>::iterator it = mulm.begin(); it != mulm.end(); it++)
{
cout << "姓名" << it->second.p_name << " 编号:"<<it->second.p_id<<" 第一轮成绩:" <<it->first << endl;
}
cout << "-——————————————————————" << endl;
cout << "第二组的成绩为:" << endl;
for (multimap<int, person>::iterator it = mulm2.begin(); it != mulm2.end(); it++)
{
cout << "姓名" << it->second.p_name << " 编号:" << it->second.p_id << " 第一轮成绩:" << it->first << endl;
}
//淘汰三名选手
mulm.erase(mulm.begin());
mulm.erase(mulm.begin());
mulm.erase(mulm.begin());
mulm2.erase(mulm2.begin());
mulm2.erase(mulm2.begin());
mulm2.erase(mulm2.begin());
//显示晋级选手信息
cout << "-——————————————————————" << endl;
cout << "第一轮晋级选手信息如下:" << endl;
for (multimap<int, person>::iterator it = mulm.begin(); it != mulm.end(); it++)
{
cout << "姓名" << it->second.p_name << " 编号:" << it->second.p_id << " 第一轮成绩:" << it->first << endl;
v3.push_back(it->second.p_id);
}
for (multimap<int, person>::iterator it = mulm2.begin(); it != mulm2.end(); it++)
{
cout << "姓名" << it->second.p_name << " 编号:" << it->second.p_id << " 第一轮成绩:" << it->first << endl;
v3.push_back(it->second.p_id);
}
//return v3;
}
//第二轮比赛
void secondspee(vector<person>& v)
{
cout << "决赛正式开始!" << endl;
srand((unsigned int)time(NULL));//随机数种子
//打乱顺序
random_shuffle(v3.begin(), v3.end());
cout << "决赛抽签顺序为:" << endl;
for_each(v3.begin(), v3.end(), myprint());
cout << endl;
multimap<int, person,MyCompare> mulm;//存放选手所有信息的临时容器
for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
{
for (vector<person>::iterator jt = v.begin(); jt != v.end(); jt++)
{
if ((*jt).p_id == *it)
{
deque<int>v2;
for (int i = 0; i < 10; i++)
{
//十位评委打分
int k = rand() % 100 + 1;
v2.push_back(k);
}
sort(v2.begin(), v2.end());
v2.pop_back();//去掉最高分
v2.pop_front();//去掉最低分
int sum = 0;
for (int i = 0; i < v2.size(); i++)
{
sum += v2[i];
}
int ave = sum / v2.size();//平均分
(*jt).score[1] = ave;
mulm.insert(pair<int, person>(ave, *jt));//将平均分和选手信息放上去
}
}
}
//cout << "所有人的成绩如下:" << endl;
//allprint(v, 2);//显示所有成绩
save_file(v);
cout << "-——————————————————————" << endl;
for (multimap<int, person>::iterator it = mulm.begin(); it != mulm.end(); it++)
{
cout << "姓名" << it->second.p_name << " 编号:" << it->second.p_id << " 第二轮成绩:" << it->first << endl;
}
cout << "-——————————————————————" << endl;
cout << "决赛胜出选手信息如下:" << endl;
int num = 0;
for (multimap<int, person>::iterator it = mulm.begin(); it != mulm.end(); it++)
{
if(num<3)
cout << "姓名" << it->second.p_name << " 编号:" << it->second.p_id << " 第二轮成绩:" << it->first << endl;
num++;
}
}
//开始演讲比赛
void startspee(vector<person>& v)
{
cout << "比赛正式开始!" << endl;
firstspee(v);
system("pause");
system("cls");
secondspee(v);
system("pause");
system("cls");
}
//查看往届记录
void lookbefore()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
char buf[4000] = { 0 };
while (ifs.getline(buf, sizeof(buf)))
{
cout << buf << endl;
}
ifs.close();
}
//清空文件
void clear_file()
{
cout << "确认清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
ofstream ofs(FILENAME, ios::trunc);//打开模式 ios::trunc 如果存在删除文件并重新创建
ofs.close();
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
//退出比赛程序
void exit_pro()
{
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}
int main() {
int choice = 0;
while (1)
{vector<person>v;
creatmember(v);
showmenu();
cin >> choice;
switch (choice)
{
case 1:startspee(v);
break;
case 2:lookbefore();
break;
case 3:clear_file();
break;
case 4:exit_pro();
break;
default:
break;
}
}
system("pause");
return 0;
}
运行主界面如下:
保存的文件数据参考:
该代码存在一个小bug:
当我们下次再打开软件重新运行时得到的新纪录是从第一届开始算起的,所以应该再在代码里面加上每次运行代码都能找到文件中已经存在几届选手,并将这个届数反馈给代码中的届数