欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

C++ Notes 打卡日 24(演讲比赛流程管理系统)

最编程 2024-04-29 09:37:01
...

1.主函数

#include<iostream>
#include<string>
#include<speechManager.h>
using namespace std;

int main()
{
    SpeechManager sm;
    int choice = 0;
    
    while(true)
    {
        sm.show_Menu();
        cout << "请输入您的选择:" << endl;
        cin >> choice;
        switch(choice)
        {
        case 1:
            sm.SpeechDraw();
            break;
        case 2:
            break;
        case 3:
            break;
        case 0:
            sm.exitSystem();
            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.h中写声明:

    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.选择为0时退出系统

在Speaker.h中写声明。

    void exitSystem();

SpeechManager.cpp中写实现:

void SpeechManager::exitSystem()
{
    cout << "欢迎下次使用" << endl;
    system("pause");
    exit(0);
}

5.创建选手类

在Speaker.h中写声明。一个属性是姓名,另一个属性是存放两轮得分的数组:

#pragma once
#include<iostream>
using namespace std;

class Speaker
{
public:
    Speaker();
    string m_name;
    double m_score[2];  // 最多有两轮得分
};

6.比赛开始前:随机进行抽签

在SpeechManager.h中写声明。建立三个数组,v1存放第一轮选手的编号,v2存放第二轮选手的编号,v存放获胜的前三名编号。以及创建对组map<int, Speaker> m_Speaker存放编号和对应的选手。

    vector<int> v1;  // 保存第一轮选手编号
    vector<int> v2;  // 第一轮晋级选手编号
    vector<int> v;  // 前三名选手编号
    map<int, Speaker> m_Speaker;  // 存放编号以及对应具体选手
    int m_Index;  // 比赛轮数

    void initSpeech();  // 初始化
    void createSpeaker();  // 创建十二名选手
    void SpeechDraw();  // 抽签
void SpeechManager::initSpeech()
{
    // 容器都置空
    this->v1.clear();
    this->v2.clear();
    this->v.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+10001);
        // 将编号以及演讲者添加到map容器中
        this->m_Speaker.insert(make_pair(i+10001, sp));
    }
}

将初始化和创造12名选手的函数添加到构造函数中:

SpeechManager::SpeechManager()
{
    this->initSpeech();  // 初始化
    this->createSpeaker();  // 创建十二名选手
}

进行抽签。在Speaker.h中写声明。:

 #include<algorithm>

    void SpeechDraw();  // 抽签

SpeechManager.cpp中写实现。如果是第一轮,用v1。如果是第二轮,用v2:

void SpeechManager::SpeechDraw()
{
    cout << "第" << this->m_Index << "轮开始抽签" << endl;
    vector<int> v_Src;
    if(this->m_Index == 1)
    {
        v_Src = v1;
    }
    else
    {
        v_Src = v2; 
    }
    random_shuffle(v_Src.begin(), v_Src.end());
    cout << "随机抽签的顺序如下:" << endl;
    for(vector<int>::iterator it=v_Src.begin(); it != v_Src.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    cout << "------------------------" << endl;
    system("pause");
    cout << endl;
}

7.比赛开始

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

    // 准备临时容器,存放小组成绩
    multimap<double, int, greater<double>> groupScore;
    int num = 0; // 用于记录人员个数 6人一组


    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> q;
        for(int i=0; i<10; i++)
        {
            double score = (rand() % 401 + 600) / 10.f; // 600~1000
            // cout << score << " ";
            q.push_back(score);
        }
        // cout << endl;
        sort(q.begin(), q.end(), greater<double>());
        q.pop_front();  // 去除最高分
        q.pop_back();  // 去除最低分

        double sum = accumulate(q.begin(), q.end(), 0.0f);
        double avg = sum / (double)q.size();
        // cout << "编号:" << *it << " 选手姓名:" << this->m_Speaker[*it].m_name << " 均分:" << avg << endl;

        // 将平均分放入map容器, *it为选手编号
        this->m_Speaker[*it].m_score[this->m_Index-1] = avg;

        groupScore.insert(make_pair(avg, *it));  // key是得分,value是具体选手的编号
        // 每6人取出前三名
        if(num % 6 == 0)
        {
            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;
            }

            // 取走前三名
            int count = 0;
            for(multimap<double, int, greater<double>>::iterator it=groupScore.begin(); it!=groupScore.end() && count < 3; it++, count++)
            {
                if(this->m_Index == 1)
                {
                    v2.push_back((*it).second);
                }
                else
                {
                    v.push_back((*it).second);
                }
            }
            groupScore.clear();
            cout << endl;
        }
    }
    cout << "------------第" << this->m_Index << "轮比赛完毕----------";
    cout << endl;
    system("pause");
}

8.比赛结束后:展示晋级结果

void speechManager::showScore()
{
    cout << "第" << this->m_Index << "轮比赛晋级选手如下:" << endl;
    vector<int> v_Src;
    if(this->m_Index == 1)
    {
        v_Src = v2;
    }
    else
    {
        v_Src = v;
    }
    for(vector<int>::iterator it=v_Src.begin(); it!=v_Src.end(); it++)
    {
        cout << "选手编号:" << *it 
        << " 选手姓名:" << this->m_Speaker[*it].m_name 
        << " 成绩:" << this->m_Speaker[*it].m_score[this->m_Index-1]
        << endl;
    }
    cout << endl;
    system("pause");
    system("cls");
    this->show_menu();
}

9.整个比赛流程:

抽签->开始比赛->展示比赛结果->展示晋级结果

void speechManager::startSpeech()
{
    // 第一轮比赛
    // 1.抽签
    this->SpeechDraw();
    // 2.比赛
    this->speechContest();
    // 3.显示晋级结果
    this->showScore();
    // 第二轮比赛
    this->m_Index++;
    // 1.抽签
    this->SpeechDraw();
    // 2.比赛
    this->speechContest(); 
    // 3.显示晋级结果
    this->showScore();
    // 保存分数到文件中
    this->saveRecord();
}

比赛结束后需要重置比赛: 

    // 重置比赛,获取记录
    this->initSpeech();
    this->createSpeaker();
    this->loadRecord();  // 加载往届记录

    cout << "本届比赛完毕!" << endl;
    system("pause");
    system("cls");

9.保存比赛记录

将比赛记录保存在“speech.csv”文件中。同时设置一个变量fileIsEmpty,记录文件是否为空,便于后续读取的条件判断。

void speechManager::saveRecord()
{
    ofstream ofs;
    ofs.open("speech.csv", ios::out | ios::app);  // 以追加的方式写文件
    for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
    {
        ofs << *it << "," << this->m_Speaker[*it].m_score[1] << ",";
    }
    ofs << endl;
    ofs.close();
    cout << "记录已经保存" << endl;
    this->fileIsEmpty = false;
}

10.加载往届记录

在SpeechManager.h中写声明:

    void loadRecord();
    bool fileIsEmpty;  // 文件是否为空
    map<int, vector<string>> m_Record;  // 存放往届记录的容器

在SpeechManager.cpp中写实现。将读取的记录都存放在m_Record:

void speechManager::loadRecord()
{
    ifstream ifs("speech.csv", ios::in);  // 读文件
    if(!ifs.is_open())
    {
        this->fileIsEmpty = true;
        cout << "文件不存在" << endl;
        ifs.close();
        return;
    }

    // 文件清空
    char ch;
    ifs >> ch;
    if(ifs.eof())
    {
        cout << "文件为空" << endl;
        ifs.close();
        return;
    }

    // 文件不为空
    this->fileIsEmpty = false;
    ifs.putback(ch);  // 将上面读取的单个字符放回来
    string data;
    int index = 0;
    while(ifs >> data)
    {
        vector<string> v_Src;
        int pos = -1; // 查到逗号的位置
        int start_pos = 0;
        while(true)
        {
            pos = data.find(",", start_pos);
            if(pos == -1)
            {
                // 没找到
                break;
            }
            string temp = data.substr(start_pos, pos-start_pos);
            v_Src.push_back(temp);
            start_pos = pos + 1;
        }
        this->m_Record.insert(make_pair(index, v_Src));
        index++;
    }
    ifs.close();
    //for(map<int, vector<string>>::iterator it=m_Record.begin(); it!=m_Record.end(); it++)
    //{
        //cout << "第" << it->first+1 << "届" << " 冠军编号:" << it->second[0] << " 分数:" << it->second[1] << endl;
    //}
}

11.展示往届记录

void speechManager::showRecord()
{
    if(this->fileIsEmpty)
    {
        cout << "文件不存在,记录为空" << endl;
    }
    else
    {
        for(int i=0; i<this->m_Record.size(); i++)
        {
            cout << "第" << i+1 << "届" << " "
            << "冠军编号:" << this->m_Record[i][0] << " 得分:" << this->m_Record[i][1] << " "
            << "亚军编号:" << this->m_Record[i][2] << " 得分:" << this->m_Record[i][3] << " "
            << "季军编号:" << this->m_Record[i][4] << " 得分:" << this->m_Record[i][5] << endl;
        }
    }
    system("pause");
    system("cls");
}

12.清空记录

void speechManager::emptyRecord()
{
    cout << "确认清空吗?" << endl;
    cout << "1.确认 2.返回" << endl;

    int select = 0;
    cin >> select;
    if(select == 1)
    {
        ofstream ofs("speech.csv", ios::trunc);
        ofs.close();
        this->initSpeech();
        this->createSpeaker();
        this->loadRecord();
        cout << "清空成功" << endl;
    }
    system("pause");
    system("cls");
}