写代码时建议先画结构图(使程序结构化,方便修改),再思考算法,可以使得问题求解思路简单清晰
头文件
#pragma once
#include<iostream>
using namespace std;
#include<cstdlib>
#include<string>
#include<vector>
#include<ctime>
#include<algorithm>
#include<set>
#include<fstream>
//创建比赛人员
class Participant
{
public:
Participant()
{
this->score = 0;
}
bool operator!=(Participant& p)const
{
return this->name != p.name;
}
string name;
double score;
};
void interface();//界面
void startcontest();//开始比赛
void print(Participant& p);//打印信息
void GiveMark(vector<Participant>&v,int number);//评分
bool compare(Participant& p1, Participant& p2);//自定义降序
void save(vector<Participant>& v);//保存信息
void previous();//显示往届信息
void clear1();//清空文件信息
cpp文件
#include"演讲比赛管理系统.h"
void interface()//界面
{
cout << "******************演讲比赛管理系统***********************" << endl;
cout << "******************1.开始演讲比赛*************************" << endl;
cout << "******************2.查看往届比赛记录*********************" << endl;
cout << "******************3.清空比赛记录*************************" << endl;
cout << "******************4.退出比赛程序 ************************" << endl;
}
void print(Participant& p)//打印信息
{
cout << "姓名:" << p.name << " " << "分数:" << p.score << endl;
}
void startcontest()//开始比赛
{
//创建12个比赛人员
string name = "ABCDEFGHIJKL";
Participant p[12];
for (int i = 0; i < 12; i++)
{
p[i].name = "比赛人员";
p[i].name += name[i];
}
//创建两个容器把人员分两组 初赛组
Participant pp;
pp.name = "000";
vector<Participant>v1;
v1.resize(6, pp);//初始化容器
vector<Participant>v2;
v2.resize(6, pp);
srand((unsigned int)time(NULL));
//随机分组
for (int i = 0; i < 6; i++)
{
int temp = 0;
while (true)
{
temp = rand() % 12;
int num = 0;
for (vector<Participant>::iterator it = v1.begin(); it != v1.end(); it++)
{
if (*it != p[temp])
{
num++;
}
}
if (num == 6)
{
break;
}
}
v1[i] = p[temp];
}
for (int i = 0; i < 6; i++)
{
int temp = 0;
while (true)
{
temp = rand() % 12;
while (true)
{
int num = 0;
for (vector<Participant>::iterator it = v1.begin(); it != v1.end(); it++)
{
if (*it != p[temp])
{
num++;
}
}
if (num == 6)
{
break;
}
else
{
temp = rand() % 12;
}
}
int num2 = 0;
for (vector<Participant>::iterator it2 = v2.begin(); it2 != v2.end(); it2++)
{
if (p[temp] != *it2)
{
num2++;
}
}
if (num2 == 6)
{
break;
}
}
v2[i] = p[temp];
}
cout << "第一组人员信息为:" << endl;
for_each(v1.begin(), v1.end(), print);
cout << "第二组人员信息为:" << endl;
for_each(v2.begin(), v2.end(), print);
system("pause");
cout << "------------------------------------------------------" << endl;
cout << "第一组评分结果为:" << endl;
GiveMark(v1, 6);
for_each(v1.begin(), v1.end(), print);
cout << "第二组评分结果为:" << endl;
GiveMark(v2, 6);
for_each(v2.begin(), v2.end(), print);
system("pause");
cout << "------------------------------------------------------" << endl;
cout << "决赛组人员信息为:" << endl;
vector<Participant>vv;//决赛组 将个组的前三名进入决赛
for (int i = 0; i < 3; i++)
{
vv.push_back(v1[i]);
vv.push_back(v2[i]);
}
for_each(vv.begin(), vv.end(), print);
system("pause");
cout << "------------------------------------------------------" << endl;
cout << "决赛信息评分信息为:" << endl;
for (int i = 0; i < 6; i++)
{
vv[i].score = 0;
}
GiveMark(vv, 6);
for_each(vv.begin(), vv.end(), print);
cout << "------------------------------------------------------" << endl;
cout << "此次前三排名信息为:"<<endl;
string rank[3] = { "冠军","亚军","季军" };
for (int i = 0; i < 3; i++)
{
cout << rank[i] <<" " << "姓名: " << vv[i].name << " " << "分数:" << vv[i].score << endl;
}
cout << "比赛结束" << endl;
save(vv);
system("pause");
system("cls");
}
void GiveMark(vector<Participant>& v,int number)//评分
{
for (int i = 0; i < number; i++)
{
//在组内随机抽取参赛人员进行评分
int temp = 0;
while (true)
{
temp = rand() % 6;
if (v[temp].score == 0)
{
break;
}
}
//10个裁判评分
multiset<double>m;
for (int i = 0; i < 10; i++)
{
int tempnumber = rand() % 41 + 60;
m.insert(tempnumber);//60~100
}
//删除最大,最小值
m.erase(m.begin());
m.erase(--m.end());
double total = 0;//总分
for (multiset<double>::iterator it = m.begin(); it != m.end(); it++)
{
total = total + (*it);
}
v[temp].score = total/8;//取平均分
}
sort(v.begin(), v.end(), compare);//进行降序排序
}
bool compare(Participant& p1, Participant& p2)//自定义降序
{
return p1.score > p2.score;
}
void save(vector<Participant>& v)//保存信息
{
string rank[3] = { "冠军","亚军","季军" };
ofstream ofs;
ofs.open("演讲比赛信息.txt", ios::out|ios::app);//注意必须加app否则会覆盖数据
if (!ofs.is_open())
{
cout << "打开文件失败" << endl;
return;
}
for (int i = 0; i < 3; i++)
{
ofs << rank[i] <<" " << v[i].name <<" "<< v[i].score << endl;
}
cout << "文件已保存" << endl;
ofs.close();
}
void previous()//显示往届信息
{
string rank;
string name;
double score;
ifstream ifs;
ifs.open("演讲比赛信息.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
return;
}
int num = 0;//记录第几届
while (ifs >> rank &&ifs>>name && ifs >> score)
{
if (num % 3 == 0)
{
cout << "第" << (num/3)+1 << "届" << endl;
}
cout <<rank<< " " << "姓名:" << name << " " << "分数:" << " " << score << endl;
num++;
}
ifs.close();
if (num == 0)
{
cout << "暂无往届记录!" << endl;
}
system("pause");
}
void clear1()//清空文件信息
{
cout << "确认清空所有记录?(1:确定;0:退出)";
int choice = 2;
cin >> choice;
if (choice==0)
{
return;
}
else
{
ofstream ofs;
ofs.open("演讲比赛信息.txt",ios::trunc);
if (!ofs.is_open())
{
cout << "打开文件失败" << endl;
return;
}
ofs.close();
cout << "已清空所有记录" << endl;
}
}
主函数文件.cpp
#include"演讲比赛管理系统.h"
int main()
{
int choice = 0;
while (true)
{
interface();
cout << "请输入数字进行操作:";
cin >> choice;
switch (choice)
{
case 1://开始演讲比赛
startcontest();
break;
case 2://查看往届比赛记录
previous();
break;
case 3://清空比赛记录
clear1();
break;
case 4://退出比赛程序
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
break;
default:
cout << "输入错误,请从新输入" << endl;
}
}
return 0;
}
比赛分数还有相等的情况,可以适当添加.修改代码