C++实现演讲比赛系统,多种容器使用。

演讲比赛系统

项目需求

演讲比赛人数为12人,分为两组比赛,每组的前三名进入下一局的比赛,最后的三人为冠亚季军。十名评委打分,去掉一个最高分和一个最低分。每轮比赛后显示晋级的前三名选手。最后的冠亚季军的资料保存在excel文件中。
功能:
1.开始比赛
2.显示以往记录
3.清空记录
0.退出系统

代码

speaker.h

#ifndef SPEAKER_H_INCLUDED
#define SPEAKER_H_INCLUDED

#include<iostream>
using namespace std;

class speaker
{
public:
    string name;
    double score[2];
};

#endif // SPEAKER_H_INCLUDED

speechmanger,h

#ifndef SPEECHMANGER_H_INCLUDED
#define SPEECHMANGER_H_INCLUDED
#include<iostream>
#include<vector>
#include<map>
#include"speaker.h"
#include<string>
#include<algorithm>
#include<functional>
#include<deque>
#include<numeric>
#include<fstream>
#include<ctime>
using namespace std;
class speechmanger
{
    public:
        speechmanger();
        void showmenu();
        void exitspeech();
        void initspeech();
        void createspeaker();
        void startgame();
        void speechdraw();
        void speechcontest();
        void showscore();
        void saverecord();
        void loadrecord();
        void showrecord();
        void clearrecord();
        ~speechmanger();
        vector<int>v1;
        vector<int>v2;
        vector<int>vvictory;
        map<int,speaker>mspeaker;
        map<int,vector<string>>mrecord;
        int gamenum;
        bool fileempty;
};

#endif // SPEECHMANGER_H_INCLUDED

speechmanger,cpp

#include"speechmanger.h"
speechmanger::speechmanger()
{
    this->initspeech();
    this->createspeaker();
    this->loadrecord();
}
void speechmanger::showmenu()
{
    cout<<"欢迎使用演讲比赛系统"<<"\n";
    cout<<"1.开始演讲比赛"<<"\n";
    cout<<"2.查看往届比赛"<<"\n";
    cout<<"3.清空比赛记录"<<"\n";
    cout<<"0.退出演讲比赛"<<"\n";
}
void speechmanger::exitspeech()
{
    cout<<"欢迎下次使用"<<"\n";
    system("pause");
    exit(0);
}
void speechmanger::initspeech()
{
    this->v1.clear();
    this->v2.clear();
    this->vvictory.clear();
    this->mspeaker.clear();
    this->gamenum=1;
    this->mrecord.clear();
}
void speechmanger::createspeaker()
{
    string nameseed="ABCDEFGHIJKL";
    for(int i=0;i<nameseed.size();i++)
    {
        string mname="选手";
        mname+=nameseed[i];
        speaker one;
        one.name=mname;
        for(int j=0;j<2;j++)
        {
            one.score[j]=0;
        }
        this->v1.push_back(i+10001);
        this->mspeaker.insert(make_pair(i+10001,one));
    }
}
void speechmanger::startgame()
{
    this->speechdraw();
    this->speechcontest();
    this->showscore();
    this->gamenum++;
    this->speechdraw();
    this->speechcontest();
    this->showscore();
    this->saverecord();
    this->initspeech();
    this->createspeaker();
    this->loadrecord();
    system("cls");
}
void speechmanger::speechdraw()
{
    cout<<"第"<<this->gamenum<<"轮比赛正在抽签"<<"\n";
    cout<<"-----------------"<<"\n";
    cout<<"抽签顺序如下:"<<"\n";
    if(this->gamenum==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<<"---------------"<<"\n";
    system("pause");
}
void speechmanger::speechcontest()
{
    cout<<"第"<<this->gamenum<<"轮比赛正式开始"<<"\n";
    multimap<double,int,greater<double>>groupscore;
    int num=0;
    vector<int>v;
    if(this->gamenum==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->mspeaker[*it].score[this->gamenum-1]=avg;
            groupscore.insert(make_pair(avg,*it));
            if(num%6==0)
            {
                cout<<"第"<<num/6<<"小组比赛名次如下:"<<"\n";
                for(multimap<double,int,greater<double>>::iterator it=groupscore.begin();it!=groupscore.end();it++)
                {
                    cout<<"编号:"<<it->second<<"姓名:"<<this->mspeaker[it->second].name<<"得分:"<<this->mspeaker[it->second].score[this->gamenum-1]<<"\n";
                }
                int number=0;
                for(multimap<double,int,greater<double>>::iterator it=groupscore.begin();it!=groupscore.end()&&number<3;it++,number++)
                {
                    if(this->gamenum==1)
                    {
                        v2.push_back((*it).second);
                    }
                    else
                    {
                        vvictory.push_back((*it).second);
                    }
                }
                groupscore.clear();
            }
    }
    cout<<"第"<<this->gamenum<<"轮比赛结束"<<"\n";
    system("pause");
}
void speechmanger::showscore()
{
    cout<<"第"<<this->gamenum<<"晋级选手如下:"<<"\n";
    vector<int>v;
    if(this->gamenum==1)
    {
        v=v2;
    }
    else
    {
        v=vvictory;
    }
    for(vector<int>::iterator it=v.begin();it!=v.end();it++)
    {
        cout<<"编号:"<<*it<<"姓名:"<<mspeaker[*it].name<<"得分:"<<mspeaker[*it].score[this->gamenum-1]<<"\n";
    }
    cout<<endl;
    system("pause");
    system("cls");
    this->showmenu();

}
void speechmanger::saverecord()
{
    ofstream ofs;
    ofs.open("speech.csv",ios::out|ios::app);
    for(vector<int>::iterator it=vvictory.begin();it!=vvictory.end();it++)
    {
        ofs<<*it<<","<<mspeaker[*it].score[1]<<",";
    }
    ofs<<endl;
    ofs.close();
    cout<<"记录保存完毕"<<"\n";
}
void speechmanger::loadrecord()
{
    ifstream ifs;
    ifs.open("speech.csv",ios::in);
    if(!ifs.is_open())
    {
        this->fileempty=true;
        cout<<"文件不存在"<<"\n";
        ifs.close();
        return;
    }
    char ch;
    ifs>>ch;
    if(ifs.eof())
    {
        cout<<"文件为空"<<"\n";
        this->fileempty=true;
        ifs.close();
        return;
    }
    this->fileempty=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 tem=data.substr(start,pos-start);
            v.push_back(tem);
            start=pos+1;
        }
        this->mrecord.insert(make_pair(index,v));
        index++;
    }
    ifs.close();
//    for(map<int,vector<string>>::iterator it=mrecord.begin();it!=mrecord.end();it++)
//    {
//        cout<<"第"<<it->first<<"界冠军编号:"<<it->second[0]<<"分数"<<it->second[1]<<"\n";
//    }
//    system("pause");
//    system("cls");
}
void speechmanger::showrecord()
{
    for(int i=0;i<mrecord.size();i++)
    {
        cout<<"第"<<i+1<<"界"<<"\n";
        cout<<"冠军编号:"<<this->mrecord[i][0]<<"得分:"<<this->mrecord[i][1]<<"\n";
        cout<<"亚军编号:"<<this->mrecord[i][2]<<"得分:"<<this->mrecord[i][3]<<"\n";
        cout<<"季军编号:"<<this->mrecord[i][4]<<"得分:"<<this->mrecord[i][5]<<"\n";
    }
    system("pause");
    system("cls");
}
void speechmanger::clearrecord()
{
    cout<<"确定清空数据? 1.是 2.否"<<"\n";
    int select;
    cin>>select;
    if(select==1)
    {
        ofstream ofs;
        ofs.open("speech.csv",ios::trunc);
        ofs.close();
        this->initspeech();
        this->createspeaker();
        this->loadrecord();
        cout<<"清空成功"<<"\n";
    }
}
speechmanger::~speechmanger()
{

}

演讲流程管理.cpp

#include<iostream>
#include"speechmanger.h"
using namespace std;
int main()
{
    srand((unsigned int)time(NULL));//随机种子;
    speechmanger one;
//    for(map<int,speaker>::iterator it=one.mspeaker.begin();it!=one.mspeaker.end();it++)
//    {
//        cout<<it->first<<"\t";
//        cout<<it->second.name<<"\t";
//        cout<<it->second.score[0]<<"\n";
//    }
    while(true)
    {
        one.showmenu();
        cout<<"please choose your choice:"<<"\n";
        int selection;
        cin>>selection;
        switch(selection)
        {
            case 1:one.startgame();
                break;
            case 2:one.showrecord();
                break;
            case 3:one.clearrecord();
                break;
            case 0:one.exitspeech();
                break;
            default:system("cls");
                break;
        }
    }
    return 0;
}

截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值