鱼C论坛

 找回密码
 立即注册
查看: 3654|回复: 3

[已解决]演讲比赛案例的问题

[复制链接]
发表于 2022-4-18 10:00:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 孤世星辰 于 2022-4-19 08:14 编辑

speechManager.h
  1. #pragma once
  2. #include<iostream>
  3. using namespace std;
  4. #include<string>
  5. #include<vector>
  6. #include<map>
  7. #include"speak.h"
  8. #include<algorithm>
  9. #include<deque>
  10. #include<functional>
  11. #include<numeric>
  12. #include<ctime>

  13. class SpeechManager
  14. {
  15. public:
  16.         SpeechManager();

  17.         //显示菜单
  18.         void showMenu();

  19.         //退出菜单
  20.         void Exit();

  21.         ~SpeechManager();

  22.         //初始化容器和属性
  23.         void initSpeech();

  24.         //创建12名选手
  25.         void createSpeaker();

  26.         //比赛流程函数
  27.         void startSpeech();

  28.         //抽签
  29.         void speechDraw();

  30.         //比赛
  31.         void speechContest();

  32.         //保存第一轮比赛选手编号容器
  33.         vector<int>v1;
  34.        
  35.         //保存第二轮比赛选手编号容器
  36.         vector<int>v2;
  37.        
  38.         //胜出前三名选手编号容器
  39.         vector<int>vVictory;

  40.         //存放编号及对应具体选手容器
  41.         map<int, Speaker>m_Speaker;

  42.         //比赛轮数
  43.         int m_index;

  44. };
复制代码


speak.h

  1. #pragma once
  2. #include<iostream>
  3. using namespace std;
  4. #include<string>


  5. //选手类
  6. class Speaker
  7. {
  8. public:

  9.         string m_Name;
  10.         double m_Score[2];//最多两轮得分
  11. };
复制代码


演讲比赛流程管理系统.cpp

  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #include"speechManager.h"

  5. int main()
  6. {
  7.         SpeechManager sm;

  8.         srand((unsigned int)time(NULL));
  9.         /*for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++)
  10.         {
  11.                 cout << "选手编号:" << it->first << "选手姓名:" <<it->second.m_Name<< endl;
  12.         }*/

  13.         int choice = 0;

  14.         while (true)
  15.         {
  16.                 sm.showMenu();

  17.                 cout << "请输入您的选择" << endl;
  18.                 cin >> choice;

  19.                 switch (choice)
  20.                 {
  21.                 case 1:sm.startSpeech();//开始比赛
  22.                         break;
  23.                 case 2://查看往届记录
  24.                         break;
  25.                 case 3://清空比赛记录
  26.                         break;
  27.                 case 0:sm.Exit();//退出系统
  28.                         break;
  29.                 default:
  30.                         cout << "输入有误,请重新输入" << endl;
  31.                         system("pause");
  32.                         system("cls");
  33.                         break;
  34.                 }

  35.         }



  36.         system("pause");

  37.         return 0;
  38. }
复制代码


speechManager.cpp
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #include"speechManager.h"



  5. SpeechManager:: SpeechManager()
  6. {
  7.         this->initSpeech();
  8.         this->createSpeaker();
  9. }


  10. void SpeechManager::showMenu()
  11. {
  12.         cout << "******************************" << endl;
  13.         cout << "*****  欢迎参加演讲比赛  *****" << endl;
  14.         cout << "*****  1、开始演讲比赛   *****" << endl;
  15.         cout << "*****  2、查看往届记录   *****" << endl;
  16.         cout << "*****  3、清空比赛记录   *****" << endl;
  17.         cout << "*****  0、退出比赛程序   *****" << endl;
  18.         cout << "******************************" << endl;
  19. }

  20. void SpeechManager::Exit()
  21. {
  22.         cout << "欢迎下次使用" << endl;
  23.         system("pause");
  24.         exit(0);
  25.        
  26. }

  27. void SpeechManager::initSpeech()
  28. {
  29.         //容器先置空
  30.         this->v1.clear();
  31.         this->v2.clear();
  32.         this->vVictory.clear();
  33.         this->m_Speaker.clear();

  34.         this->m_index = 1;
  35. }

  36. void SpeechManager::createSpeaker()
  37. {
  38.         string nameSeed = "ABCDEFGHIJKL";

  39.         for (int i = 0; i < nameSeed.size(); i++)
  40.         {
  41.                 string name = "选手";
  42.                 name += nameSeed[i];

  43.                 //创建选手
  44.                 Speaker sp;
  45.                 sp.m_Name = name;

  46.                 for (int j = 0; j < 2; j++)
  47.                 {
  48.                         sp.m_Score[j] = 0;
  49.                 }

  50.                 //创建选手编号放到v1容器中
  51.                 this->v1.push_back(i + 1001);

  52.                 //选手编号和选手信息放入map容器
  53.                 this->m_Speaker.insert(make_pair(i + 1001, sp));
  54.         }
  55. }

  56. void SpeechManager::startSpeech()
  57. {
  58.         //第一轮开始比赛

  59.         //抽签
  60.         this->speechDraw();

  61.         //比赛
  62.         this->speechContest();
  63.         //显示晋级结果
  64.         //
  65.         //第二轮开始比赛

  66.         //抽签

  67.         //比赛

  68.         //显示最终结果


  69.        
  70. }

  71. void SpeechManager::speechDraw()
  72. {
  73.         cout << "第" << this->m_index << "轮选手正在抽签" << endl;
  74.         cout << "---------------------------------------" << endl;
  75.         cout << "抽签后演讲顺序为" << endl;

  76.         if (this->m_index == 1)
  77.         {       
  78.                 //第一轮抽签
  79.                 random_shuffle(v1.begin(), v1.end());
  80.                 for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
  81.                 {
  82.                         cout << *it << " ";
  83.                 }
  84.                 cout << endl;
  85.         }
  86.         else
  87.         {
  88.                 //第二轮抽签
  89.                 random_shuffle(v2.begin(), v2.end());
  90.                 for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
  91.                 {
  92.                         cout << *it << " ";
  93.                 }
  94.                 cout << endl;
  95.         }
  96.         cout << "---------------------------------------" << endl;
  97.         system("pause");
  98.         cout << endl;

  99. }

  100. void SpeechManager::speechContest()
  101. {
  102.         cout << "----------第" << this->m_index << "轮比赛开始------------" << endl;

  103.         //准备临时容器 存放小组数据
  104.         multimap<double, int, greater<double>()>groupScore;

  105.         int num = 0;

  106.         vector<int>v_Src;//比赛选手容器

  107.         if (this->m_index == 1)
  108.         {
  109.                 v_Src = v1;
  110.         }
  111.         else
  112.         {
  113.                 v_Src = v2;
  114.         }

  115.         for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it ++ )
  116.         {

  117.                 num++;
  118.                 //评委打分
  119.                 deque<double>d;
  120.                 for (int i = 0; i < 10; i++)
  121.                 {
  122.                         double score = (rand() % 401 + 600) / 10.F;
  123.                         //cout << score << " ";
  124.                         d.push_back(score);
  125.                 }
  126.                 //cout << endl;

  127.                 sort(d.begin(), d.end(), greater<int>());

  128.                 d.pop_back();
  129.                 d.pop_front();

  130.                 double sum = accumulate(d.begin(), d.end(),0.0f);

  131.                 double avg = sum / (double)d.size();

  132.                 //将平均分放入map容器
  133.                 this->m_Speaker[*it].m_Score[this->m_index-1] = avg;

  134.                 //打印平均分
  135.                 //cout << "编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name << " 平均分:" << this->m_Speaker[*it].m_Score[this->m_index - 1] << endl;           //3、这里我即便注释掉了他居然还是会输出
  136.                
  137.                 groupScore.insert(make_pair(avg, *it));
  138.                
  139.                 //每6人取出前三名
  140.                 if (num % 6 == 0)             //4、不知道为什么就是进不了这个循环
  141.                 {
  142.                         cout << "第" << num / 6 << "小组名次:" << endl;
  143.                         for (multimap<double, int, greater<double>>::iterator it= groupScore.begin(); it != groupScore.end(); it++)
  144.                         {
  145.                                 cout << "编号:" << it->second << " 姓名:" << this->m_Speaker[it->second].m_Name
  146.                                         << " 成绩:" << this->m_Speaker[it->second].m_Score[this->m_index - 1] << endl;
  147.                         }
  148.                 }



  149.         }

  150. }



  151. SpeechManager::~SpeechManager()
  152. {

  153. }
复制代码


F0S34B3B9)4YZ@LU[J9ONKB.png

1、我这个代码一开始是用c最新语法标准敲得,然后因为要用random_shuffle就又改了c14标准,然后我debug就感觉有很多问题,比如这个num为什么调试不出来

还有就是
XOOUE%Y%TR@C3%YO@KS17.png

XOOUE%Y%TR@C3%YO@KS17.png

2、调试到这里他就又会跳回for上面一行,然后要这样循环很多下才能继续进行


5、因为语法不一致,因此我打的断点没有用,然后我是点断点设置--位置--允许源码不一致,但是有时候会这样
3.png

最佳答案
2022-4-19 18:25:03
没看懂你的问题
“这个num为什么调试不出来”
num怎么了?

1. 你在.h头文件里面包含了好多头文件,但是都没有使用
2. 不建议用 using namespace std;
会出现名字冲突的问题,我已经遇到好多次了
3.
  1.     //multimap<double, int, greater<double>()> groupScore;
  2.     multimap<double, int, greater<double>> groupScore;
复制代码


这行代码不对吧?


speak.h
  1. #ifndef _SPEAK_H_
  2. #define _SPEAK_H_

  3. //#pragma once
  4. //#include <iostream>
  5. #include <string>

  6. //using namespace std;
  7. using std::string;

  8. //选手类
  9. class Speaker {
  10. public:
  11.     string m_Name;
  12.     double m_Score[2]; //最多两轮得分
  13. };

  14. #endif
复制代码


speechManager.h
  1. #ifndef _SPEECHMANAGER_H_
  2. #define _SPEECHMANAGER_H_

  3. //#pragma once
  4. //#include <iostream>
  5. #include "speak.h"
  6. //#include <algorithm>
  7. //#include <ctime>
  8. //#include <deque>
  9. //#include <functional>
  10. #include <map>
  11. //#include <numeric>
  12. //#include <string>
  13. #include <vector>

  14. //using namespace std;
  15. using std::map, std::vector;

  16. class SpeechManager {
  17. public:
  18.     SpeechManager();
  19.     ~SpeechManager();

  20.     //显示菜单
  21.     void showMenu();

  22.     //退出菜单
  23.     void Exit();

  24.     //初始化容器和属性
  25.     void initSpeech();

  26.     //创建12名选手
  27.     void createSpeaker();

  28.     //比赛流程函数
  29.     void startSpeech();

  30.     //抽签
  31.     void speechDraw();

  32.     //比赛
  33.     void speechContest();

  34.     //保存第一轮比赛选手编号容器
  35.     vector<int> v1;

  36.     //保存第二轮比赛选手编号容器
  37.     vector<int> v2;

  38.     //胜出前三名选手编号容器
  39.     vector<int> vVictory;

  40.     //存放编号及对应具体选手容器
  41.     map<int, Speaker> m_Speaker;

  42.     //比赛轮数
  43.     int m_index;
  44. };

  45. #endif
复制代码


speechManager.cpp
  1. #include "speechManager.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <deque>
  6. #include <numeric>

  7. //using namespace std;
  8. using std::cout, std::endl;
  9. using std::make_pair;
  10. using std::multimap, std::greater;
  11. using std::deque;

  12. SpeechManager::SpeechManager() {
  13.     this->initSpeech();
  14.     this->createSpeaker();
  15. }

  16. void SpeechManager::showMenu() {
  17.     cout << "******************************" << endl;
  18.     cout << "*****  欢迎参加演讲比赛  *****" << endl;
  19.     cout << "*****  1、开始演讲比赛   *****" << endl;
  20.     cout << "*****  2、查看往届记录   *****" << endl;
  21.     cout << "*****  3、清空比赛记录   *****" << endl;
  22.     cout << "*****  0、退出比赛程序   *****" << endl;
  23.     cout << "******************************" << endl;
  24. }

  25. void SpeechManager::Exit() {
  26.     cout << "欢迎下次使用" << endl;
  27.     //system("pause");
  28.     exit(0);
  29. }

  30. void SpeechManager::initSpeech() {
  31.     //容器先置空
  32.     this->v1.clear();
  33.     this->v2.clear();
  34.     this->vVictory.clear();
  35.     this->m_Speaker.clear();

  36.     this->m_index = 1;
  37. }

  38. void SpeechManager::createSpeaker() {
  39.     string nameSeed = "ABCDEFGHIJKL";

  40.     //for(int i = 0; i < nameSeed.size(); i++) {
  41.     for(size_t i = 0; i < nameSeed.size(); i++) {
  42.         string name = "选手";
  43.         name += nameSeed[i];

  44.         //创建选手
  45.         Speaker sp;
  46.         sp.m_Name = name;

  47.         for(int j = 0; j < 2; j++) {
  48.             sp.m_Score[j] = 0;
  49.         }

  50.         //创建选手编号放到v1容器中
  51.         this->v1.push_back(i + 1001);

  52.         //选手编号和选手信息放入map容器
  53.         this->m_Speaker.insert(make_pair(i + 1001, sp));
  54.     }
  55. }

  56. void SpeechManager::startSpeech() {
  57.     //第一轮开始比赛

  58.     //抽签
  59.     this->speechDraw();

  60.     //比赛
  61.     this->speechContest();
  62.     //显示晋级结果
  63.     //
  64.     //第二轮开始比赛

  65.     //抽签

  66.     //比赛

  67.     //显示最终结果
  68. }

  69. void SpeechManager::speechDraw() {
  70.     cout << "第" << this->m_index << "轮选手正在抽签" << endl;
  71.     cout << "---------------------------------------" << endl;
  72.     cout << "抽签后演讲顺序为" << endl;

  73.     if(this->m_index == 1) {
  74.         //第一轮抽签
  75.         random_shuffle(v1.begin(), v1.end());
  76.         for(vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
  77.             cout << *it << " ";
  78.         }
  79.         cout << endl;
  80.     } else {
  81.         //第二轮抽签
  82.         random_shuffle(v2.begin(), v2.end());
  83.         for(vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
  84.             cout << *it << " ";
  85.         }
  86.         cout << endl;
  87.     }
  88.     cout << "---------------------------------------" << endl;
  89.     //system("pause");
  90.     cout << endl;
  91. }

  92. void SpeechManager::speechContest() {
  93.     cout << "----------第" << this->m_index << "轮比赛开始------------" << endl;

  94.     //准备临时容器 存放小组数据
  95.     //multimap<double, int, greater<double>()> groupScore;
  96.     multimap<double, int, greater<double>> groupScore;

  97.     int num = 0;

  98.     vector<int> v_Src; //比赛选手容器

  99.     if(this->m_index == 1) {
  100.         v_Src = v1;
  101.     } else {
  102.         v_Src = v2;
  103.     }

  104.     for(vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {

  105.         num++;
  106.         //评委打分
  107.         deque<double> d;
  108.         for(int i = 0; i < 10; i++) {
  109.             double score = (rand() % 401 + 600) / 10.F;
  110.             // cout << score << " ";
  111.             d.push_back(score);
  112.         }
  113.         // cout << endl;

  114.         sort(d.begin(), d.end(), greater<int>());

  115.         d.pop_back();
  116.         d.pop_front();

  117.         double sum = accumulate(d.begin(), d.end(), 0.0f);

  118.         double avg = sum / (double)d.size();

  119.         //将平均分放入map容器
  120.         this->m_Speaker[*it].m_Score[this->m_index - 1] = avg;

  121.         //打印平均分
  122.         // cout << "编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name
  123.         // << " 平均分:" << this->m_Speaker[*it].m_Score[this->m_index - 1] <<
  124.         // endl;           //3、这里我即便注释掉了他居然还是会输出

  125.         groupScore.insert(make_pair(avg, *it));

  126.         //每6人取出前三名
  127.         if(num % 6 == 0) // 4、不知道为什么就是进不了这个循环
  128.         {
  129.             cout << "第" << num / 6 << "小组名次:" << endl;
  130.             for(multimap<double, int, greater<double>>::iterator it =
  131.                     groupScore.begin();
  132.                 it != groupScore.end(); it++) {
  133.                 cout << "编号:" << it->second << " 姓名:"
  134.                      << this->m_Speaker[it->second].m_Name << " 成绩:"
  135.                      << this->m_Speaker[it->second].m_Score[this->m_index - 1]
  136.                      << endl;
  137.             }
  138.         }
  139.     }
  140. }

  141. SpeechManager::~SpeechManager() {}
复制代码


main.cpp
  1. #include "speechManager.h"
  2. #include <iostream>
  3. #include <string>

  4. //using namespace std;
  5. using std::cin, std::cout, std::endl;

  6. int main() {
  7.     SpeechManager sm;

  8.     srand((unsigned int)time(NULL));
  9.     /*for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it !=
  10.     sm.m_Speaker.end(); it++)
  11.     {
  12.             cout << "选手编号:" << it->first << "选手姓名:"
  13.     <<it->second.m_Name<< endl;
  14.     }*/

  15.     int choice = 0;

  16.     while(true) {
  17.         sm.showMenu();

  18.         cout << "请输入您的选择" << endl;
  19.         cin >> choice;

  20.         switch(choice) {
  21.         case 1:
  22.             sm.startSpeech(); //开始比赛
  23.             break;
  24.         case 2: //查看往届记录
  25.             break;
  26.         case 3: //清空比赛记录
  27.             break;
  28.         case 0:
  29.             sm.Exit(); //退出系统
  30.             break;
  31.         default:
  32.             cout << "输入有误,请重新输入" << endl;
  33.             //system("pause");
  34.             //system("cls");
  35.             system("clear");
  36.             break;
  37.         }
  38.     }

  39.     //system("pause");

  40.     return 0;
  41. }
复制代码

  1. $ g++-debug -o main main.cpp speechManager.cpp
  2. $ ./main
  3. ******************************
  4. *****  欢迎参加演讲比赛  *****
  5. *****  1、开始演讲比赛   *****
  6. *****  2、查看往届记录   *****
  7. *****  3、清空比赛记录   *****
  8. *****  0、退出比赛程序   *****
  9. ******************************
  10. 请输入您的选择
  11. 1
  12. 第1轮选手正在抽签
  13. ---------------------------------------
  14. 抽签后演讲顺序为
  15. 1001 1004 1003 1011 1007 1009 1010 1005 1012 1002 1006 1008
  16. ---------------------------------------

  17. ----------第1轮比赛开始------------
  18. 第1小组名次:
  19. 编号:1001 姓名:选手A 成绩:83.975
  20. 编号:1003 姓名:选手C 成绩:83.9625
  21. 编号:1011 姓名:选手K 成绩:82.875
  22. 编号:1009 姓名:选手I 成绩:82.875
  23. 编号:1007 姓名:选手G 成绩:77.725
  24. 编号:1004 姓名:选手D 成绩:75.85
  25. 第2小组名次:
  26. 编号:1006 姓名:选手F 成绩:90.0375
  27. 编号:1010 姓名:选手J 成绩:84.3625
  28. 编号:1001 姓名:选手A 成绩:83.975
  29. 编号:1003 姓名:选手C 成绩:83.9625
  30. 编号:1011 姓名:选手K 成绩:82.875
  31. 编号:1009 姓名:选手I 成绩:82.875
  32. 编号:1005 姓名:选手E 成绩:82.625
  33. 编号:1008 姓名:选手H 成绩:82.35
  34. 编号:1012 姓名:选手L 成绩:79.4875
  35. 编号:1007 姓名:选手G 成绩:77.725
  36. 编号:1004 姓名:选手D 成绩:75.85
  37. 编号:1002 姓名:选手B 成绩:75.2125
  38. ******************************
  39. *****  欢迎参加演讲比赛  *****
  40. *****  1、开始演讲比赛   *****
  41. *****  2、查看往届记录   *****
  42. *****  3、清空比赛记录   *****
  43. *****  0、退出比赛程序   *****
  44. ******************************
  45. 请输入您的选择
  46. 0
  47. 欢迎下次使用
  48. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-19 12:50:49 | 显示全部楼层
没有人嘛,顶贴有用嘛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-19 18:25:03 | 显示全部楼层    本楼为最佳答案   
没看懂你的问题
“这个num为什么调试不出来”
num怎么了?

1. 你在.h头文件里面包含了好多头文件,但是都没有使用
2. 不建议用 using namespace std;
会出现名字冲突的问题,我已经遇到好多次了
3.
  1.     //multimap<double, int, greater<double>()> groupScore;
  2.     multimap<double, int, greater<double>> groupScore;
复制代码


这行代码不对吧?


speak.h
  1. #ifndef _SPEAK_H_
  2. #define _SPEAK_H_

  3. //#pragma once
  4. //#include <iostream>
  5. #include <string>

  6. //using namespace std;
  7. using std::string;

  8. //选手类
  9. class Speaker {
  10. public:
  11.     string m_Name;
  12.     double m_Score[2]; //最多两轮得分
  13. };

  14. #endif
复制代码


speechManager.h
  1. #ifndef _SPEECHMANAGER_H_
  2. #define _SPEECHMANAGER_H_

  3. //#pragma once
  4. //#include <iostream>
  5. #include "speak.h"
  6. //#include <algorithm>
  7. //#include <ctime>
  8. //#include <deque>
  9. //#include <functional>
  10. #include <map>
  11. //#include <numeric>
  12. //#include <string>
  13. #include <vector>

  14. //using namespace std;
  15. using std::map, std::vector;

  16. class SpeechManager {
  17. public:
  18.     SpeechManager();
  19.     ~SpeechManager();

  20.     //显示菜单
  21.     void showMenu();

  22.     //退出菜单
  23.     void Exit();

  24.     //初始化容器和属性
  25.     void initSpeech();

  26.     //创建12名选手
  27.     void createSpeaker();

  28.     //比赛流程函数
  29.     void startSpeech();

  30.     //抽签
  31.     void speechDraw();

  32.     //比赛
  33.     void speechContest();

  34.     //保存第一轮比赛选手编号容器
  35.     vector<int> v1;

  36.     //保存第二轮比赛选手编号容器
  37.     vector<int> v2;

  38.     //胜出前三名选手编号容器
  39.     vector<int> vVictory;

  40.     //存放编号及对应具体选手容器
  41.     map<int, Speaker> m_Speaker;

  42.     //比赛轮数
  43.     int m_index;
  44. };

  45. #endif
复制代码


speechManager.cpp
  1. #include "speechManager.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <deque>
  6. #include <numeric>

  7. //using namespace std;
  8. using std::cout, std::endl;
  9. using std::make_pair;
  10. using std::multimap, std::greater;
  11. using std::deque;

  12. SpeechManager::SpeechManager() {
  13.     this->initSpeech();
  14.     this->createSpeaker();
  15. }

  16. void SpeechManager::showMenu() {
  17.     cout << "******************************" << endl;
  18.     cout << "*****  欢迎参加演讲比赛  *****" << endl;
  19.     cout << "*****  1、开始演讲比赛   *****" << endl;
  20.     cout << "*****  2、查看往届记录   *****" << endl;
  21.     cout << "*****  3、清空比赛记录   *****" << endl;
  22.     cout << "*****  0、退出比赛程序   *****" << endl;
  23.     cout << "******************************" << endl;
  24. }

  25. void SpeechManager::Exit() {
  26.     cout << "欢迎下次使用" << endl;
  27.     //system("pause");
  28.     exit(0);
  29. }

  30. void SpeechManager::initSpeech() {
  31.     //容器先置空
  32.     this->v1.clear();
  33.     this->v2.clear();
  34.     this->vVictory.clear();
  35.     this->m_Speaker.clear();

  36.     this->m_index = 1;
  37. }

  38. void SpeechManager::createSpeaker() {
  39.     string nameSeed = "ABCDEFGHIJKL";

  40.     //for(int i = 0; i < nameSeed.size(); i++) {
  41.     for(size_t i = 0; i < nameSeed.size(); i++) {
  42.         string name = "选手";
  43.         name += nameSeed[i];

  44.         //创建选手
  45.         Speaker sp;
  46.         sp.m_Name = name;

  47.         for(int j = 0; j < 2; j++) {
  48.             sp.m_Score[j] = 0;
  49.         }

  50.         //创建选手编号放到v1容器中
  51.         this->v1.push_back(i + 1001);

  52.         //选手编号和选手信息放入map容器
  53.         this->m_Speaker.insert(make_pair(i + 1001, sp));
  54.     }
  55. }

  56. void SpeechManager::startSpeech() {
  57.     //第一轮开始比赛

  58.     //抽签
  59.     this->speechDraw();

  60.     //比赛
  61.     this->speechContest();
  62.     //显示晋级结果
  63.     //
  64.     //第二轮开始比赛

  65.     //抽签

  66.     //比赛

  67.     //显示最终结果
  68. }

  69. void SpeechManager::speechDraw() {
  70.     cout << "第" << this->m_index << "轮选手正在抽签" << endl;
  71.     cout << "---------------------------------------" << endl;
  72.     cout << "抽签后演讲顺序为" << endl;

  73.     if(this->m_index == 1) {
  74.         //第一轮抽签
  75.         random_shuffle(v1.begin(), v1.end());
  76.         for(vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
  77.             cout << *it << " ";
  78.         }
  79.         cout << endl;
  80.     } else {
  81.         //第二轮抽签
  82.         random_shuffle(v2.begin(), v2.end());
  83.         for(vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
  84.             cout << *it << " ";
  85.         }
  86.         cout << endl;
  87.     }
  88.     cout << "---------------------------------------" << endl;
  89.     //system("pause");
  90.     cout << endl;
  91. }

  92. void SpeechManager::speechContest() {
  93.     cout << "----------第" << this->m_index << "轮比赛开始------------" << endl;

  94.     //准备临时容器 存放小组数据
  95.     //multimap<double, int, greater<double>()> groupScore;
  96.     multimap<double, int, greater<double>> groupScore;

  97.     int num = 0;

  98.     vector<int> v_Src; //比赛选手容器

  99.     if(this->m_index == 1) {
  100.         v_Src = v1;
  101.     } else {
  102.         v_Src = v2;
  103.     }

  104.     for(vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++) {

  105.         num++;
  106.         //评委打分
  107.         deque<double> d;
  108.         for(int i = 0; i < 10; i++) {
  109.             double score = (rand() % 401 + 600) / 10.F;
  110.             // cout << score << " ";
  111.             d.push_back(score);
  112.         }
  113.         // cout << endl;

  114.         sort(d.begin(), d.end(), greater<int>());

  115.         d.pop_back();
  116.         d.pop_front();

  117.         double sum = accumulate(d.begin(), d.end(), 0.0f);

  118.         double avg = sum / (double)d.size();

  119.         //将平均分放入map容器
  120.         this->m_Speaker[*it].m_Score[this->m_index - 1] = avg;

  121.         //打印平均分
  122.         // cout << "编号:" << *it << " 姓名:" << this->m_Speaker[*it].m_Name
  123.         // << " 平均分:" << this->m_Speaker[*it].m_Score[this->m_index - 1] <<
  124.         // endl;           //3、这里我即便注释掉了他居然还是会输出

  125.         groupScore.insert(make_pair(avg, *it));

  126.         //每6人取出前三名
  127.         if(num % 6 == 0) // 4、不知道为什么就是进不了这个循环
  128.         {
  129.             cout << "第" << num / 6 << "小组名次:" << endl;
  130.             for(multimap<double, int, greater<double>>::iterator it =
  131.                     groupScore.begin();
  132.                 it != groupScore.end(); it++) {
  133.                 cout << "编号:" << it->second << " 姓名:"
  134.                      << this->m_Speaker[it->second].m_Name << " 成绩:"
  135.                      << this->m_Speaker[it->second].m_Score[this->m_index - 1]
  136.                      << endl;
  137.             }
  138.         }
  139.     }
  140. }

  141. SpeechManager::~SpeechManager() {}
复制代码


main.cpp
  1. #include "speechManager.h"
  2. #include <iostream>
  3. #include <string>

  4. //using namespace std;
  5. using std::cin, std::cout, std::endl;

  6. int main() {
  7.     SpeechManager sm;

  8.     srand((unsigned int)time(NULL));
  9.     /*for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it !=
  10.     sm.m_Speaker.end(); it++)
  11.     {
  12.             cout << "选手编号:" << it->first << "选手姓名:"
  13.     <<it->second.m_Name<< endl;
  14.     }*/

  15.     int choice = 0;

  16.     while(true) {
  17.         sm.showMenu();

  18.         cout << "请输入您的选择" << endl;
  19.         cin >> choice;

  20.         switch(choice) {
  21.         case 1:
  22.             sm.startSpeech(); //开始比赛
  23.             break;
  24.         case 2: //查看往届记录
  25.             break;
  26.         case 3: //清空比赛记录
  27.             break;
  28.         case 0:
  29.             sm.Exit(); //退出系统
  30.             break;
  31.         default:
  32.             cout << "输入有误,请重新输入" << endl;
  33.             //system("pause");
  34.             //system("cls");
  35.             system("clear");
  36.             break;
  37.         }
  38.     }

  39.     //system("pause");

  40.     return 0;
  41. }
复制代码

  1. $ g++-debug -o main main.cpp speechManager.cpp
  2. $ ./main
  3. ******************************
  4. *****  欢迎参加演讲比赛  *****
  5. *****  1、开始演讲比赛   *****
  6. *****  2、查看往届记录   *****
  7. *****  3、清空比赛记录   *****
  8. *****  0、退出比赛程序   *****
  9. ******************************
  10. 请输入您的选择
  11. 1
  12. 第1轮选手正在抽签
  13. ---------------------------------------
  14. 抽签后演讲顺序为
  15. 1001 1004 1003 1011 1007 1009 1010 1005 1012 1002 1006 1008
  16. ---------------------------------------

  17. ----------第1轮比赛开始------------
  18. 第1小组名次:
  19. 编号:1001 姓名:选手A 成绩:83.975
  20. 编号:1003 姓名:选手C 成绩:83.9625
  21. 编号:1011 姓名:选手K 成绩:82.875
  22. 编号:1009 姓名:选手I 成绩:82.875
  23. 编号:1007 姓名:选手G 成绩:77.725
  24. 编号:1004 姓名:选手D 成绩:75.85
  25. 第2小组名次:
  26. 编号:1006 姓名:选手F 成绩:90.0375
  27. 编号:1010 姓名:选手J 成绩:84.3625
  28. 编号:1001 姓名:选手A 成绩:83.975
  29. 编号:1003 姓名:选手C 成绩:83.9625
  30. 编号:1011 姓名:选手K 成绩:82.875
  31. 编号:1009 姓名:选手I 成绩:82.875
  32. 编号:1005 姓名:选手E 成绩:82.625
  33. 编号:1008 姓名:选手H 成绩:82.35
  34. 编号:1012 姓名:选手L 成绩:79.4875
  35. 编号:1007 姓名:选手G 成绩:77.725
  36. 编号:1004 姓名:选手D 成绩:75.85
  37. 编号:1002 姓名:选手B 成绩:75.2125
  38. ******************************
  39. *****  欢迎参加演讲比赛  *****
  40. *****  1、开始演讲比赛   *****
  41. *****  2、查看往届记录   *****
  42. *****  3、清空比赛记录   *****
  43. *****  0、退出比赛程序   *****
  44. ******************************
  45. 请输入您的选择
  46. 0
  47. 欢迎下次使用
  48. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-20 11:12:23 | 显示全部楼层
人造人 发表于 2022-4-19 18:25
没看懂你的问题
“这个num为什么调试不出来”
num怎么了?

谢谢老师,我之前也用了std::的形式后来又遇到点问题,变成using namespace std,就不报错了就又改了,以后我一定用std::的形式吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 12:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表