前言
该程序适合练手C++项目,适用于课程设计作业,实训作业,学习C++技术栈等。
一、
1.极棒比赛流程管理系统
头文件: competion.h
#include<iostream>
using namespace std;
#include<fstream>
#include<map>
#include<vector>
#include"competior.h"
#include<algorithm>
#include<functional>
#include<deque>
#include<numeric>
#include<cstdlib>
#include<ctime>
class competion
{
public:
competion();
void showmenu();
void cleardata();
void exitapl();
void creatcompetior();
~competion();
vector<int>v1;//第一轮比赛人员
vector<int>v2;//第二轮比赛人员
vector<int>victory;//获胜者
map<int, competior>m_competior;
int compt_num ;
bool fileempty;
map<int, vector<string>>m_record;
void startcomt();
void contest();
void showscore();
void comptdraw();
void saverecord();
void loadfile();
void showlatescore();
void deletedata();
private:
};
头文件:competior.h
#include<string>
#include<iostream>
using namespace std;
class competior
{
public:
string p_name;
double p_score[2];
};
competion.cpp
#include"competion.h"
#include"competior.h"
competion::competion()
{
this->cleardata();
this->creatcompetior();
this->loadfile();
}
competion::~competion()
{
}
void competion::showmenu()
{
cout << "********************************************" << endl;
cout << "************* 欢迎参加极棒大赛 ************" << endl;
cout << "************* 1.开始比赛 *****************" << endl;
cout << "************* 2.查看往届记录 *************" << endl;
cout << "************* 3.清空比赛记录 *************" << endl;
cout << "************* 0.退出比赛程序 *************" << endl;
cout << "********************************************" << endl;
cout << endl;
}
void competion::exitapl()
{
cout << "欢迎下次使用!!!" << endl;
system("pause");
exit(0); //exit接口作为强行退出
}
void competion::cleardata()
{
this->v1.clear();
this->v2.clear();
this->victory.clear();
this->m_competior.clear();
this->compt_num = 1;
this->m_record.clear();
}
void competion::creatcompetior()
{
string nameseed = "ABCDEFGHIJKL";
for (int i = 0; i < nameseed.size(); i++)
{
string name = "参赛选手";
name += nameseed[i];
competior sp;
sp.p_name = name;
for (int i = 0; i < 2; i++)
{
sp.p_score[i] = 0;
}
this->v1.push_back(i + 10001);
this->m_competior.insert(make_pair(i + 10001, sp));
}
}
void competion::startcomt()
{
//准备比赛
this->comptdraw();
//开始比赛
//第一轮比赛
this->contest();
this->showscore();
this->compt_num++;
//第二轮比赛
this->contest();
this->showscore();
this->saverecord();
this->cleardata();
this->creatcompetior();
this->loadfile();
}
void competion::showscore()
{
vector<int>v;
if (this->compt_num == 1)
{
v = v2;
cout << "第" << this->compt_num << "轮晋级名单:" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "编号:" << *it << "姓名:" << this->m_competior[*it].p_name << "分数:" << this->m_competior[*it].p_score[this->compt_num - 1] << endl;
}
system("pause");
system("cls");
}
else
{
v = victory;
cout << "获奖名单:"<< endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "编号:" << *it << "姓名:" << this->m_competior[*it].p_name << "分数:" << this->m_competior[*it].p_score[this->compt_num - 1] << endl;
}
system("pause");
system("cls");
}
//this->showmenu();
}
void competion::saverecord()
{
ofstream ofs;
ofs.open("record.csv", ios::out | ios::app);
for (vector<int>::iterator it = victory.begin(); it != victory.end(); it++)
{
ofs << *it << ',' << this->m_competior[*it].p_score[1] << ',';
}
ofs << endl;
ofs.close();
cout << "比赛记录保存成功!!!" << endl;
cout << "比赛结束!!!" << endl;
this->fileempty = false;
system("pause");
system("cls");
}
void competion::loadfile()
{
ifstream ifs("record.csv", ios::in);
if (!ifs.is_open())
{
fileempty = true;
//cout << "文件不存在(无比赛记录)" << endl;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
fileempty = true;
//cout << "文件为空(无比赛纪录)" << endl;
ifs.close();
return;
}
fileempty = false;
string data;
int jie=0;
while (ifs >> data)
{
int pos = -1;
int start = 0;
vector<string>v;
while (true)
{
pos = data.find(',', start);
if (pos == -1)
{
break;
}
string sub = data.substr(start, pos - start);
v.push_back(sub);
start = pos + 1;
}
this->m_record.insert(make_pair(jie, v));
jie++;
}
ifs.close();
}
void competion::contest()
{
cout << "第"<<this->compt_num<<"轮比赛进行中:" << endl;
multimap<double, int, greater<double>>groupscore;
vector<int>vc;
int num = 0;
if (this->compt_num==1)
{
vc = v1;
}
else
{
vc = v2;
}
for (vector<int>::iterator it = vc.begin(); it != vc.end(); it++)
{
num++;
deque<double>d;
for (int i = 0; i < 5; i++)
{
double score = (rand() % 401 + 600) / 10.f;
//cout << score << ' ';
d.push_back(score);
}
//cout << endl;
sort(d.begin(), d.end(), greater<double>());
d.pop_front();
d.pop_back();
double sum = accumulate(d.begin(), d.end(), 0.0f);
double aver = sum / (double)d.size();
//cout << "编号:" << *it << "选手" << this->m_competior[*it].p_name << "平均分:" <<aver<< endl;
this->m_competior[*it].p_score[this->compt_num-1] = aver;
groupscore.insert(make_pair(aver, *it));
if (num % 6 == 0)
{
cout << "第" << num / 6 << "组排名成绩公布如下:" << endl;
for (multimap<double, int,greater<double>>::iterator it = groupscore.begin(); it != groupscore.end(); it++)
{
cout << "选手编号;" << it->second << "姓名:" << this->m_competior[it->second].p_name << "分数:" << this->m_competior[it->second].p_score[this->compt_num-1] << endl;
}
int count = 0;
for (multimap<double, int, greater<double>>::iterator it = groupscore.begin(); it != groupscore.end()&&count<3;count++, it++)
{
if (this->compt_num == 1)
{
v2.push_back(it->second);
}
else
{
victory.push_back(it->second);
}
}
groupscore.clear();
}
}
cout << "第"<<this->compt_num<<"轮比赛结束" << endl;
system("pause");
system("cls");
}
void competion::comptdraw()
{
system("cls");
cout << "第一轮比赛正式开始,进行抽签环节中" << endl;
cout << "------------------------------------------" << endl;
cout << "抽签的位次顺序为比赛的顺序,祝各位取得好成绩!!!" << endl;
cout << "选手抽签的顺序为:" << endl;
if (this->compt_num == 1)
{
random_shuffle(v1.begin(), v1.end());
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it << ' ';
}
cout << endl;
}
else
{
random_shuffle(v2.begin(), v2.end());
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << ' ';
}
cout << endl;
}
cout << "---------------------------------------" << endl;
system("pause");
system("cls");
}
void competion::showlatescore()
{
if (fileempty)
{
cout << "无比赛记录!!!" << endl;
}
else
{
for (int i = 0; i < this->m_record.size(); i++)
{
cout << "第" << i + 1 << "届极棒比赛记录:" << endl;
cout << "冠军选手号:" << this->m_record[i][0] << "比赛成绩:" << this->m_record[i][1] << ' '
<< "亚军选手号:" << this->m_record[i][2] << "比赛成绩:" << this->m_record[i][3] << ' '
<< "季军选手号:" << this->m_record[i][4] << "比赛成绩:" << this->m_record[i][5] << ' ';
cout << endl;
}
}
system("pause");
system("cls");
}
void competion::deletedata()
{
int choose;
cout << "是否清空记录?" << endl;
cout << "1.确定" << "2.取消" << endl;
cin >> choose;
if (choose == 1)
{
ofstream ofs("record.csv", ios::trunc);
ofs.close();
this->cleardata();
this->creatcompetior();
this->loadfile();
cout << "记录清空成功!!!" << endl;
}
system("pause");
system("cls");
}
main.cpp
#include<iostream> #include"competion.h" using namespace std; int main() { competion pl; int choose; srand((unsigned int) time(NULL)); while (true) { pl.showmenu(); cout << "请输入:"; cin >> choose; switch (choose) { case 1: pl.startcomt(); break; case 2: pl.showlatescore(); break; case 3: pl.deletedata(); break; case 0: system("cls"); pl.exitapl(); break; default: system("cls"); break; } } system("pause"); return 0; }
总结
若是有用,请各位客官拿走源码后,留下您的收藏和点赞,谢谢啦!!!