标题:
C++演讲比赛程序
[打印本页]
作者:
jackzhu123
时间:
2023-7-23 16:38
标题:
C++演讲比赛程序
#include<iostream>
using namespace std;
#include<vector>;
#include<map>
#include<string>
#include<algorithm>
#include<ctime>
#include<functional>
#include<deque>
#include<numeric>
#include<fstream>
#include<ctime>
void showMenu()
{
cout << "**********************************" << endl;
cout << "******* 欢迎参加演讲比赛 *******" << endl;
cout << "******* 1.开始演讲比赛 *******" << endl;
cout << "******* 2.查看往届记录 *******" << endl;
cout << "******* 3.清空比赛记录 *******" << endl;
cout << "******* 0.退出比赛记录 *******" << endl;
}
class Player
{
public:
Player()
{
}
Player(string name, double score)
{
this->name = name;
this->score = score;
}
string name;
double score;
};
void print(int a)
{
cout << a << " ";
}
class Jiang
{
public:
bool operator()(double dou1, double dou2)const
{
return dou1 > dou2;
}
};
class SpeechContest
{
public:
map<int, string>players;
vector<int>draw;
bool is_empty=true;
map<int, vector<string>>his_rec;
//创建比赛选手容器
void creatPlayers()
{
string st = "ABCDEFJHIJKL";
int number = 10001;
for (int i = 0; i < 12; i++)
{
string name = "选手";
name += st[i];
players.insert(make_pair(number + i, name));
draw.push_back(number + i);
}
}
//进行抽签
void drawLots()
{
random_shuffle(draw.begin(), draw.end());
}
//打印抽签后的演讲顺序
void print_DrawLots()
{
cout << "抽签后演讲顺序如下:" << endl;
for_each(draw.begin(), draw.end(), print);
cout << endl;
system("pause");
}
void print_group(multimap<double, int, Jiang>&group1)
{
for (multimap<double, int>::iterator it = group1.begin(); it != group1.end(); it++)
{
cout << it->second << " " << it->first << " " << players.find(it->second)->second << endl;
}
}
void delete_End(multimap<double, int, Jiang>& group1)
{
int i = 0;
do
{
multimap<double, int>::iterator it = group1.end();
--it;
group1.erase(it);
i++;
}while (i < 3);
}
void add_Final_Player(multimap<double, int, Jiang>& group1)
{
for (multimap<double, int>::iterator it = group1.begin(); it != group1.end(); it++)
{
draw.push_back(it->second);
}
}
void competition(multimap<double, int, Jiang>&group1,int a)
{
deque<double>de;
double score;
for (int i = a; i < 6+a; i++)
{
de.clear();
for(int j=0;j<10;j++)
{
score = rand() % 21 + 79 + rand() % 100 / 100.00;
de.push_back(score);
}
sort(de.begin(),de.end());
de.pop_back();
de.pop_front();
score=accumulate(de.begin(), de.end(),0)/8.00;
group1.insert(make_pair(score, draw[i]));
}
}
void first_Round()
{
multimap<double, int, Jiang>group1;
multimap<double, int, Jiang>group2;
drawLots();
print_DrawLots();
competition(group1,0);
cout << "第1小组比赛名次:" << endl;
print_group(group1);
competition(group2,6);
cout << "第2小组比赛名次:" << endl;
print_group(group2);
delete_End(group1);
delete_End(group2);
cout << "晋级人员:" << endl;
print_group(group1);
print_group(group2);
draw.clear();
add_Final_Player(group1);
add_Final_Player(group2);
cout << "第一轮比赛完毕" << endl;
}
void read()
{
ifstream ifs;
ifs.open("speech_result.csv", ios::in);
if (!ifs.is_open())
{
cout << "该文件不存在" << endl;
is_empty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "记录为空" << endl;
is_empty = true;
ifs.close();
return;
}
is_empty = false;
ifs.putback(ch);
vector<string>ve;
string date;
int index = 1;
while (ifs>>date)
{
ve.clear();
int start = 0;
int pos = -1;
while (true)
{
pos = date.find(',', start);
if (pos==-1)
{
break;
}
string st = date.substr(start, pos - start);
ve.push_back(st);
start = pos + 1;
}
his_rec.insert(make_pair(index, ve));
index++;
}
}
void store(multimap<double, int, Jiang>&group)
{
ofstream ofs;
ofs.open("speech_result.csv",ios::out|ios::app);
for(multimap<double, int, Jiang>::iterator it=group.begin();it!=group.end();it++)
{
ofs << it->second << "," << it->first << ",";
}
ofs << endl;
ofs.close();
cout << "记录已保存" << endl;
is_empty = false;
}
void second_Round()
{
multimap<double, int, Jiang>group;
cout << "第二轮比赛开始" << endl;
drawLots();
print_DrawLots();
competition(group, 0);
cout << "决赛名次:" << endl;
print_group(group);
delete_End(group);
cout << "冠亚季军:" << endl;
print_group(group);
store(group);
}
void del_rec()
{
fstream fs;
fs.open("speech_result.csv", ios::out);
fs.clear();
is_empty = true;
cout << "记录已清空" << endl;
}
};
void contest(SpeechContest &con)
{
con.creatPlayers();
con.first_Round();
con.second_Round();
}
void showTop(string st)
{
cout << st << "\t";
}
void showHis(const pair<int,vector<string>>&pa)
{
cout << "第" << pa.first << "届:";
for_each(pa.second.begin(), pa.second.end(), showTop);
cout << endl;
}
void hisRec(SpeechContest &con)
{
con.read();
if (con.is_empty)
{
return;
}
for_each(con.his_rec.begin(), con.his_rec.end(),showHis);
}
int main()
{
srand((unsigned int)time(NULL));
SpeechContest con;
while (true)
{
showMenu();
cout << "请输入您的选择:" << endl;
int sel;
cin >> sel;
switch (sel)
{
case 1:
contest(con);
system("pause");
system("cls");
break;
case 2:
hisRec(con);
system("pause");
system("cls");
break;
case 3:
con.del_rec();
system("pause");
system("cls");
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "您输入有误!" << endl;
system("pause");
system("cls");
}
}
system("pause");
return 0;
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1