赞
踩
运用c++实现一个基于STL的演讲比赛流程管理系统。
演讲比赛流程管理系统.cpp
#include<iostream> #include"SpeechManager.h" #include<ctime> using namespace std; int main(){ srand((unsigned int)time(NULL)); SpeechManager s1; int input; while (1){ s1.Show_menu(); cout << " 请输入你的选项: " << endl; cin >> input; switch (input){ case 0: //退出比赛程序 s1.Exit_system(); break; case 1: //开始演讲比赛 s1.Start_speech(); break; case 2: //查看往届记录 s1.Show_record(); break; case 3: //清空比赛记录 s1.Clear_record(); break; default: system("cls");//清屏 break; } } system("pause"); return 0; }
该段为项目的雏形,上面也是接下来我们将要实现的功能。
首先在SpeechManager.h中设计管理类
#pragma once
#include<iostream>
using namespace std;
class SpeechManager
{
public:
SpeechManager();
~SpeechManager();
};
然后在SpeechManager.cpp中将构造和析构函数空实现补全
#include "speechManager.h"
SpeechManager::SpeechManager(){
}
SpeechManager::~SpeechManager(){
}
首先在管理类SpeechManager类中添加成员函数 void show_Menu()
然后在SpeechManager.cpp中实现
void SpeechManager::Show_menu(){
cout << " " << endl;
cout << " 欢迎参加演讲比赛! " << endl;
cout << " 1.开始演讲比赛 " << endl;
cout << " 2.查看往届记录 " << endl;
cout << " 3.清空比赛记录 " << endl;
cout << " 0.退出比赛程序 " << endl;
cout << " " << endl;
}
实现效果:
首先在在SpeechManager类中添加成员函数 void exitSystem()
然后在SpeechManager.cpp中实现
void SpeechManager::Exit_system(){
cout << " 欢迎下次使用 " << endl;
system("pause");
exit(0);
}
实现效果:
在Speaker.h设计选手类
#pragma once;
#include<iostream>
#include<string>
using namespace std;
class Speaker{
public:
string Name;
double Score[2];//最多有两轮成绩
};
在SpeechManager类中添加成员变量
int rounds; //比赛轮次
vector<int>v1; //第一轮参与选手(12位
vector<int>v2;//晋级第二轮的选手(6位
vector<int>third;//获得前三名的选手
map<int, Speaker>speaker;//存放编号以及对应具体选手
首先在SpeechManager类中添加成员函数 void Init_speaker()
然后在SpeechManager.cpp中实现
void SpeechManager::Init_speaker(){
//初始化选手数据
this->v1.clear();
this->v2.clear();
this->third.clear();
this->speaker.clear();
this->rounds = 1;//初始化比赛轮次
}
接着在SpeechManager.cpp中的SpeechManager构造函数中调用void Init_speaker()
SpeechManager::SpeechManager(){
this->Init_speaker();
}
首先在SpeechManager类中添加成员函数 void Create_speaker()
然后在SpeechManager.cpp中实现
void SpeechManager::Create_speaker(){
string nameSeed = "ABCDEFGHIJKL";
for (int i = 0; i < nameSeed.size(); i++){
string name = "选手";
name += nameSeed[i];
Speaker s;
s.Name = name;
for (int j = 0; j < 2; j++) s.Score[j] = 0;
this->v1.push_back(10001 + i);
this->speaker.insert(make_pair(10001 + i, s));
}
}
接着在SpeechManager.cpp中的SpeechManager构造函数中调用void Create_speaker()
SpeechManager::SpeechManager(){
this->Init_speaker();
this->Create_speaker();
}
首先在SpeechManager类中添加成员函数 void Start_speech()
然后在SpeechManager.cpp中实现
void SpeechManager::Start_speech(){ //第一轮比赛 //抽签 this->Drawing(); //比赛 this->Completion(); //显示晋级成果 this->Show_score(); //第二轮比赛 this->rounds++; //抽签 this->Drawing(); //比赛 this->Completion(); //显示最终结果 this->Show_score(); //保存分数 this->Record(); cout << "本届比赛顺利结束" << endl; //这一条与上面的显示会闪烁过去,原因不明 system("pasue"); system("cls"); }
首先在SpeechManager类中添加成员函数 void Drawing()
然后在SpeechManager.cpp中实现
void SpeechManager::Drawing(){ if (this->rounds == 1) cout << " 第一轮比赛选手正在抽签" << endl; else cout << " 第二轮比赛选手正在抽签" << endl; cout << "-------------------------------------" << endl; cout << " 演讲顺序如下" << endl; if (this->rounds == 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"); cout << endl; }
实现效果:
首先在SpeechManager类中添加成员函数 void Completion()
然后在SpeechManager.cpp中实现
void SpeechManager::Completion(){ if (this->rounds == 1) cout << " 第一轮比赛正式开始" << endl; else cout << " 第二轮比赛正式开始" << endl; multimap<double, int, greater<double>>group;//记录每组选手数据 int num = 0;//6人为一组 vector<int>v; if (this->rounds == 1) v = v1; else v = v2; for (vector<int>::iterator it = v.begin(); it != v.end(); it++){ num++; deque<double>d;//存放分数 for (int i = 0; i < 10; i++){ double score = (rand() % 401 + 600) / 10.f; //600~1000 d.push_back(score); } sort(d.begin(), d.end(), greater<double>()); d.pop_front();//去除最高分 d.pop_back();//去除最低分 double sum = accumulate(d.begin(), d.end(), 0.0f);//计算总分 double avg = sum / (double)d.size();//计算平均分 this->speaker[*it].Score[this->rounds - 1] = avg; group.insert(make_pair(avg, *it)); //6人一组 if (num % 6 == 0){ if (num / 6 == 1) cout << "第一小组比赛名次如下:" << endl; else cout << "第二小组比赛名次如下:" << endl; for (multimap<double, int, greater<double>>::iterator it = group.begin(); it != group.end(); it++) cout << "编号:" << it->second << " 姓名:" << this->speaker[it->second].Name << " 成绩:" << this->speaker[it->second].Score[this->rounds - 1] << endl; //取各组前三名 int count = 0; for (multimap<double, int, greater<double>>::iterator it = group.begin(); it != group.end() && count < 3; it++, count++){ if (this->rounds == 1) v2.push_back((*it).second); else third.push_back((*it).second); } group.clear(); cout << endl; } } if (this->rounds == 1) cout << " 第一轮比赛完毕" << endl; else cout << " 第二轮比赛完毕" << endl; system("pause"); }
实现效果:
首先在SpeechManager类中添加成员函数 void Show_score()
然后在SpeechManager.cpp中实现
void SpeechManager::Show_score(){ if (this->rounds == 1) cout << " 第一轮比赛晋级选手信息如下:" << endl; else cout << " 第二轮比赛晋级选手信息如下:" << endl; vector<int>v; if (this->rounds == 1) v = v2; else v = third; for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << "编号:" << *it<< " 姓名:" <<this-> speaker[*it].Name << " 成绩:" << this->speaker[*it].Score[this->rounds - 1] << endl; cout << endl; system("pause"); system("cls"); this->Show_menu(); }
实现效果:
第二轮比赛仅需将比赛轮次+1,后续流程与第一轮一样
实现效果:
首先在SpeechManager类中添加成员函数 void Record()
然后在SpeechManager.cpp中实现
void SpeechManager::Record(){
ofstream ofs;
ofs.open("speech.csv", ios::out | ios::app);// 用输出的方式打开文件
//存入数据
for (vector<int>::iterator it = third.begin(); it != third.end(); it++)
ofs << *it << "," << speaker[*it].Score[1] << ",";
ofs <<endl;
ofs.close();
cout << "记录已保存" << endl; //这一条与接下来的显示会闪烁过去,原因不明
}
利用记事本打开文件speech.csv,里面保存了前三名选手的编号以及得分
首先在SpeechManager类中添加成员函数和成员变量
void Load_record();//读取往届记录
bool File_is_empty;//判断文件是否为空
map<int, vector<string>>record;//往届记录
然后在SpeechManager.cpp中实现
void SpeechManager::Load_record(){ ifstream ifs("speech.csv", ios::in); if (!ifs.is_open()){ this->File_is_empty = true; cout << "文件不存在!" << endl; ifs.close(); return; } char ch; ifs >> ch; if (ifs.eof()){ cout << "文件为空!" << endl; this->File_is_empty = true; ifs.close(); return; } this->File_is_empty = 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 Record()
利用File_is_empty
更新文件状态
//比赛完后文件不为空
this->File_is_empty = false;
在SpeechManager.cpp中的SpeechManager构造函数中调用void Record()
SpeechManager::SpeechManager(){
this->Init_speaker();
this->Create_speaker();
this->Load_record();
}
在SpeechManager.cpp中的void Init_speaker()
函数中初始化记录容器
this->record.clear();
在SpeechManager.cpp中的void Start_speech()
函数中重置比赛
//重置比赛
this->Init_speaker();
this->Create_speaker();
this->Load_record();
首先在SpeechManager类中添加成员函数 void Show_record()
然后在SpeechManager.cpp中实现
void SpeechManager::Show_record(){
if (this->File_is_empty) cout << "文件不存在,或记录为空!" << endl;
else{
for (map<int, vector<string>>::iterator it = this->record.begin();
it != this->record.end(); it++){
cout << "第" << it->first+1 << "届" <<
"冠军编号:" << it->second[0] << " 得分:" << it->second[1] << " "
"亚军编号:" << it->second[2] << " 得分:" << it->second[3] << " "
"季军编号:" << it->second[4] << " 得分:" << it->second[5] << endl;
}
}
system("pause");
system("cls");
}
实现效果:
首先在SpeechManager类中添加成员函数 void Clear_record()
然后在SpeechManager.cpp中实现
void SpeechManager::Clear_record() { cout << "确认清空?" << endl << "1. Yes" << endl << "2. No" << endl; int select; cin >> select; if (select == 1){ ofstream ofs("speech.csv", ios::trunc); ofs.close(); this->Init_speaker(); this->Create_speaker(); this->Load_record(); cout << "清除成功" << endl; } else if (select == 2) return; else cout << "输入有误!" << endl; system("pause"); system("cls"); }
实现效果:
#pragma once;
#include<iostream>
#include<string>
using namespace std;
class Speaker{
public:
string Name;
double Score[2];
};
#pragma once #include<iostream> #include<vector> #include<map> #include"Speaker.h" #include<algorithm> #include<numeric> #include<functional> #include<deque> #include<fstream> using namespace std; class SpeechManager{ public: SpeechManager(); void Show_menu(); void Exit_system(); void Init_speaker(); int rounds; vector<int>v1; vector<int>v2; vector<int>third; map<int, Speaker>speaker; void Create_speaker(); void Start_speech(); void Drawing(); void Show_score(); void Record(); void Completion(); void Load_record(); bool File_is_empty; map<int, vector<string>>record; void Show_record(); void Clear_record(); ~SpeechManager(); };
#include "SpeechManager.h" SpeechManager::SpeechManager(){ this->Init_speaker(); this->Create_speaker(); this->Load_record(); } SpeechManager::~SpeechManager(){ } void SpeechManager::Show_menu(){ cout << " " << endl; cout << " 欢迎参加演讲比赛! " << endl; cout << " 1.开始演讲比赛 " << endl; cout << " 2.查看往届记录 " << endl; cout << " 3.清空比赛记录 " << endl; cout << " 0.退出比赛程序 " << endl; cout << " " << endl; } void SpeechManager::Exit_system(){ cout << " 欢迎下次使用 " << endl; system("pause"); exit(0); } void SpeechManager::Init_speaker(){ this->v1.clear(); this->v2.clear(); this->third.clear(); this->speaker.clear(); this->record.clear(); this->rounds = 1; } void SpeechManager::Create_speaker(){ string nameSeed = "ABCDEFGHIJKL"; for (int i = 0; i < nameSeed.size(); i++){ string name = "选手"; name += nameSeed[i]; Speaker s; s.Name = name; for (int j = 0; j < 2; j++) s.Score[j] = 0; this->v1.push_back(10001 + i); this->speaker.insert(make_pair(10001 + i, s)); } } void SpeechManager::Start_speech(){ this->Drawing(); this->Completion(); this->Show_score(); this->rounds++; this->Drawing(); this->Completion(); this->Show_score(); this->Record(); this->Init_speaker(); this->Create_speaker(); this->Load_record(); cout << "本届比赛顺利结束" << endl; system("pasue"); system("cls"); } void SpeechManager::Drawing(){ if (this->rounds == 1) cout << " 第一轮比赛选手正在抽签" << endl; else cout << " 第二轮比赛选手正在抽签" << endl; cout << "-------------------------------------" << endl; cout << " 演讲顺序如下" << endl; if (this->rounds == 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"); cout << endl; } void SpeechManager::Completion(){ if (this->rounds == 1) cout << " 第一轮比赛正式开始" << endl; else cout << " 第二轮比赛正式开始" << endl; multimap<double, int, greater<double>>group; int num = 0; vector<int>v; if (this->rounds == 1) v = v1; else v = v2; for (vector<int>::iterator it = v.begin(); it != v.end(); it++){ num++; deque<double>d; for (int i = 0; i < 10; i++){ double score = (rand() % 401 + 600) / 10.f; d.push_back(score); } sort(d.begin(), d.end(), greater<double>()); d.pop_front(); d.pop_back(); double sum = accumulate(d.begin(), d.end(), 0.0f); double avg = sum / (double)d.size(); this->speaker[*it].Score[this->rounds - 1] = avg; group.insert(make_pair(avg, *it)); if (num % 6 == 0){ if (num / 6 == 1) cout << "第一小组比赛名次如下:" << endl; else cout << "第二小组比赛名次如下:" << endl; for (multimap<double, int, greater<double>>::iterator it = group.begin(); it != group.end(); it++) cout << "编号:" << it->second << " 姓名:" << this->speaker[it->second].Name << " 成绩:" << this->speaker[it->second].Score[this->rounds - 1] << endl; int count = 0; for (multimap<double, int, greater<double>>::iterator it = group.begin(); it != group.end() && count < 3; it++, count++){ if (this->rounds == 1) v2.push_back((*it).second); else third.push_back((*it).second); } group.clear(); cout << endl; } } if (this->rounds == 1) cout << " 第一轮比赛完毕" << endl; else cout << " 第二轮比赛完毕" << endl; system("pause"); } void SpeechManager::Show_score(){ if (this->rounds == 1) cout << " 第一轮比赛晋级选手信息如下:" << endl; else cout << " 第二轮比赛晋级选手信息如下:" << endl; vector<int>v; if (this->rounds == 1) v = v2; else v = third; for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << "编号:" << *it<< " 姓名:" <<this-> speaker[*it].Name << " 成绩:" << this->speaker[*it].Score[this->rounds - 1] << endl; cout << endl; system("pause"); system("cls"); this->Show_menu(); } void SpeechManager::Record(){ ofstream ofs; ofs.open("speech.csv", ios::out | ios::app); for (vector<int>::iterator it = third.begin(); it != third.end(); it++) ofs << *it << "," << speaker[*it].Score[1] << ","; ofs <<endl; ofs.close(); cout << "记录已保存" << endl; this->File_is_empty = false; } void SpeechManager::Load_record(){ ifstream ifs("speech.csv", ios::in); if (!ifs.is_open()){ this->File_is_empty = true; cout << "文件不存在!" << endl; ifs.close(); return; } char ch; ifs >> ch; if (ifs.eof()){ cout << "文件为空!" << endl; this->File_is_empty = true; ifs.close(); return; } this->File_is_empty = 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::Show_record(){ if (this->File_is_empty) cout << "文件不存在,或记录为空!" << endl; else{ for (map<int, vector<string>>::iterator it = this->record.begin(); it != this->record.end(); it++){ cout << "第" << it->first+1 << "届" << "冠军编号:" << it->second[0] << " 得分:" << it->second[1] << " " "亚军编号:" << it->second[2] << " 得分:" << it->second[3] << " " "季军编号:" << it->second[4] << " 得分:" << it->second[5] << endl; } } system("pause"); system("cls"); } void SpeechManager::Clear_record() { cout << "确认清空?" << endl << "1. Yes" << endl << "2. No" << endl; int select; cin >> select; if (select == 1){ ofstream ofs("speech.csv", ios::trunc); ofs.close(); this->Init_speaker(); this->Create_speaker(); this->Load_record(); cout << "清除成功" << endl; } else if (select == 2) return; else cout << "输入有误!" << endl; system("pause"); system("cls"); }
#include<iostream> #include"SpeechManager.h" #include<ctime> using namespace std; int main(){ srand((unsigned int)time(NULL)); SpeechManager s1; int input; while (1){ s1.Show_menu(); cout << " 请输入你的选项: " << endl; cin >> input; switch (input){ case 0: s1.Exit_system(); break; case 1: s1.Start_speech(); break; case 2: s1.Show_record(); break; case 3: s1.Clear_record(); break; default: system("cls"); break; } } system("pause"); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。