提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
内容
`
一场比赛,,12人,,,比赛两轮,,,第一轮淘汰赛,,,,第二轮决赛
10001-------10012
分组比赛,,,每组六人
第一轮两个小组,抽签顺序演讲
十个评委打分,去掉最高分和最低分,求平均值,为本轮选手成绩
小组内演讲完后,淘汰最后三名,前三名晋级
第二名为决赛,前三名胜出
每组比赛完后,显示选手信息
一、功能
开始演讲比赛
查看往届记录
清空比赛记录
退出比赛程序
二、代码
1.头文件
#include<iostream>
#include<string>
#include<deque>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<random>
#include<fstream>
#define HUNDRED 100.00
#define FILENAME "speech competition.txt"
using namespace std;
2.类
class racer
{
public:
racer(){}
// 从外界读入属性,有参构造函数
racer(string name, int serial_number, float score)
{
// 读入
this->m_name = name;
this->m_serial_number = serial_number;
this->m_score = score;
}
// 成员属性
// 姓名
string m_name;
// 选手编号
int m_serial_number;
// 成绩
float m_score;
};
3.函数声明
// 升序排序函数
bool my_compare(const racer& r1, const racer& r2);
// 1、开始比赛函数
void begin_competition();
// 2、查看往届记录
void check_the_competition_rankings();
// 3、清空往届记录
void empty_record();
// 4、退出比赛函数
void exit_competition();
4.主函数入口
int main()
{
// 开始大循环
while (true)
{
// 创建菜单
cout << "=======================================" << endl;
cout << endl;
cout << " 1、开始演讲比赛" << endl;
cout << " 2、查看往届记录" << endl;
cout << " 3、清空比赛记录" << endl;
cout << " 4、退出比赛程序" << endl;
cout << endl;
cout << "=======================================" << endl;
// 初始化用户选择
int choice = 0;
// 提示用户输入
cout << "请输入您的选择:" << endl;
cin >> choice;
// 根据选择进入不同的选项
switch (choice)
{
case 1:
// 开始比赛
begin_competition();
break;
case 2:
// 查看往届记录
check_the_competition_rankings();
break;
case 3:
// 清空往届记录
empty_record();
break;
case 4:
exit_competition();
// 退出比赛函数
break;
default:
break;
}
}
return 0;
}
5.升序排序函数
bool my_compare(const racer& r1, const racer& r2)
{
// 按照分数降序
return r1.m_score > r2.m_score;
}
6.开始函数比赛(笨方法,很繁琐)
// 1、开始比赛函数
void begin_competition()
{
srand(time(0));
// 一次性把全部成绩生成完
// 一共12 x 10 = 120 个数,定义一个数组去装他们
float score_array[120] = {};
for (int j = 0; j < 120; j++)
{
score_array[j] = rand() % 4000 + 6000;
score_array[j] /= HUNDRED;
}
//default_random_engine e;
分布
//uniform_real_distribution<float> u(60.0, 100.0);
成绩分配
//e.seed(time(NULL));
测试
//for (deque<racer>::iterator it = d.begin(); it != d.end(); it++)
//{
// cout << "姓名:" << it->m_name << "\t\t"
// << "编号:" << it->m_serial_number << "\t\t"
// << "成绩:" << it->m_score << endl;
//}
// 初始化成绩
float score = 0.00;
// 初始容器
vector<racer>v;
// 打分1
//制备deque存放评委打分
deque<float>d1;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i];
// 装到容器之中
d1.push_back(score);
score = 0.0;
}
sort(d1.begin(), d1.end());
// 去掉最高最低
d1.pop_back();
d1.pop_front();
// 介入中间变量
float middle_score = 0;
for(deque<float>::iterator it1 = d1.begin(); it1 != d1.end(); it1++)
{
// 累加
middle_score += (*it1);
}
// 取平均值
float end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r1("A", 10001, end_score);
v.push_back(r1);
// 初始化成绩
// 打分2
//制备deque存放评委打分
deque<float>d2;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 10];
// 装到容器之中
d2.push_back(score);
score = 0.0;
}
sort(d2.begin(), d2.end());
// 去掉最高最低
d2.pop_back();
d2.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it2 = d2.begin(); it2 != d2.end(); it2++)
{
// 累加
middle_score += (*it2);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r2("B", 10002, end_score);
v.push_back(r2);
// 初始化成绩
// 打分3
//制备deque存放评委打分
deque<float>d3;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 20];
// 装到容器之中
d3.push_back(score);
score = 0.0;
}
sort(d3.begin(), d3.end());
// 去掉最高最低
d3.pop_back();
d3.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it3 = d3.begin(); it3 != d3.end(); it3++)
{
// 累加
middle_score += (*it3);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r3("C", 10003, end_score);
v.push_back(r3);
// 初始化成绩
// 打分4
//制备deque存放评委打分
deque<float>d4;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 30];
// 装到容器之中
d4.push_back(score);
score = 0.0;
}
sort(d4.begin(), d4.end());
// 去掉最高最低
d4.pop_back();
d4.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it4 = d4.begin(); it4 != d4.end(); it4++)
{
// 累加
middle_score += (*it4);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r4("D", 10004, end_score);
v.push_back(r4);
// 初始化成绩
// 打分5
//制备deque存放评委打分
deque<float>d5;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 40];
// 装到容器之中
d5.push_back(score);
score = 0.0;
}
sort(d5.begin(), d5.end());
// 去掉最高最低
d5.pop_back();
d5.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it5 = d5.begin(); it5 != d5.end(); it5++)
{
// 累加
middle_score += (*it5);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r5("E", 10005, end_score);
v.push_back(r5);
// 初始化成绩
// 打分6
//制备deque存放评委打分
deque<float>d6;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 50];
// 装到容器之中
d6.push_back(score);
score = 0.0;
}
sort(d6.begin(), d6.end());
// 去掉最高最低
d6.pop_back();
d6.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it6 = d6.begin(); it6 != d6.end(); it6++)
{
// 累加
middle_score += (*it6);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r6("F", 10006, end_score);
v.push_back(r6);
// 初始化成绩
// 打分7
//制备deque存放评委打分
deque<float>d7;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 60];
// 装到容器之中
d7.push_back(score);
score = 0.0;
}
sort(d7.begin(), d7.end());
// 去掉最高最低
d7.pop_back();
d7.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it7 = d7.begin(); it7 != d7.end(); it7++)
{
// 累加
middle_score += (*it7);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r7("G", 10007, end_score);
v.push_back(r7);
// 初始化成绩
// 打分8
//制备deque存放评委打分
deque<float>d8;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 70];
// 装到容器之中
d8.push_back(score);
score = 0.0;
}
sort(d8.begin(), d8.end());
// 去掉最高最低
d8.pop_back();
d8.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it8 = d8.begin(); it8 != d8.end(); it8++)
{
// 累加
middle_score += (*it8);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r8("H", 10008, end_score);
v.push_back(r8);
// 初始化成绩
// 打分9
//制备deque存放评委打分
deque<float>d9;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 80];
// 装到容器之中
d9.push_back(score);
score = 0.0;
}
sort(d9.begin(), d9.end());
// 去掉最高最低
d9.pop_back();
d9.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it9 = d9.begin(); it9 != d9.end(); it9++)
{
// 累加
middle_score += (*it9);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r9("I", 10009, end_score);
v.push_back(r9);
// 初始化成绩
// 打分10
//制备deque存放评委打分
deque<float>d10;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 90];
// 装到容器之中
d10.push_back(score);
score = 0.0;
}
sort(d10.begin(), d10.end());
// 去掉最高最低
d10.pop_back();
d10.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it10 = d10.begin(); it10 != d10.end(); it10++)
{
// 累加
middle_score += (*it10);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r10("J", 10010, end_score);
v.push_back(r10);
// 初始化成绩
// 打分11
//制备deque存放评委打分
deque<float>d11;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 100];
// 装到容器之中
d11.push_back(score);
score = 0.0;
}
sort(d11.begin(), d11.end());
// 去掉最高最低
d11.pop_back();
d11.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it11 = d11.begin(); it11 != d11.end(); it11++)
{
// 累加
middle_score += (*it11);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r11("K", 10011, end_score);
v.push_back(r11);
// 初始化成绩
// 打分12
//制备deque存放评委打分
deque<float>d12;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 110];
// 装到容器之中
d12.push_back(score);
score = 0.0;
}
sort(d12.begin(), d12.end());
// 去掉最高最低
d12.pop_back();
d12.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it12 = d12.begin(); it12 != d12.end(); it12++)
{
// 累加
middle_score += (*it12);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r12("L", 10012, end_score);
v.push_back(r12);
测试
//for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << "姓名:" << it->m_name << "\t\t"
// << "编号:" << it->m_serial_number << "\t\t"
// << "成绩:" << it->m_score << endl;
//}
/*for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_name << "\t\t"
<< "编号:" << it->m_serial_number << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << endl;
cout << endl;*/
system("cls");
cout << "第 << 1 >> 轮比赛选手正在抽签" << endl;
cout << "=====================================================" << endl;
// 打乱排序
random_shuffle(v.begin(), v.end());
cout << "抽签后演讲顺序如下:" << endl;
for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
{
cout << it->m_serial_number << "\t";
}
cout << endl;
cout << "=====================================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
cout << endl;
cout << "=======================第一轮比赛正式开始=================================" << endl;
/*for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_name << "\t\t"
<< "编号:" << it->m_serial_number << "\t\t"
<< "成绩:" << it->m_score << endl;
}*/
// 创建两个容器,分别存储分组
// 第一组
vector<racer>v1;
//第二组
vector<racer>v2;
// 先把原来的值赋值进来,
v1 = v;
v2 = v;
// v1 把后面的数删除了
v1.resize(6);
// v2 先倒过来在删除数字
reverse(v2.begin(), v2.end());
v2.resize(6);
// 分别根据分数降序排序
sort(v1.begin(), v1.end(), my_compare);
sort(v2.begin(), v2.end(), my_compare);
// 分别打印各自名次
cout << "第一小组比赛名次:" << endl;
for (vector<racer>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << endl;
cout << "第二小组比赛名次:" << endl;
for (vector<racer>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << endl;
cout << "=======================第一轮比赛结束=================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
// 宣布晋级信息
// 把v1后三名pass掉
v1.resize(3);
// 把v2后三名pass掉
v2.resize(3);
// 创建一个v3,把v1, v2合并到一起,并排序输出
vector<racer>v3;
预留空间
//v3.resize(v1.size() + v2.size());
v3.insert(v3.end(), v1.begin(), v1.end());
v3.insert(v3.end(), v2.begin(), v2.end());
sort(v3.begin(), v3.end(), my_compare);
cout << "==================第一轮晋级选手信息如下============================" << endl;
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
// 提示,请按任意键继续
system("pause");
cout << endl;
system("cls");
// 初始化
score = 0.0;
// 第二轮比赛重新随机六十个数
float new_score_array[60] = {};
for (int k = 0; k < 60; k++)
{
new_score_array[k] = rand() % 3000 + 6999;
new_score_array[k] /= HUNDRED;
}
// v3里面的人要重新赋值
deque<float>d20;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i];
// 装到容器之中
d20.push_back(score);
score = 0.0;
}
sort(d20.begin(), d20.end());
// 去掉最高最低
d20.pop_back();
d20.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it20 = d20.begin(); it20 != d20.end(); it20++)
{
// 累加
middle_score += (*it20);
}
// 取平均值
float end_score1 = 0;
end_score1 = middle_score / 8.0;
deque<float>d21;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 10];
// 装到容器之中
d21.push_back(score);
score = 0.0;
}
sort(d21.begin(), d21.end());
// 去掉最高最低
d21.pop_back();
d21.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it21 = d21.begin(); it21 != d21.end(); it21++)
{
// 累加
middle_score += (*it21);
}
// 取平均值
float end_score2 = 0;
end_score2 = middle_score / 8.0;
deque<float>d22;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 20];
// 装到容器之中
d22.push_back(score);
score = 0.0;
}
sort(d22.begin(), d22.end());
// 去掉最高最低
d22.pop_back();
d22.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it22 = d22.begin(); it22 != d22.end(); it22++)
{
// 累加
middle_score += (*it22);
}
// 取平均值
float end_score3 = 0;
end_score3 = middle_score / 8.0;
deque<float>d23;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 30];
// 装到容器之中
d23.push_back(score);
score = 0.0;
}
sort(d23.begin(), d23.end());
// 去掉最高最低
d23.pop_back();
d23.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it23 = d23.begin(); it23 != d23.end(); it23++)
{
// 累加
middle_score += (*it23);
}
// 取平均值
float end_score4 = 0;
end_score4 = middle_score / 8.0;
deque<float>d24;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 40];
// 装到容器之中
d24.push_back(score);
score = 0.0;
}
sort(d24.begin(), d24.end());
// 去掉最高最低
d24.pop_back();
d24.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it24 = d24.begin(); it24 != d24.end(); it24++)
{
// 累加
middle_score += (*it24);
}
// 取平均值
float end_score5 = 0;
end_score5 = middle_score / 8.0;
deque<float>d25;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 50];
// 装到容器之中
d25.push_back(score);
score = 0.0;
}
sort(d25.begin(), d25.end());
// 去掉最高最低
d25.pop_back();
d25.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it25 = d25.begin(); it25 != d25.end(); it25++)
{
// 累加
middle_score += (*it25);
}
// 取平均值
float end_score6 = 0;
end_score6 = middle_score / 8.0;
// 把全部平均数装到一起
float end_score_array[6] = {
end_score1,
end_score2,
end_score3,
end_score4,
end_score5,
end_score6
};
/*for (int i = 0; i < 6; i++)
{
cout << end_score_array[i] << endl;
}*/
int i = 0;
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
while (i < 6)
{
it->m_score = new_score_array[i];
i++;
break;
}
}
cout << "第 << 2 >> 轮比赛选手正在抽签" << endl;
cout << "=====================================================" << endl;
// 打乱排序
random_shuffle(v3.begin(), v3.end());
cout << "抽签后演讲顺序如下:" << endl;
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
cout << it->m_serial_number << "\t";
}
cout << endl;
cout << "=====================================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
cout << "=======================第二轮比赛正式开始=================================" << endl;
cout << "比赛名次:" << endl;
sort(v3.begin(), v3.end(), my_compare);
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << "=======================第二轮比赛结束=================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
// 创个新vector接受第一名,第二名,第三名
vector<racer>v4;
v4 = v3;
v4.resize(3);
cout << "==================第二轮晋级选手信息如下============================" << endl;
for (vector<racer>::iterator it = v4.begin(); it != v4.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
// 把获胜者存到文件中
fstream _FILE;
_FILE.open(FILENAME, ios::in);
if (!_FILE)
{
// 没有文本文档
ofstream ofs;
ofs.open(FILENAME, ios::out);
int first = 1;
ofs << "第" << first << "届比赛获奖者:" << " ";
for (vector<racer>::iterator it = v4.begin(); it != v4.end(); it++)
{
ofs << "*" << "姓名:" << it->m_name << " "
<< "编号:" << it->m_serial_number << " "
<< "得分:" << it->m_score << " ";
}
ofs << endl;
ofs.close();
}
else
{
// 有文本文档
// 先统计原有文件行数
int n_lines = 0;
ifstream ifs;
int n = 0;
string tmp;
ifs.open(FILENAME, ios::in);
{
while (getline(ifs, tmp))
{
if (tmp == "")
{
continue;
}
n++;
}
}
ofstream ofs;
ofs.open(FILENAME, ios::out|ios::app);
ofs << "第" << n + 1 << "届比赛获奖者:"<<" ";
for (vector<racer>::iterator it = v4.begin(); it != v4.end(); it++)
{
ofs << "*" << "姓名:" << it->m_name << " "
<< "编号:" << it->m_serial_number << " "
<< "得分:" << it->m_score << " ";
}
ofs << endl;
ofs.close();
}
_FILE.close();
// 提示,请按任意键继续
system("pause");
cout << endl;
system("cls");
}
7.查看往届记录
void check_the_competition_rankings()
{
// 看看文件是否存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "没有往届记录!" << endl;
return;
}
else
{
string result;
char buf[1024];
while (ifs.getline(buf, sizeof(buf)))
{
cout << buf << endl;
cout << endl;
}
}
system("pause");
system("cls");
}
8.清空往届记录
// 3、清空往届记录
void empty_record()
{
cout << "确认是否清空往届记录?" << endl;
cout << "1、确定" << endl;
cout << "2、否认" << endl;
// 初始化用户选择
int choice = 0;
// 提示用户输入
cout << "请输入您的选择:" << endl;
cin >> choice;
// 根据判断进行下一步
if (choice == 1)
{
// 看看文件是否存在
string file_name = FILENAME;
ofstream file_writer(file_name, ios_base::out);
cout << "清空成功!" << endl;
// 清屏操作
system("pause");
system("cls");
}
else
{
// 清屏操作
system("pause");
system("cls");
return;
}
}
9.退出
// 退出比赛函数
void exit_competition()
{
// 询问是否退出
cout << "确认推迟吗?" << endl;
cout << "1、确认" << endl;
cout << "2、不退出" << endl;
// 初始化用户选择
int choice = 0;
// 提示用户输入
cout << "请输入您的选择:" << endl;
cin >> choice;
// 根据判断进行下一步
if (choice == 1)
{
cout << "‘欢迎下次使用,再见!" << endl;
system("pause");
exit(0);
}
else
{
// 清屏操作
system("pause");
system("cls");
return;
}
}
三、全部代码
/*
一场比赛,,12人,,,比赛两轮,,,第一轮淘汰赛,,,,第二轮决赛
10001-------10012
分组比赛,,,每组六人
第一轮两个小组,抽签顺序演讲
十个评委打分,去掉最高分和最低分,求平均值,为本轮选手成绩
小组内演讲完后,淘汰最后三名,前三名晋级
第二名为决赛,前三名胜出
每组比赛完后,显示选手信息
开始演讲比赛
查看往届记录
清空比赛记录
退出比赛程序
*/
#include<iostream>
#include<string>
#include<deque>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<random>
#include<fstream>
#define HUNDRED 100.00
#define FILENAME "speech competition.txt"
using namespace std;
class racer
{
public:
racer(){}
// 从外界读入属性,有参构造函数
racer(string name, int serial_number, float score)
{
// 读入
this->m_name = name;
this->m_serial_number = serial_number;
this->m_score = score;
}
// 成员属性
// 姓名
string m_name;
// 选手编号
int m_serial_number;
// 成绩
float m_score;
};
// 升序排序函数
bool my_compare(const racer& r1, const racer& r2);
// 1、开始比赛函数
void begin_competition();
// 2、查看往届记录
void check_the_competition_rankings();
// 3、清空往届记录
void empty_record();
// 4、退出比赛函数
void exit_competition();
int main()
{
// 开始大循环
while (true)
{
// 创建菜单
cout << "=======================================" << endl;
cout << endl;
cout << " 1、开始演讲比赛" << endl;
cout << " 2、查看往届记录" << endl;
cout << " 3、清空比赛记录" << endl;
cout << " 4、退出比赛程序" << endl;
cout << endl;
cout << "=======================================" << endl;
// 初始化用户选择
int choice = 0;
// 提示用户输入
cout << "请输入您的选择:" << endl;
cin >> choice;
// 根据选择进入不同的选项
switch (choice)
{
case 1:
// 开始比赛
begin_competition();
break;
case 2:
// 查看往届记录
check_the_competition_rankings();
break;
case 3:
// 清空往届记录
empty_record();
break;
case 4:
exit_competition();
// 退出比赛函数
break;
default:
break;
}
}
return 0;
}
打分
//void setScore(vector<racer>& v)
//{
// for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
// {
// // 制备deque存放评委打分
// deque<double>d;
//
// d.push_back(score);
// }
//
// cout << "姓名:" << it->m_name << "\t" << "分数:" << endl;
// for (deque<double>::iterator dit = d.begin(); dit != d.end(); dit++)
// {
// cout << *dit << "\t";
// }
// cout << endl;
//
// sort(d.begin(), d.end());
// // 去掉最高最低
// d.pop_back();
// d.pop_front();
//
//
// }
//
//
//}
bool my_compare(const racer& r1, const racer& r2)
{
// 按照分数降序
return r1.m_score > r2.m_score;
}
// 1、开始比赛函数
void begin_competition()
{
srand(time(0));
// 一次性把全部成绩生成完
// 一共12 x 10 = 120 个数,定义一个数组去装他们
float score_array[120] = {};
for (int j = 0; j < 120; j++)
{
score_array[j] = rand() % 4000 + 6000;
score_array[j] /= HUNDRED;
}
//default_random_engine e;
分布
//uniform_real_distribution<float> u(60.0, 100.0);
成绩分配
//e.seed(time(NULL));
测试
//for (deque<racer>::iterator it = d.begin(); it != d.end(); it++)
//{
// cout << "姓名:" << it->m_name << "\t\t"
// << "编号:" << it->m_serial_number << "\t\t"
// << "成绩:" << it->m_score << endl;
//}
// 初始化成绩
float score = 0.00;
// 初始容器
vector<racer>v;
// 打分1
//制备deque存放评委打分
deque<float>d1;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i];
// 装到容器之中
d1.push_back(score);
score = 0.0;
}
sort(d1.begin(), d1.end());
// 去掉最高最低
d1.pop_back();
d1.pop_front();
// 介入中间变量
float middle_score = 0;
for(deque<float>::iterator it1 = d1.begin(); it1 != d1.end(); it1++)
{
// 累加
middle_score += (*it1);
}
// 取平均值
float end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r1("A", 10001, end_score);
v.push_back(r1);
// 初始化成绩
// 打分2
//制备deque存放评委打分
deque<float>d2;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 10];
// 装到容器之中
d2.push_back(score);
score = 0.0;
}
sort(d2.begin(), d2.end());
// 去掉最高最低
d2.pop_back();
d2.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it2 = d2.begin(); it2 != d2.end(); it2++)
{
// 累加
middle_score += (*it2);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r2("B", 10002, end_score);
v.push_back(r2);
// 初始化成绩
// 打分3
//制备deque存放评委打分
deque<float>d3;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 20];
// 装到容器之中
d3.push_back(score);
score = 0.0;
}
sort(d3.begin(), d3.end());
// 去掉最高最低
d3.pop_back();
d3.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it3 = d3.begin(); it3 != d3.end(); it3++)
{
// 累加
middle_score += (*it3);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r3("C", 10003, end_score);
v.push_back(r3);
// 初始化成绩
// 打分4
//制备deque存放评委打分
deque<float>d4;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 30];
// 装到容器之中
d4.push_back(score);
score = 0.0;
}
sort(d4.begin(), d4.end());
// 去掉最高最低
d4.pop_back();
d4.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it4 = d4.begin(); it4 != d4.end(); it4++)
{
// 累加
middle_score += (*it4);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r4("D", 10004, end_score);
v.push_back(r4);
// 初始化成绩
// 打分5
//制备deque存放评委打分
deque<float>d5;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 40];
// 装到容器之中
d5.push_back(score);
score = 0.0;
}
sort(d5.begin(), d5.end());
// 去掉最高最低
d5.pop_back();
d5.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it5 = d5.begin(); it5 != d5.end(); it5++)
{
// 累加
middle_score += (*it5);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r5("E", 10005, end_score);
v.push_back(r5);
// 初始化成绩
// 打分6
//制备deque存放评委打分
deque<float>d6;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 50];
// 装到容器之中
d6.push_back(score);
score = 0.0;
}
sort(d6.begin(), d6.end());
// 去掉最高最低
d6.pop_back();
d6.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it6 = d6.begin(); it6 != d6.end(); it6++)
{
// 累加
middle_score += (*it6);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r6("F", 10006, end_score);
v.push_back(r6);
// 初始化成绩
// 打分7
//制备deque存放评委打分
deque<float>d7;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 60];
// 装到容器之中
d7.push_back(score);
score = 0.0;
}
sort(d7.begin(), d7.end());
// 去掉最高最低
d7.pop_back();
d7.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it7 = d7.begin(); it7 != d7.end(); it7++)
{
// 累加
middle_score += (*it7);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r7("G", 10007, end_score);
v.push_back(r7);
// 初始化成绩
// 打分8
//制备deque存放评委打分
deque<float>d8;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 70];
// 装到容器之中
d8.push_back(score);
score = 0.0;
}
sort(d8.begin(), d8.end());
// 去掉最高最低
d8.pop_back();
d8.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it8 = d8.begin(); it8 != d8.end(); it8++)
{
// 累加
middle_score += (*it8);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r8("H", 10008, end_score);
v.push_back(r8);
// 初始化成绩
// 打分9
//制备deque存放评委打分
deque<float>d9;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 80];
// 装到容器之中
d9.push_back(score);
score = 0.0;
}
sort(d9.begin(), d9.end());
// 去掉最高最低
d9.pop_back();
d9.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it9 = d9.begin(); it9 != d9.end(); it9++)
{
// 累加
middle_score += (*it9);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r9("I", 10009, end_score);
v.push_back(r9);
// 初始化成绩
// 打分10
//制备deque存放评委打分
deque<float>d10;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 90];
// 装到容器之中
d10.push_back(score);
score = 0.0;
}
sort(d10.begin(), d10.end());
// 去掉最高最低
d10.pop_back();
d10.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it10 = d10.begin(); it10 != d10.end(); it10++)
{
// 累加
middle_score += (*it10);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r10("J", 10010, end_score);
v.push_back(r10);
// 初始化成绩
// 打分11
//制备deque存放评委打分
deque<float>d11;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 100];
// 装到容器之中
d11.push_back(score);
score = 0.0;
}
sort(d11.begin(), d11.end());
// 去掉最高最低
d11.pop_back();
d11.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it11 = d11.begin(); it11 != d11.end(); it11++)
{
// 累加
middle_score += (*it11);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r11("K", 10011, end_score);
v.push_back(r11);
// 初始化成绩
// 打分12
//制备deque存放评委打分
deque<float>d12;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = score_array[i + 110];
// 装到容器之中
d12.push_back(score);
score = 0.0;
}
sort(d12.begin(), d12.end());
// 去掉最高最低
d12.pop_back();
d12.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it12 = d12.begin(); it12 != d12.end(); it12++)
{
// 累加
middle_score += (*it12);
}
// 取平均值
end_score = 0;
end_score = middle_score / 8.0;
// 创建对象
racer r12("L", 10012, end_score);
v.push_back(r12);
测试
//for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << "姓名:" << it->m_name << "\t\t"
// << "编号:" << it->m_serial_number << "\t\t"
// << "成绩:" << it->m_score << endl;
//}
/*for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_name << "\t\t"
<< "编号:" << it->m_serial_number << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << endl;
cout << endl;*/
system("cls");
cout << "第 << 1 >> 轮比赛选手正在抽签" << endl;
cout << "=====================================================" << endl;
// 打乱排序
random_shuffle(v.begin(), v.end());
cout << "抽签后演讲顺序如下:" << endl;
for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
{
cout << it->m_serial_number << "\t";
}
cout << endl;
cout << "=====================================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
cout << endl;
cout << "=======================第一轮比赛正式开始=================================" << endl;
/*for (vector<racer>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_name << "\t\t"
<< "编号:" << it->m_serial_number << "\t\t"
<< "成绩:" << it->m_score << endl;
}*/
// 创建两个容器,分别存储分组
// 第一组
vector<racer>v1;
//第二组
vector<racer>v2;
// 先把原来的值赋值进来,
v1 = v;
v2 = v;
// v1 把后面的数删除了
v1.resize(6);
// v2 先倒过来在删除数字
reverse(v2.begin(), v2.end());
v2.resize(6);
// 分别根据分数降序排序
sort(v1.begin(), v1.end(), my_compare);
sort(v2.begin(), v2.end(), my_compare);
// 分别打印各自名次
cout << "第一小组比赛名次:" << endl;
for (vector<racer>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << endl;
cout << "第二小组比赛名次:" << endl;
for (vector<racer>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << endl;
cout << "=======================第一轮比赛结束=================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
// 宣布晋级信息
// 把v1后三名pass掉
v1.resize(3);
// 把v2后三名pass掉
v2.resize(3);
// 创建一个v3,把v1, v2合并到一起,并排序输出
vector<racer>v3;
预留空间
//v3.resize(v1.size() + v2.size());
v3.insert(v3.end(), v1.begin(), v1.end());
v3.insert(v3.end(), v2.begin(), v2.end());
sort(v3.begin(), v3.end(), my_compare);
cout << "==================第一轮晋级选手信息如下============================" << endl;
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
// 提示,请按任意键继续
system("pause");
cout << endl;
system("cls");
// 初始化
score = 0.0;
// 第二轮比赛重新随机六十个数
float new_score_array[60] = {};
for (int k = 0; k < 60; k++)
{
new_score_array[k] = rand() % 3000 + 6999;
new_score_array[k] /= HUNDRED;
}
// v3里面的人要重新赋值
deque<float>d20;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i];
// 装到容器之中
d20.push_back(score);
score = 0.0;
}
sort(d20.begin(), d20.end());
// 去掉最高最低
d20.pop_back();
d20.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it20 = d20.begin(); it20 != d20.end(); it20++)
{
// 累加
middle_score += (*it20);
}
// 取平均值
float end_score1 = 0;
end_score1 = middle_score / 8.0;
deque<float>d21;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 10];
// 装到容器之中
d21.push_back(score);
score = 0.0;
}
sort(d21.begin(), d21.end());
// 去掉最高最低
d21.pop_back();
d21.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it21 = d21.begin(); it21 != d21.end(); it21++)
{
// 累加
middle_score += (*it21);
}
// 取平均值
float end_score2 = 0;
end_score2 = middle_score / 8.0;
deque<float>d22;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 20];
// 装到容器之中
d22.push_back(score);
score = 0.0;
}
sort(d22.begin(), d22.end());
// 去掉最高最低
d22.pop_back();
d22.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it22 = d22.begin(); it22 != d22.end(); it22++)
{
// 累加
middle_score += (*it22);
}
// 取平均值
float end_score3 = 0;
end_score3 = middle_score / 8.0;
deque<float>d23;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 30];
// 装到容器之中
d23.push_back(score);
score = 0.0;
}
sort(d23.begin(), d23.end());
// 去掉最高最低
d23.pop_back();
d23.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it23 = d23.begin(); it23 != d23.end(); it23++)
{
// 累加
middle_score += (*it23);
}
// 取平均值
float end_score4 = 0;
end_score4 = middle_score / 8.0;
deque<float>d24;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 40];
// 装到容器之中
d24.push_back(score);
score = 0.0;
}
sort(d24.begin(), d24.end());
// 去掉最高最低
d24.pop_back();
d24.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it24 = d24.begin(); it24 != d24.end(); it24++)
{
// 累加
middle_score += (*it24);
}
// 取平均值
float end_score5 = 0;
end_score5 = middle_score / 8.0;
deque<float>d25;
for (int i = 0; i < 10; i++)
{
// 把随机数依次装入
score = new_score_array[i + 50];
// 装到容器之中
d25.push_back(score);
score = 0.0;
}
sort(d25.begin(), d25.end());
// 去掉最高最低
d25.pop_back();
d25.pop_front();
// 介入中间变量
middle_score = 0;
for (deque<float>::iterator it25 = d25.begin(); it25 != d25.end(); it25++)
{
// 累加
middle_score += (*it25);
}
// 取平均值
float end_score6 = 0;
end_score6 = middle_score / 8.0;
// 把全部平均数装到一起
float end_score_array[6] = {
end_score1,
end_score2,
end_score3,
end_score4,
end_score5,
end_score6
};
/*for (int i = 0; i < 6; i++)
{
cout << end_score_array[i] << endl;
}*/
int i = 0;
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
while (i < 6)
{
it->m_score = new_score_array[i];
i++;
break;
}
}
cout << "第 << 2 >> 轮比赛选手正在抽签" << endl;
cout << "=====================================================" << endl;
// 打乱排序
random_shuffle(v3.begin(), v3.end());
cout << "抽签后演讲顺序如下:" << endl;
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
cout << it->m_serial_number << "\t";
}
cout << endl;
cout << "=====================================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
cout << "=======================第二轮比赛正式开始=================================" << endl;
cout << "比赛名次:" << endl;
sort(v3.begin(), v3.end(), my_compare);
for (vector<racer>::iterator it = v3.begin(); it != v3.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
cout << "=======================第二轮比赛结束=================================" << endl;
// 提示,请按任意键继续
system("pause");
cout << endl;
// 创个新vector接受第一名,第二名,第三名
vector<racer>v4;
v4 = v3;
v4.resize(3);
cout << "==================第二轮晋级选手信息如下============================" << endl;
for (vector<racer>::iterator it = v4.begin(); it != v4.end(); it++)
{
cout << "编号:" << it->m_serial_number << "\t\t"
<< "姓名:" << it->m_name << "\t\t"
<< "成绩:" << it->m_score << endl;
}
// 把获胜者存到文件中
fstream _FILE;
_FILE.open(FILENAME, ios::in);
if (!_FILE)
{
// 没有文本文档
ofstream ofs;
ofs.open(FILENAME, ios::out);
int first = 1;
ofs << "第" << first << "届比赛获奖者:" << " ";
for (vector<racer>::iterator it = v4.begin(); it != v4.end(); it++)
{
ofs << "*" << "姓名:" << it->m_name << " "
<< "编号:" << it->m_serial_number << " "
<< "得分:" << it->m_score << " ";
}
ofs << endl;
ofs.close();
}
else
{
// 有文本文档
// 先统计原有文件行数
int n_lines = 0;
ifstream ifs;
int n = 0;
string tmp;
ifs.open(FILENAME, ios::in);
{
while (getline(ifs, tmp))
{
if (tmp == "")
{
continue;
}
n++;
}
}
ofstream ofs;
ofs.open(FILENAME, ios::out|ios::app);
ofs << "第" << n + 1 << "届比赛获奖者:"<<" ";
for (vector<racer>::iterator it = v4.begin(); it != v4.end(); it++)
{
ofs << "*" << "姓名:" << it->m_name << " "
<< "编号:" << it->m_serial_number << " "
<< "得分:" << it->m_score << " ";
}
ofs << endl;
ofs.close();
}
_FILE.close();
// 提示,请按任意键继续
system("pause");
cout << endl;
system("cls");
}
void check_the_competition_rankings()
{
// 看看文件是否存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "没有往届记录!" << endl;
return;
}
else
{
string result;
char buf[1024];
while (ifs.getline(buf, sizeof(buf)))
{
cout << buf << endl;
cout << endl;
}
}
system("pause");
system("cls");
}
// 3、清空往届记录
void empty_record()
{
cout << "确认是否清空往届记录?" << endl;
cout << "1、确定" << endl;
cout << "2、否认" << endl;
// 初始化用户选择
int choice = 0;
// 提示用户输入
cout << "请输入您的选择:" << endl;
cin >> choice;
// 根据判断进行下一步
if (choice == 1)
{
// 看看文件是否存在
string file_name = FILENAME;
ofstream file_writer(file_name, ios_base::out);
cout << "清空成功!" << endl;
// 清屏操作
system("pause");
system("cls");
}
else
{
// 清屏操作
system("pause");
system("cls");
return;
}
}
// 退出比赛函数
void exit_competition()
{
// 询问是否退出
cout << "确认推迟吗?" << endl;
cout << "1、确认" << endl;
cout << "2、不退出" << endl;
// 初始化用户选择
int choice = 0;
// 提示用户输入
cout << "请输入您的选择:" << endl;
cin >> choice;
// 根据判断进行下一步
if (choice == 1)
{
cout << "‘欢迎下次使用,再见!" << endl;
system("pause");
exit(0);
}
else
{
// 清屏操作
system("pause");
system("cls");
return;
}
}
四、效果预览