该案例是黑马的案例,不方便下载源代码的可以看一下
该案例有两个头文件,两个源文件
main.c :与用户交互 ,显示菜单等
#include<iostream>
using namespace std;
#include"speechManager.h"
#include<ctime>
int main()
{srand((unsigned int)time(NULL));SpeechManager sm;//创建speechmanager对象for (auto it = sm.m.begin(); it != sm.m.end(); it++){cout << "编号:" << it->first << " 姓名:" << it->second.m_name << " 分数:" << it->second.m_score[0] << endl;}while (1){sm.showMeneu();cout << "请选择:" << endl;int chooce;cin >> chooce;switch (chooce){case 1:sm.startspeech();//开始比赛break;case 2:sm.showrecord();//比赛记录break;case 3:sm.clearecord();//清除记录break;case 0:sm.exitsystem();//退出程序break;default:system("cls");break;}}system("pause");return 0;
}
speaker.h :里面存放speeker类
#pragma once
#include<iostream>
using namespace std;
#include<string>class Speaker
{
public:string m_name;double m_score[2];//最多有两轮得分
};
speechmanager.h : 存放功能函数的声明,以及speaker的容器
第一轮演讲12人,用v1存储
第二轮6人,用v2存储
胜出的3人,用win存储
#pragma
#include<iostream>
#include<vector>
#include<map>
#include"speaker.h"
#include<algorithm>
#include<numeric>
#include<string>
#include<fstream>
using namespace std;
class SpeechManager
{
public:SpeechManager();//菜单void showMeneu();//退出void exitsystem();//容器初始化void initspeech();//创建选手void creatspeaker();~SpeechManager();//开始比赛void startspeech();//抽签void speechdraw();//比赛进程void speechcontest();//显示得分void showscore();//保存分数void saverecord();//读取记录void loadrecord();//显示往届记录void showrecord();//判断文件是否为空bool fileempty;//存放往届记录的容器map<int, vector<string>>m_record;//清空记录void clearecord();vector<int>v1;vector<int>v2;vector<int>win;map<int, Speaker>m;int m_index;
};
speechmanager.c :功能函数的实现
#include<iostream>
using namespace std;
#include"speechManager.h"
#include<deque>
#include<algorithm>
SpeechManager::SpeechManager()
{this->initspeech();this->creatspeaker();\//加载往届记录this->loadrecord();
}void SpeechManager::showMeneu()
{cout << "******************************" << endl;cout << "********欢迎参加演讲比赛******" << endl;cout << "********1、开始演讲比赛*******" << endl;cout << "********2、查看往届记录*******" << endl;cout << "********3、清空比赛记录*******" << endl;cout << "********0、退出比赛程序*******" << endl;cout << "******************************" << endl;
} void SpeechManager::exitsystem()
{cout << "欢迎下次使用" << endl;system("pause");exit(0);
}
void SpeechManager::initspeech()
{//容器置空this->v1.clear();this->v2.clear();this->win.clear();this->m.clear();this->m_index = 1;this->m_record.clear();
}
//创建选手
void SpeechManager::creatspeaker()
{string nameseed = "ABCDEFGHIJKL";for (int i = 0; i < nameseed.size(); i++){string name = "选手";name += nameseed[i];Speaker sp;sp.m_name = name;for (int i = 0; i < 2; i++){sp.m_score[i] = 0;}this->v1.push_back(i+10001);this->m.insert(make_pair(i + 10001, sp));}
}
//开始比赛
void SpeechManager::startspeech()
{//第一轮开始比赛//抽签this->speechdraw();//比赛this->speechcontest();//显示结果this->showscore();//第二轮开始比赛this->m_index++;//抽签this->speechdraw();//比赛this->speechcontest();//显示结果this->showscore();//保存记录this->saverecord();this->initspeech();this->creatspeaker();this->loadrecord();cout << "比赛完毕。" << endl;system("pause");system("cls");
}
void SpeechManager::speechdraw()
{cout << "第<<" << this->m_index << ">>轮比赛选手正在抽签" << endl;cout << "————————————" << endl;cout << "抽签后演讲顺序如下: " << endl;if (this->m_index == 1){ random_shuffle(v1.begin(), v1.end());for (auto it = v1.begin(); it != v1.end(); it ++ ){cout << *it << " ";}}else{random_shuffle(v2.begin(), v2.end());for (auto it = v2.begin(); it != v2.end(); it ++ ){cout << *it << " ";}}cout << "————————————" << endl;system("pause");cout << endl;
}
void SpeechManager::speechcontest()
{cout << "————————第<<" << this->m_index << ">>轮比赛开始————————" << endl;//临时容器存放成绩multimap<double, int, greater<double>>group;int num = 0;vector<int>v_scr;if (this->m_index == 1){v_scr = v1;}else{v_scr = v2;}for (auto it = v_scr.begin(); it != v_scr.end(); it++){num++;deque<double>d;for (int i = 0; i < 10; i++){double score = (rand() % 401 + 600)/10.f;//cout << score << " ";d.push_back(score);}sort(d.begin(), d.end(), greater<double>());d.pop_back();d.pop_front();double sum = accumulate(d.begin(), d.end(), 0);//平均分double avr = sum / (double)d.size();//cout << "编号:" <<*it<< " 姓名:"<<this->m[*it].m_name<<" 平均分:"<<avr<<endl;//平均分放入mapthis->m[*it].m_score[this->m_index - 1] = avr;//分数放入临时容器group.insert(make_pair(avr, *it));//key是平均分 val是编号if (num % 6 == 0){cout << "第<<" << num / 6 << ">>小组比赛名次:" << endl;for (auto it = group.begin(); it != group.end(); it++){cout << "编号:" << it->second << " 姓名:" << this->m[it->second].m_name << " 成绩:" << it->first << endl;}//取前三int count = 0;for (auto it=group.begin();it!=group.end()&&count<3;it++,count++){if (this->m_index==1){v2.push_back((*it).second);}else{win.push_back((*it).second);}}group.clear();cout << endl;}}cout << "————————第<<" << this->m_index<< ">>轮比赛完毕————————" << endl;system("pause");}
//显示得分
void SpeechManager::showscore()
{cout << "——————第<<" << this->m_index << ">>轮晋级选手信息如下——————" << endl;vector<int>v;if (this->m_index == 1){v = v2;}else{v = win;}for (auto it = v.begin(); it!= v.end(); it++){cout << "编号:" << *it << " 姓名:"<< this->m[*it].m_name << " 成绩:" << this->m[*it].m_score[this->m_index-1] << endl;}cout << endl;system("pause");system("cls");this->showMeneu();
}
void SpeechManager::saverecord()
{ofstream ofs;ofs.open("speech.csv", ios::out | ios::app);for (auto it = win.begin(); it != win.end(); it++){ofs << *it << "," << this->m[*it].m_score[1] << ",";}ofs << endl;ofs.close();cout << "记录已保存" << endl;this->fileempty = false;}
void SpeechManager::loadrecord()
{ifstream ifs("speech.csv", ios::in);if (!ifs.is_open()){this->fileempty = true;//cout << "文件不存在。" << endl;ifs.close();return;} char ch;ifs >> ch;if (ifs.eof()){//cout << "文件为空。" << endl;this->fileempty = true;ifs.close();return;}this->fileempty = 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 temp = data.substr(start, pos - start);//cout << temp << endl;v.push_back(temp);start = pos + 1; }this->m_record.insert(make_pair(index, v));index++;}ifs.close();for (auto it = this->m_record.begin(); it != this->m_record.end(); it++){cout << it->first << " 冠军编号: " << it->second[0] << "分数: " << it->second[1] << endl;}}void SpeechManager::showrecord()
{if (this->fileempty){cout << "文件为空或不存在"<<endl;}else {for (int i = 0; i < this->m_record.size(); i++){cout << "第<<" << i + 1 << ">>届" << endl<< " 冠军编号: " << this->m_record[i][0] << "分数: " << this->m_record[i][1] << endl<< " 亚军编号: " << this->m_record[i][2] << "分数: " << this->m_record[i][3] << endl<< " 季军编号: " << this->m_record[i][4] << "分数: " << this->m_record[i][5] << endl;}}
}
void SpeechManager::clearecord()
{cout << "是否确认清空?" << endl;cout << "1、是" << endl << "2、否" << endl;int select = 0;cin >> select;if (select==1){ofstream ofs("speech.csv", ios::trunc);ofs.close();this->initspeech();this->creatspeaker();this->loadrecord();cout << "清空成功" << endl;}system("pause");system("cls");
}
SpeechManager::~SpeechManager()
{}