|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 孤世星辰 于 2022-4-19 08:14 编辑
speechManager.h
- #pragma once
- #include<iostream>
- using namespace std;
- #include<string>
- #include<vector>
- #include<map>
- #include"speak.h"
- #include<algorithm>
- #include<deque>
- #include<functional>
- #include<numeric>
- #include<ctime>
- class SpeechManager
- {
- public:
- SpeechManager();
- //显示菜单
- void showMenu();
- //退出菜单
- void Exit();
- ~SpeechManager();
- //初始化容器和属性
- void initSpeech();
- //创建12名选手
- void createSpeaker();
- //比赛流程函数
- void startSpeech();
- //抽签
- void speechDraw();
- //比赛
- void speechContest();
- //保存第一轮比赛选手编号容器
- vector<int>v1;
-
- //保存第二轮比赛选手编号容器
- vector<int>v2;
-
- //胜出前三名选手编号容器
- vector<int>vVictory;
- //存放编号及对应具体选手容器
- map<int, Speaker>m_Speaker;
- //比赛轮数
- int m_index;
- };
复制代码
speak.h
- #pragma once
- #include<iostream>
- using namespace std;
- #include<string>
- //选手类
- class Speaker
- {
- public:
- string m_Name;
- double m_Score[2];//最多两轮得分
- };
复制代码
演讲比赛流程管理系统.cpp
- #include<iostream>
- using namespace std;
- #include<string>
- #include"speechManager.h"
- int main()
- {
- SpeechManager sm;
- srand((unsigned int)time(NULL));
- /*for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++)
- {
- cout << "选手编号:" << it->first << "选手姓名:" <<it->second.m_Name<< endl;
- }*/
- int choice = 0;
- while (true)
- {
- sm.showMenu();
- cout << "请输入您的选择" << endl;
- cin >> choice;
- switch (choice)
- {
- case 1:sm.startSpeech();//开始比赛
- break;
- case 2://查看往届记录
- break;
- case 3://清空比赛记录
- break;
- case 0:sm.Exit();//退出系统
- break;
- default:
- cout << "输入有误,请重新输入" << endl;
- system("pause");
- system("cls");
- break;
- }
- }
- system("pause");
- return 0;
- }
复制代码
speechManager.cpp
- #include<iostream>
- using namespace std;
- #include<string>
- #include"speechManager.h"
- SpeechManager:: SpeechManager()
- {
- this->initSpeech();
- this->createSpeaker();
- }
- void SpeechManager::showMenu()
- {
- cout << "******************************" << endl;
- cout << "***** 欢迎参加演讲比赛 *****" << endl;
- cout << "***** 1、开始演讲比赛 *****" << endl;
- cout << "***** 2、查看往届记录 *****" << endl;
- cout << "***** 3、清空比赛记录 *****" << endl;
- cout << "***** 0、退出比赛程序 *****" << endl;
- cout << "******************************" << endl;
- }
- void SpeechManager::Exit()
- {
- cout << "欢迎下次使用" << endl;
- system("pause");
- exit(0);
-
- }
- void SpeechManager::initSpeech()
- {
- //容器先置空
- this->v1.clear();
- this->v2.clear();
- this->vVictory.clear();
- this->m_Speaker.clear();
- this->m_index = 1;
- }
- void SpeechManager::createSpeaker()
- {
- string nameSeed = "ABCDEFGHIJKL";
- for (int i = 0; i < nameSeed.size(); i++)
- {
- string name = "选手";
- name += nameSeed[i];
- //创建选手
- Speaker sp;
- sp.m_Name = name;
- for (int j = 0; j < 2; j++)
- {
- sp.m_Score[j] = 0;
- }
- //创建选手编号放到v1容器中
- this->v1.push_back(i + 1001);
- //选手编号和选手信息放入map容器
- this->m_Speaker.insert(make_pair(i + 1001, sp));
- }
- }
- void SpeechManager::startSpeech()
- {
- //第一轮开始比赛
- //抽签
- this->speechDraw();
- //比赛
- this->speechContest();
- //显示晋级结果
- //
- //第二轮开始比赛
- //抽签
- //比赛
- //显示最终结果
-
- }
- void SpeechManager::speechDraw()
- {
- cout << "第" << this->m_index << "轮选手正在抽签" << endl;
- cout << "---------------------------------------" << endl;
- cout << "抽签后演讲顺序为" << endl;
- if (this->m_index == 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::speechContest()
- {
- cout << "----------第" << this->m_index << "轮比赛开始------------" << endl;
- //准备临时容器 存放小组数据
- multimap<double, int, greater<double>()>groupScore;
- int num = 0;
- vector<int>v_Src;//比赛选手容器
- if (this->m_index == 1)
- {
- v_Src = v1;
- }
- else
- {
- v_Src = v2;
- }
- for (vector<int>::iterator 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.F;
- //cout << score << " ";
- d.push_back(score);
- }
- //cout << endl;
- sort(d.begin(), d.end(), greater<int>());
- d.pop_back();
- d.pop_front();
- double sum = accumulate(d.begin(), d.end(),0.0f);
- double avg = sum / (double)d.size();
- //将平均分放入map容器
- this->m_Speaker[*it].m_Score[this->m_index-1] = avg;
- //打印平均分
- //cout << "编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 平均分:" << this->m_Speaker[*it].m_Score[this->m_index - 1] << endl; //3、这里我即便注释掉了他居然还是会输出
-
- groupScore.insert(make_pair(avg, *it));
-
- //每6人取出前三名
- if (num % 6 == 0) //4、不知道为什么就是进不了这个循环
- {
- cout << "第" << num / 6 << "小组名次:" << endl;
- for (multimap<double, int, greater<double>>::iterator it= groupScore.begin(); it != groupScore.end(); it++)
- {
- cout << "编号:" << it->second << " 姓名:" << this->m_Speaker[it->second].m_Name
- << " 成绩:" << this->m_Speaker[it->second].m_Score[this->m_index - 1] << endl;
- }
- }
- }
- }
- SpeechManager::~SpeechManager()
- {
- }
复制代码
1、我这个代码一开始是用c最新语法标准敲得,然后因为要用random_shuffle就又改了c14标准,然后我debug就感觉有很多问题,比如这个num为什么调试不出来
还有就是
2、调试到这里他就又会跳回for上面一行,然后要这样循环很多下才能继续进行
5、因为语法不一致,因此我打的断点没有用,然后我是点断点设置--位置--允许源码不一致,但是有时候会这样
没看懂你的问题
“这个num为什么调试不出来”
num怎么了?
1. 你在.h头文件里面包含了好多头文件,但是都没有使用
2. 不建议用 using namespace std;
会出现名字冲突的问题,我已经遇到好多次了
3.
- //multimap<double, int, greater<double>()> groupScore;
- multimap<double, int, greater<double>> groupScore;
复制代码
这行代码不对吧?
speak.h
- #ifndef _SPEAK_H_
- #define _SPEAK_H_
- //#pragma once
- //#include <iostream>
- #include <string>
- //using namespace std;
- using std::string;
- //选手类
- class Speaker {
- public:
- string m_Name;
- double m_Score[2]; //最多两轮得分
- };
- #endif
复制代码
speechManager.h
- #ifndef _SPEECHMANAGER_H_
- #define _SPEECHMANAGER_H_
- //#pragma once
- //#include <iostream>
- #include "speak.h"
- //#include <algorithm>
- //#include <ctime>
- //#include <deque>
- //#include <functional>
- #include <map>
- //#include <numeric>
- //#include <string>
- #include <vector>
- //using namespace std;
- using std::map, std::vector;
- class SpeechManager {
- public:
- SpeechManager();
- ~SpeechManager();
- //显示菜单
- void showMenu();
- //退出菜单
- void Exit();
- //初始化容器和属性
- void initSpeech();
- //创建12名选手
- void createSpeaker();
- //比赛流程函数
- void startSpeech();
- //抽签
- void speechDraw();
- //比赛
- void speechContest();
- //保存第一轮比赛选手编号容器
- vector<int> v1;
- //保存第二轮比赛选手编号容器
- vector<int> v2;
- //胜出前三名选手编号容器
- vector<int> vVictory;
- //存放编号及对应具体选手容器
- map<int, Speaker> m_Speaker;
- //比赛轮数
- int m_index;
- };
- #endif
复制代码
speechManager.cpp
- #include "speechManager.h"
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <deque>
- #include <numeric>
- //using namespace std;
- using std::cout, std::endl;
- using std::make_pair;
- using std::multimap, std::greater;
- using std::deque;
- SpeechManager::SpeechManager() {
- this->initSpeech();
- this->createSpeaker();
- }
- void SpeechManager::showMenu() {
- cout << "******************************" << endl;
- cout << "***** 欢迎参加演讲比赛 *****" << endl;
- cout << "***** 1、开始演讲比赛 *****" << endl;
- cout << "***** 2、查看往届记录 *****" << endl;
- cout << "***** 3、清空比赛记录 *****" << endl;
- cout << "***** 0、退出比赛程序 *****" << endl;
- cout << "******************************" << endl;
- }
- void SpeechManager::Exit() {
- cout << "欢迎下次使用" << endl;
- //system("pause");
- exit(0);
- }
- void SpeechManager::initSpeech() {
- //容器先置空
- this->v1.clear();
- this->v2.clear();
- this->vVictory.clear();
- this->m_Speaker.clear();
- this->m_index = 1;
- }
- void SpeechManager::createSpeaker() {
- string nameSeed = "ABCDEFGHIJKL";
- //for(int i = 0; i < nameSeed.size(); i++) {
- for(size_t i = 0; i < nameSeed.size(); i++) {
- string name = "选手";
- name += nameSeed[i];
- //创建选手
- Speaker sp;
- sp.m_Name = name;
- for(int j = 0; j < 2; j++) {
- sp.m_Score[j] = 0;
- }
- //创建选手编号放到v1容器中
- this->v1.push_back(i + 1001);
- //选手编号和选手信息放入map容器
- this->m_Speaker.insert(make_pair(i + 1001, sp));
- }
- }
- void SpeechManager::startSpeech() {
- //第一轮开始比赛
- //抽签
- this->speechDraw();
- //比赛
- this->speechContest();
- //显示晋级结果
- //
- //第二轮开始比赛
- //抽签
- //比赛
- //显示最终结果
- }
- void SpeechManager::speechDraw() {
- cout << "第" << this->m_index << "轮选手正在抽签" << endl;
- cout << "---------------------------------------" << endl;
- cout << "抽签后演讲顺序为" << endl;
- if(this->m_index == 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::speechContest() {
- cout << "----------第" << this->m_index << "轮比赛开始------------" << endl;
- //准备临时容器 存放小组数据
- //multimap<double, int, greater<double>()> groupScore;
- multimap<double, int, greater<double>> groupScore;
- int num = 0;
- vector<int> v_Src; //比赛选手容器
- if(this->m_index == 1) {
- v_Src = v1;
- } else {
- v_Src = v2;
- }
- for(vector<int>::iterator 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.F;
- // cout << score << " ";
- d.push_back(score);
- }
- // cout << endl;
- sort(d.begin(), d.end(), greater<int>());
- d.pop_back();
- d.pop_front();
- double sum = accumulate(d.begin(), d.end(), 0.0f);
- double avg = sum / (double)d.size();
- //将平均分放入map容器
- this->m_Speaker[*it].m_Score[this->m_index - 1] = avg;
- //打印平均分
- // cout << "编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name
- // << " 平均分:" << this->m_Speaker[*it].m_Score[this->m_index - 1] <<
- // endl; //3、这里我即便注释掉了他居然还是会输出
- groupScore.insert(make_pair(avg, *it));
- //每6人取出前三名
- if(num % 6 == 0) // 4、不知道为什么就是进不了这个循环
- {
- cout << "第" << num / 6 << "小组名次:" << endl;
- for(multimap<double, int, greater<double>>::iterator it =
- groupScore.begin();
- it != groupScore.end(); it++) {
- cout << "编号:" << it->second << " 姓名:"
- << this->m_Speaker[it->second].m_Name << " 成绩:"
- << this->m_Speaker[it->second].m_Score[this->m_index - 1]
- << endl;
- }
- }
- }
- }
- SpeechManager::~SpeechManager() {}
复制代码
main.cpp
- #include "speechManager.h"
- #include <iostream>
- #include <string>
- //using namespace std;
- using std::cin, std::cout, std::endl;
- int main() {
- SpeechManager sm;
- srand((unsigned int)time(NULL));
- /*for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it !=
- sm.m_Speaker.end(); it++)
- {
- cout << "选手编号:" << it->first << "选手姓名:"
- <<it->second.m_Name<< endl;
- }*/
- int choice = 0;
- while(true) {
- sm.showMenu();
- cout << "请输入您的选择" << endl;
- cin >> choice;
- switch(choice) {
- case 1:
- sm.startSpeech(); //开始比赛
- break;
- case 2: //查看往届记录
- break;
- case 3: //清空比赛记录
- break;
- case 0:
- sm.Exit(); //退出系统
- break;
- default:
- cout << "输入有误,请重新输入" << endl;
- //system("pause");
- //system("cls");
- system("clear");
- break;
- }
- }
- //system("pause");
- return 0;
- }
复制代码
- $ g++-debug -o main main.cpp speechManager.cpp
- $ ./main
- ******************************
- ***** 欢迎参加演讲比赛 *****
- ***** 1、开始演讲比赛 *****
- ***** 2、查看往届记录 *****
- ***** 3、清空比赛记录 *****
- ***** 0、退出比赛程序 *****
- ******************************
- 请输入您的选择
- 1
- 第1轮选手正在抽签
- ---------------------------------------
- 抽签后演讲顺序为
- 1001 1004 1003 1011 1007 1009 1010 1005 1012 1002 1006 1008
- ---------------------------------------
- ----------第1轮比赛开始------------
- 第1小组名次:
- 编号:1001 姓名:选手A 成绩:83.975
- 编号:1003 姓名:选手C 成绩:83.9625
- 编号:1011 姓名:选手K 成绩:82.875
- 编号:1009 姓名:选手I 成绩:82.875
- 编号:1007 姓名:选手G 成绩:77.725
- 编号:1004 姓名:选手D 成绩:75.85
- 第2小组名次:
- 编号:1006 姓名:选手F 成绩:90.0375
- 编号:1010 姓名:选手J 成绩:84.3625
- 编号:1001 姓名:选手A 成绩:83.975
- 编号:1003 姓名:选手C 成绩:83.9625
- 编号:1011 姓名:选手K 成绩:82.875
- 编号:1009 姓名:选手I 成绩:82.875
- 编号:1005 姓名:选手E 成绩:82.625
- 编号:1008 姓名:选手H 成绩:82.35
- 编号:1012 姓名:选手L 成绩:79.4875
- 编号:1007 姓名:选手G 成绩:77.725
- 编号:1004 姓名:选手D 成绩:75.85
- 编号:1002 姓名:选手B 成绩:75.2125
- ******************************
- ***** 欢迎参加演讲比赛 *****
- ***** 1、开始演讲比赛 *****
- ***** 2、查看往届记录 *****
- ***** 3、清空比赛记录 *****
- ***** 0、退出比赛程序 *****
- ******************************
- 请输入您的选择
- 0
- 欢迎下次使用
- $
复制代码
|
|