扣篮大赛案例(含VScode实现)C++

扣篮大赛:实现的功能: 

1.开始扣篮大赛

2.查看往届记录

3.清空比赛记录

0.退出比赛进程

扣篮大赛规则:

       共有六名球员,五个评委。进行两轮比赛,第一轮比赛为六进三,第二轮为第一轮中晋级的三个人,第二轮比赛前第一轮数据清空,第二轮分数最高者夺冠 。每个评委打分区间为7-10分不等。将每次比赛的数据写到当前文件夹下名为dunk.csv的文件中。

创建扣篮比赛系统头文件,dunk.h文件如下: 

#pragma once

#include <iostream>
#include <vector>
#include <map>
#include "player.h"
#include <algorithm>
#include <deque>
#include <functional>
#include <numeric>
#include <string>
#include <fstream>

using namespace std;

//扣篮系统类
class Dunk
{
public:
    //构造函数
    Dunk(/* args */);

    //菜单函数
    void show_menu();

    //退出函数
    void existsys();

    //初始化所有容器
    void chushihua();

    //创建参赛的6名球员
    void createplayer();

    //开始比赛 比赛流程
    void startdunk();

    //抽签
    void dunkdraw();

    //比赛
    void dunkcontest();

    //显示得分
    void showscore();

    //保存分数
    void savedata();

    //加载记录
    void loaddata();

    //判断文件是否为空
    bool filesempty;

    //存放往届数据的容器
    map<int, vector<string>> m_data;


    //清空功能
    void cleardata();

    //成员属性
    //保存第一轮比赛球员编号
    vector<int> p1;

    //第一轮晋级球员编号,即第二轮参赛球员编号
    vector<int> p2;

    //第二轮晋级球员编号即冠军球员编号
    vector<int> pch;

    //存放编号以及对应的球员
    map<int, player> m_player;

    //存放比赛轮数
    int m_total;

    //析构函数
    ~Dunk();
};

 球员类的头文件player.h,记录球员属性:姓名和得分,代码如下:

#pragma once
#include <iostream>

using namespace std;

//运动员类
class player
{
public:
    string m_name;  //姓名
    int M_score[2]; //分数,一共两轮
};

实现函数test00.cpp文件如下: 

#include "dunk.h"

//构造函数
Dunk::Dunk()
{
    //初始化属性
    this->chushihua();

    //创建6名球员
    this->createplayer();

    //加载往届数据
    this->loaddata();
}

//菜单函数
void Dunk::show_menu()
{
    cout << "********************************" << endl;
    cout << "*******欢迎参加扣篮大赛**********" << endl;
    cout << "********1.开始扣篮大赛***********" << endl;
    cout << "********2.查看往届记录***********" << endl;
    cout << "********3.清空比赛记录***********" << endl;
    cout << "********0.退出比赛进程***********" << endl;
    cout << "********************************" << endl;
    cout << endl;
}

//创建球员
void Dunk::createplayer()
{
    string nameseed[6] = {"拉文", "卡特", "乔丹", "戈登", "霍华德", "小琼斯"};
    for (int i = 0; i < 6; i++)
    {
        string name = "球员";
        name += nameseed[i];

        //创建球员姓名得分
        player dp;
        dp.m_name = name;

        for (int j = 0; j < 2; j++)
        {
            dp.M_score[j] = 0;
        }

        //创建球员编号
        this->p1.push_back(i + 100);

        //球员编号以及对应的球员放入map
        this->m_player.insert(make_pair(i + 100, dp));
    }
}

//开始比赛 比赛流程
void Dunk::startdunk()
{
    //第一轮开始比赛

    // 1.抽签
    this->dunkdraw();

    // 2.比赛
    this->dunkcontest();

    // 3.显示晋级结果
    this->showscore();

    //第二轮比赛
    this->m_total++;

    // 1.抽签
    this->dunkdraw();

    // 2.比赛
    this->dunkcontest();

    // 3.显示冠军归属
    this->showscore();

    // 4.保存分数到文件中
    this->savedata();

    cout << "本届比赛全部结束" << endl;
    system("pause");
    system("cls");
}

//抽签
void Dunk::dunkdraw()
{
    cout << "第《" << this->m_total << "》轮比赛抽签进行中" << endl;
    cout << "---------------------------" << endl;
    cout << "抽签后的顺序是:" << endl;
    if (this->m_total == 1)
    {
        //第一轮
        random_shuffle(p1.begin(), p1.end());
        for (vector<int>::iterator it = p1.begin(); it != p1.end(); it++)
        {
            cout << *it << " ";
        }
        cout << endl;
    }
    else
    {
        //第二轮
        random_shuffle(p2.begin(), p2.end());
        for (vector<int>::iterator it = p2.begin(); it != p2.end(); it++)
        {
            cout << *it << " ";
        }
        cout << endl;
    }
    cout << "---------------------------" << endl;
    cout << "|*************************|" << endl;
    cout << "---------------------------" << endl;
    system("pause");
    cout << endl;
}

//比赛
void Dunk::dunkcontest()
{
    cout << "第《" << this->m_total << "》轮比赛正式开始" << endl;

    //准备临时容器 存放小组
    multimap<int, int, greater<int>> groupscore;

    int fenzu = 0;

    vector<int> p_src;
    if (this->m_total == 1)
    {
        p_src = p1;
    }
    else
    {
        p_src = p2;
    }

    //遍历所有球员
    for (vector<int>::iterator it = p_src.begin(); it != p_src.end(); it++)
    {
        fenzu++;
        //评委打分
        deque<int> d;
        for (int i = 0; i < 5; i++) // 5个评委打分
        {
            int score = rand() % 4 + 7; //分数区间为7分到10分
            // cout <<score << " ";//测试打分是否正确
            d.push_back(score);
        }
        cout << endl;
        sort(d.begin(), d.end(), greater<int>());    //进行排序
        int sum = accumulate(d.begin(), d.end(), 0); //总分

        //打印总分
        cout << "编号:" << *it << "| 姓名:" << this->m_player[*it].m_name << "| 总得分:" << sum << endl;

        //将总分放入map容器
        this->m_player[*it].M_score[this->m_total - 1] = sum;

        //将打分数据放入到临时小组容器中
        groupscore.insert(make_pair(sum, *it)); //键值key是总得分,value是球员编号

        //获得前三名
        if (fenzu % 6 == 0 && this->m_total == 1) //每6个人进三个
        {
            cout << endl;
            cout << "---------------------------" << endl;
            cout << "|-球员第" << this->m_total << "扣得分排名如下:-|" << endl;
            cout << "---------------------------" << endl;
            for (multimap<int, int, greater<int>>::iterator it = groupscore.begin(); it != groupscore.end(); it++)
            {
                cout << "编号:" << it->second << "| 姓名:" << this->m_player[it->second].m_name << "| 总得分:"
                     << this->m_player[it->second].M_score[this->m_total - 1] << endl;
            }

            //前三名晋级
            int count = 0;

            for (multimap<int, int, greater<int>>::iterator it = groupscore.begin(); it != groupscore.end() && count < 3; it++, count++)
            {
                if (this->m_total == 1)
                {
                    p2.push_back(it->second);
                }
                else
                {
                    pch.push_back(it->second);
                }
            }
            groupscore.clear();
        }
        else if (fenzu % 3 == 0 && this->m_total == 2)
        {
            cout << endl;
            cout << "---------------------------" << endl;
            cout << "|-球员第" << this->m_total << "扣得分排名如下:-|" << endl;
            cout << "---------------------------" << endl;
            for (multimap<int, int, greater<int>>::iterator it = groupscore.begin(); it != groupscore.end(); it++)
            {
                cout << "编号:" << it->second << "| 姓名:" << this->m_player[it->second].m_name << "| 总得分:"
                     << this->m_player[it->second].M_score[this->m_total - 2] << endl;
            }

            //冠军诞生
            int count = 0;
            for (multimap<int, int, greater<int>>::iterator it = groupscore.begin(); it != groupscore.end() && count < 1; it++, count++)
            {
                if (this->m_total == 1)
                {
                    p2.push_back(it->second);
                }
                else
                {
                    pch.push_back(it->second);
                }
            }
            groupscore.clear();
        }
    }
    cout << "------第《" << this->m_total << "》轮比赛结束!------" << endl;
    system("pause");
}

//显示得分
void Dunk::showscore()
{
    vector<int> p;
    if (this->m_total == 1)
    {
        cout << "-----第《" << this->m_total << "》轮晋级球员信息如下:-----" << endl;
        p = p2;
        for (vector<int>::iterator it = p.begin(); it != p.end(); it++)
        {
            cout << "编号:" << *it << "| 姓名: " << this->m_player[*it].m_name << "| 得分:"
                 << this->m_player[*it].M_score[this->m_total - 1] << endl;
        }
    }
    else
    {
        cout << "-----第《" << this->m_total << "》轮冠军球员信息如下:-----" << endl;
        p = pch;
        for (vector<int>::iterator it = p.begin(); it != p.end(); it++)
        {
            cout << "编号:" << *it << "| 姓名: " << this->m_player[*it].m_name << "| 得分:"
                 << this->m_player[*it].M_score[this->m_total - 2] << endl;
        }
    }
    cout << endl;

    system("pause");
    system("cls");
    this->show_menu();
}

//保存分数
void Dunk::savedata()
{
    ofstream ofs;
    ofs.open("dunk.csv", ios::out | ios::app); // app表示追加文件,csv格式keyiyongexcel或者记事本打开

    if (!ofs)
    {
        cerr << "打开错误!" << endl;
        exit(1);
    }

    //将每个球员写入文件
    for (vector<int>::iterator it = pch.begin(); it != pch.end(); it++)
    {
        /* ofs << "编号:" << *it << ","
            << "得分:" << this->m_player[*it].M_score[1] << ","; */
        ofs << *it << ","
            << this->m_player[*it].M_score[1] << ",";
    }
    ofs << endl;

    ofs.close();
    cout << "数据记录完成" << endl;
}

//加载记录
void Dunk::loaddata()
{
    ifstream ifs("dunk.csv", ios::in); //读文件

    //文件不存在的情况
    if (!ifs.is_open())
    {
        this->filesempty = true;
        cout << "文件不存在!" << endl;
        ifs.close();
        return;
    }

    //文件清空的情况
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        cout << "文件为空" << endl;
        this->filesempty = true;
        ifs.close();
        return;
    }

    //文件不为空
    this->filesempty = false;

    ifs.putback(ch); //将上面读取的单个字符取回

    string data;
    int index = 1; //第1届
    while (ifs >> data)
    {
        cout << "第" << index << "届冠军信息:" << data << endl;

        vector<string> v; //存放字符串

        index++;
    }

    ifs.close();
}


//清空功能
void Dunk::cleardata()
{
    cout << "是否清空数据?" << endl;
    cout << "1.是" << endl;
    cout << "2.否" << endl;

    int sel = 0;
    cin >> sel;

    if (sel == 1)
    {
        //确认清空
        ofstream ofs("dunk.csv", ios::trunc); //清空
        ofs.close();

        //初始化属性
        this->chushihua();

        //创建6名球员
        this->createplayer();

        //加载往届数据
        this->loaddata();
        cout << "清空成功!" << endl;
    }
    system("pause");
    system("cls");
}

//退出函数
void Dunk::existsys()
{
    cout << "恭喜扣篮大赛圆满结束!" << endl;
    system("pause");
    exit(0);
}

//容器初始化
void Dunk::chushihua()
{
    //容器清空
    this->p1.clear();
    this->p2.clear();
    this->pch.clear();
    this->m_player.clear();

    //初始化比赛轮数
    this->m_total = 1;
}

//析构函数,在这个程序中可有可无,因为没有堆区数据
Dunk::~Dunk()
{
}

主函数c.cpp文件如下:

#include <iostream>
#include "dunk.h"
#include <string>
#include <ctime>

using namespace std;

int main()
{
    // 0 随机种子
	srand((unsigned int)time(NULL)); //利用系统时间实现真随机

	Dunk play;

	int choice = 0; //用户输入选择
	while (true)
	{
		play.show_menu();
		cout << "请输入您的选择:" << endl;
		cin >> choice;
		switch (choice)
		{
		case 1: // 1.开始扣篮大赛
			play.startdunk();
			break;
		case 2: // 2.查看往届记录
			play.loaddata();
			//play.showdata();
			break;
		case 3: // 3.清空比赛记录
			play.cleardata();
			break;
		case 0: //退出
			play.existsys();
			break;
		default:
			system("cls"); //清屏
			break;
		}
	}

	system("pause");
	return 0;
}

 结果如下:

vscode实现调试的launch.json文件和tasks.json文件

launch.json文件: 

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            //"program": "${file}",//表示当前活跃的文件
            "program":"${workspaceFolder}/image.py",//调节python文件名来运行指定文件
           // "program":"${workspaceFolder}/shuayuedu.py",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal"//集成终端
        },
        {
            "name": "cpp",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/test.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true, //运行结果是否出现弹窗 
            "console": "integratedTerminal",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\mingw64\\bin\\gdb.exe", //miDebugger的路径,应与MinGW路径一致
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件" //与tasks中的label对应
        }
    ]
}

tasks.json文件如下:

{
    "tasks": [
        //vscode的调试指令
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "g++",
            "args": [
                //"${workspaceFolder}/dunk.cpp",//调节指定C++文件,
                "${workspaceFolder}/c.cpp",
                "${workspaceFolder}/test00.cpp",
                "-g",
                "-o",   //"-o"表示优化,"-g"表示调试
                "${workspaceFolder}/test.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值