前言
在学习完C++的stl容器后,我们来写一下小项目对其进行应用!
项目名称为:演讲比赛系统
一、演讲比赛程序需求
二、每个功能模块的实现
1. 创建管理类(.h文件)
功能描述:
提供菜单界面与用户交互
对演讲比赛流程进行控制
与文件的读写交互
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<map>
#include"Speaker.h"
#include<time.h>
#include<algorithm>
#include<numeric>
#include<fstream>
using namespace std;
class speechManager {
public:
speechManager();
void Show_Menu();
void initSperker();
void creatSpeaker();
void startSpeech();
void speechDraw();
void speechContest();
void showScore();
void saveRecord();
void gameOver();
~speechManager();
void loadRecord();
void showRecord();
void clearRecord();
bool fileIsEmpty;
map<int, vector<string>> _Record;
vector<int> v1;
vector<int> v2;
vector<int> vVictory;
map<int, Speaker> _Speaker;
int index;
};
2.1. 创建管理类(.cpp文件)
#include"SpeechManager.h"
speechManager::speechManager()
{
initSperker();
creatSpeaker();
this->loadRecord();
}
void speechManager::Show_Menu()
{
cout << "************************************************" << endl;
cout << "*************欢迎参加演讲比赛*******************" << endl;
cout << "*************1.开始演讲比赛*********************" << endl;
cout << "*************2.查看往届比赛记录*****************" << endl;
cout << "*************3.清空比赛记录*********************" << endl;
cout << "*************0.退出比赛系统*********************" << endl;
cout << "************************************************" << endl;
}
void speechManager::gameOver()
{
cout << "欢迎下次再来!" << endl;
system("pause");
system("cls");
}
void speechManager::initSperker()
{
v1.clear();
v2.clear();
vVictory.clear();
_Speaker.clear();
index = 1;
this->_Record.clear();
}
void speechManager::creatSpeaker()
{
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < nameSeed.size(); i++)
{
string name("选手");
name += nameSeed[i];
Speaker sp;
sp._name = name;
for (int i = 0; i < 2; i++)
{
sp._score[i] = 0;
}
v1.push_back(10001 + i);
_Speaker.insert(make_pair(10001 + i, sp));
}
}
void speechManager::speechDraw()
{
cout << "第 " << index << " 轮选手正在抽签" << endl;
cout << "-----------------------------" << endl;
cout << "抽签后的演讲顺序如下" << endl;
if (index == 1)
{
random_shuffle(v1.begin(), v1.end());
for (auto it = v1.begin(); it != v1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
else
{
random_shuffle(v2.begin(), v2.end());
for (auto it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
cout << "-----------------------------------" << endl;
system("pause");
}
void speechManager::speechContest()
{
cout << "-------------第" << index << "轮比赛正式开始---------------" << endl;
multimap<double, int, greater<double>> groupScore;
int num = 0;
vector<int> v_src;
if (index == 1)
{
v_src = v1;
}
else
{
v_src = v2;
}
for (auto it = v_src.begin(); it != v_src.end(); it++)
{
num++;
deque<double> d;
for (int i = 0; i < 10; i++)
{
double score = (rand() % 401 + 600) / 10.0f;
d.push_back(score);
}
sort(d.begin(), d.end());
d.pop_back();
d.pop_front();
double sum = accumulate(d.begin(), d.end(), 0.0f);
double avg = sum /d.size();
_Speaker[*it]._score[index - 1] = avg;
groupScore.insert(make_pair(avg, *it));
if (num % 6 == 0)
{
cout << "第" << num / 6 << "组比赛名次" << endl;
for (auto it = groupScore.begin(); it != groupScore.end(); it++)
{
cout << "编号: " << it->second << " 姓名: " << this->_Speaker[it -> second]._name
<< " 成绩: " << this->_Speaker[it->second]._score[this->index - 1] <<endl;
}
int count = 0;
for (auto it = groupScore.begin(); it!= groupScore.end() && count < 3; it++, count++)
{
if (this->index == 1)
{
v2.push_back((*it).second);
}
else
{
vVictory.push_back((*it).second);
}
}
groupScore.clear();
cout << endl;
}
}
cout << "---------第" << index << "轮比赛完毕-----------" << endl;
system("pause");
}
void speechManager::showScore()
{
cout << "第" << index << "轮晋级选手如下" << endl;
vector<int> v;
if (index == 1)
{
v = v2;
}
else
{
v = vVictory;
}
for (auto it = v.begin(); it != v.end(); it++)
{
cout << "选手编号:" << *it << " 姓名: " << _Speaker[*it]._name << " 得分: " <<
_Speaker[*it]._score[this->index - 1] << endl;
}
cout << endl;
}
void speechManager::startSpeech()
{
speechDraw();
speechContest();
showScore();
index++;
speechDraw();
speechContest();
showScore();
saveRecord();
initSperker();
creatSpeaker();
this->loadRecord();
system("cls");
}
void speechManager::saveRecord()
{
ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app);
for (auto it = vVictory.begin(); it != vVictory.end(); it++)
{
ofs << *it << "," << _Speaker[*it]._score[1] << ",";
}
ofs << endl;
ofs.close();
this->fileIsEmpty = false;
system("pause");
}
void speechManager::loadRecord()
{
ifstream ifs("speech.csv", ios::in);
if (!ifs.is_open())
{
this->fileIsEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
this->fileIsEmpty = true;
ifs.close();
return;
}
this->fileIsEmpty = false;
ifs.putback(ch);
string data;
int index = 0;
while (ifs >> data)
{
vector<string>v;
int pos = -1;
int start = 0;
while (true)
{
pos = data.find(",", start);
if (pos == -1)
{
break;
}
string tmp = data.substr(start, pos - start);
v.push_back(tmp);
start = pos + 1;
}
this->_Record.insert(make_pair(index, v));
index++;
}
ifs.close();
}
void speechManager::showRecord()
{
if (this->fileIsEmpty)
{
cout << "文件为空或不存在!" << endl;
}
else
{
for (int i = 0; i < this->_Record.size(); i++)
{
cout << "第" << i + 1 << "届 " <<
"冠军编号:" << this->_Record[i][0] << " 得分:" << this->_Record[i][1] << " "
"亚军编号:" << this->_Record[i][2] << " 得分:" << this->_Record[i][3] << " "
"季军编号:" << this->_Record[i][4] << " 得分:" << this->_Record[i][5] << endl;
}
}
system("pause");
system("cls");
}
void speechManager::clearRecord()
{
cout << "确认清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
ofstream ofs("speech.csv", ios::trunc);
ofs.close();
this->initSperker();
this->creatSpeaker();
this->loadRecord();
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
speechManager::~speechManager()
{
}
3.创建参赛选手类(.h)
#include<string>
#include<iostream>
using namespace std;
class Speaker {
public:
string _name;
double _score[2];
};
4.将整体逻辑进行封装
#include"SpeechManager.h"
int main0()
{
speechManager pm;
srand((unsigned int)(time(NULL)));
int choice = 1;
do
{
pm.Show_Menu();
cout << "请输入你的选项:" << endl;
cin >> choice;
switch (choice)
{
case 1:
pm.startSpeech();
break;
case 2:
pm.showRecord();
break;
case 3:
pm.clearRecord();
break;
case 0:
pm.gameOver();
break;
default:
cout << "输入有误,请按任意键后重新输入!" << endl;
system("pause");
system("cls");
break;
}
} while (choice);
return 0;
}
测试
第一轮比赛结束
第二轮比赛开始抽号
第二轮比赛结束
第二轮比赛得出结果
查看往届比赛成绩
清空往届比赛成绩
项目总结
本案例主要是对C++的stl容器和文件操作进行使用,基本涵盖了容器常用接口的使用,对大家对于stl的进一步熟悉能起较大左右作用!!!