c++小项目:基于STL的演讲比赛流程管理系统
一、项目目的
运用c++实现一个基于STL的演讲比赛流程管理系统。
比赛方式
- 共两轮,第一轮为分组淘汰赛,第二轮为决赛,共有十名评委,打分方式为去掉最高分和最低分的平均分为基准
- 第一轮共两组,每组六人,为随机分组和抽签决定演讲顺序,每组取前三名进入下一轮 ;
- 第二轮为淘汰赛,六名选手同台竞技,决出冠亚季军;
- 每轮比赛结束后都会公布胜出选手分数及排名;
程序功能
- 开始演讲比赛:完成整届比赛的流程,每个比赛阶段需要给用户一个提示,用户按任意键后继续下一个阶段;该阶段还具有初始化比赛进程、抽签、进行比赛、显示晋级成果、保存分数的功能。
- 查看往届记录:查看往届比赛结冠亚军信息,每次比赛都会记录到文件(格式为csv)中。
- 清空比赛记录:将文件中往届比赛结冠亚军信息清空。
- 退出比赛程序:退出当前程序。
二、项目实现
1. 项目雏形
演讲比赛流程管理系统.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;
}
该段为项目的雏形,上面也是接下来我们将要实现的功能。
2. 管理类的实现
首先在SpeechManager.h中设计管理类
#pragma once
#include<iostream>using namespace std;class SpeechManager
{public:SpeechManager();~SpeechManager();
};
然后在SpeechManager.cpp中将构造和析构函数空实现补全
#include "speechManager.h"SpeechManager::SpeechManager(){}SpeechManager::~SpeechManager(){}
3. 实现菜单功能
首先在管理类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;
}
实现效果:
4. 实现退出功能
首先在在SpeechManager类中添加成员函数 void exitSystem()
然后在SpeechManager.cpp中实现
void SpeechManager::Exit_system(){cout << " 欢迎下次使用 " << endl;system("pause");exit(0);
}
实现效果:
5. 实现比赛功能
5.1 选手类的实现
在Speaker.h设计选手类
#pragma once;
#include<iostream>
#include<string>using namespace std;class Speaker{public:string Name;double Score[2];//最多有两轮成绩
};
5.2添加成员变量
在SpeechManager类中添加成员变量
int rounds; //比赛轮次vector<int>v1; //第一轮参与选手(12位vector<int>v2;//晋级第二轮的选手(6位vector<int>third;//获得前三名的选手map<int, Speaker>speaker;//存放编号以及对应具体选手
5.3 初始化成员变量
首先在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();
}
5.4 创建选手
首先在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();
}
5.5 比赛功能实现
首先在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");
}
5.5.1 抽签功能实现
首先在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;
}
实现效果:
5.5.2 比赛功能实现
首先在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~1000d.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");
}
实现效果:
5.5.3 显示功能实现
首先在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();
}
实现效果:
5.5.4 第二轮比赛实现
第二轮比赛仅需将比赛轮次+1,后续流程与第一轮一样
实现效果:
5.5.5 保存功能实现
首先在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,里面保存了前三名选手的编号以及得分
6. 实现读取功能
首先在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();
7. 实现查看功能
首先在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");
}
实现效果:
8. 实现清空功能
首先在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");
}
实现效果:
- 至此本项目结束
三、源代码显示
1. 头文件
“Speaker.h”
#pragma once;
#include<iostream>
#include<string>using namespace std;class Speaker{public:string Name;double Score[2];
};
“SpeechManage.h”
#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();
};
2. 源文件
“SpeechManage.cpp”
#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");
}
“演讲比赛流程管理系统.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;
}
c++小项目:基于STL的演讲比赛流程管理系统相关推荐
- C++阶段06笔记01【基于STL的演讲比赛流程管理系统】
C++匠心之作-从0到1入门学编程[视频+课件+笔记+源码] 目录 1.演讲比赛程序需求 1.1.比赛规则 1.2.程序功能 1.3.程序效果图 2.项目创建 2.1.创建项目 2.2.添加文件 3. ...
- C++基于STL的演讲比赛流程管理系统
目录 介绍 speaker.h speechManager.h speechManager.cpp 演讲比赛流程管理系统.cpp 介绍 学校举行一场演讲比赛,共有12个人参加.比宴共两轮,第一轮为淘汰 ...
- 基于STL的演讲比赛流程管理系统
一.关于这个管理系统所要实现的功能 1.12名种子选手进行轮次比赛. 2.共进行两轮比赛,第一轮分两组进行演讲比赛经过评委打分决出下一轮比赛的选手,第二轮总决赛选出前三名选手(演讲顺序依抽签为准!)( ...
- 基于STL的演讲比赛流程管理系统(C++STL实战)
目录 1.演讲比赛程序需求 1.1比赛规则 1.2程序功能 2.项目创建 3.创建管理类 3.1创建文件 3.2头文件实现 3.3源文件实现 4.菜单功能 4.1添加成员函数 4.2菜单功能实现 4. ...
- 黑马程序员:C++案例(基于STL实现演讲比赛流程管理系统)
1.1.比赛规则 学校举行一场演讲比赛,共有12个人参加,比赛共两轮,第一轮为淘汰赛,第二轮为决赛 每名选手都有对应的编号,如10001~10012 比赛方式:分组比赛,每组6个人: 第一轮分为两个小 ...
- C++提高编程(六)—— 案例 :演讲比赛流程管理系统(上)
C++系列内容的学习目录 → \rightarrow →C++学习系列内容汇总. 1. 演讲比赛程序需求 1.1 比赛规则 1.2 程序功能 1.3 程序效果图 2. 创建项目 3. 创建管理类 3. ...
- C++项目实战 —— 演讲比赛流程管理系统
演讲比赛流程管理系统 文章目录 演讲比赛流程管理系统 1.演讲比赛程序需求 1.1 比赛规则 1.2 程序功能 2.项目创建 3.创建管理类 3.1 创建文件 3.2 头文件实现 3.3 源文件实现 ...
- 【49C++项目案例:演讲比赛流程管理系统】
文章目录 演讲比赛流程管理系统 1.演讲比赛程序需求 1.1 比赛规则 1.2 程序功能 1.3 程序效果图 2.项目创建 2.1 创建项目 2.2 添加文件 3.创建管理类 3.1 创建文件 3.2 ...
- 演讲比赛流程管理系统C++
参考B站黑马视频 演讲比赛流程管理系统 1.1.比赛规则 学校举行演讲比赛,共有12人参加.比赛共两轮,第一轮为淘汰赛,第二轮为决赛 每名选手都有对应的编号,如10001 ~ 10012 比赛方式:分 ...
最新文章
- Linux下显示前10个占用空间最大的文件或目录命令
- linux定时刷新窗口,Linux的屏幕刷新率问题 窗口调整问题
- 用python的turtle画圆-Python turtle 绘图画圆
- python 验证回文串
- android 通知显示时间,android:在特定时间显示通知?
- oracle通过sid远程登录,oracle怎么捕获用户登录信息,如SID,IP地址等
- MySQL Infobright 数据仓库快速安装笔记[转]
- 如何使用腾讯云提供的虚拟主机
- mikechen详谈架构师成长之3大步骤
- 一次打卡软件的实战渗透测试
- 干货 :如何系统地学习数据挖掘
- 全是干货:MBR分区结构以及GPT分区结构
- redis 常用配置文件配置
- 【读书笔记】《CSS新世界》—— 第一章 概述
- MT 3DGIS 试用版本开发包及教程下载地址
- 【笔记】74HC573的一些记录
- 简单个人静态HTML网页设计作品——广西北海家乡旅游景点 10页 DIV布局个人介绍网页模板代码 DW个人网站制作成品 web网页制作与实现
- 【Microarchitecture of Intel and AMD CPU】 9 Sandy Bridge and Ivy Bridge pipeline 【9.1~9.3】
- S3C2440 由ADS移植到 RealView MDK kile4
- 《OKR工作法:谷歌、领英等公司的高绩效秘籍》读书笔记
热门文章
- MATLAB偏振光的反射与折射,自然光的反射折射和偏振特性如下.PPT
- numpy PIL tensor之间的相互转换
- thinkphp PHPExcel 导入数据
- 微信公众号第三方平台开发笔记--02获取component_verify_ticket
- 数组排列组合问题——BACKTRACKING
- 硕士论文评阅意见的模板参考
- 4.19 亿条 Facebook 用户账号及电话号码被泄露【智能快讯】
- 新年寄语 —— 奋斗2022
- UNITY 开发日记/教程 俄罗斯方块 (五) 方块平移和旋转
- 大力金钢机器人_大力金刚机器人(威海)有限公司