C++课程设计CBA联赛积分系统

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

//定义一个结构来表示团队的记录
struct Team {
    string name;
    int gamesPlayed;
    int wins;
    int losses;
    int points; // total points earned
};

//定义一个函数,根据两个团队的总分对其进行比较
bool compareTeams(Team team1, Team team2) {
    return team1.points > team2.points;
}

int main() {
    const int NUM_TEAMS = 8;  // number of teams in the league
    const int POINTS_WIN = 2; // points earned for a win
    const int POINTS_LOSS = 1; // points earned for a loss

    // 初始化一组团队
    vector<Team> teams;
    teams.push_back({ "Beijing Ducks", 0, 0, 0, 0 });
    teams.push_back({ "Guangdong Southern Tigers", 0, 0, 0, 0 });
    teams.push_back({ "Xinjiang Flying Tigers", 0, 0, 0, 0 });
    teams.push_back({ "Liaoning Flying Leopards", 0, 0, 0, 0 });
    teams.push_back({ "Shandong Heroes", 0, 0, 0, 0 });
    teams.push_back({ "Zhejiang Lions", 0, 0, 0, 0 });
    teams.push_back({ "Jiangsu Dragons", 0, 0, 0, 0 });
    teams.push_back({ "Bayi Rockets", 0, 0, 0, 0 });

    int numGames = 0;

    // 要求用户输入比赛结果
    while (true) {
        int home, away, scoreHome, scoreAway;
        cout << "Enter the index of the home team (1-" << NUM_TEAMS << "), or 0 to exit: ";
        cin >> home;
        if (home == 0) {
            break;
        }
        cout << "Enter the index of the away team (1-" << NUM_TEAMS << "): ";
        cin >> away;
        cout << "Enter the score of the home team: ";
        cin >> scoreHome;
        cout << "Enter the score of the away team: ";
        cin >> scoreAway;

        //根据比赛结果更新球队记录
        teams[home - 1].gamesPlayed++;
        teams[away - 1].gamesPlayed++;
        if (scoreHome > scoreAway) {
            // 主队获胜
            teams[home - 1].wins++;
            teams[away - 1].losses++;
            teams[home - 1].points += POINTS_WIN;
        }
        else {
            // 客场球队获胜或打平
            teams[away - 1].wins++;
            teams[home - 1].losses++;
            teams[away - 1].points += POINTS_WIN;
            if (scoreHome == scoreAway) {
                // 平局
                teams[home - 1].points += POINTS_LOSS;
                teams[away - 1].points += POINTS_LOSS;
            }
        }
        numGames++;
    }

    // 根据得分对团队进行排序
    sort(teams.begin(), teams.end(), compareTeams);

    // 打印团队排名
    cout << "Team              W    L    Pts    W%" << endl;
    for (int i = 0; i < NUM_TEAMS; i++) {
        cout << setw(20) << teams[i].name << setw(5) << teams[i].wins << setw(5) << teams[i].losses;
        cout << setw(7) << teams[i].points << setw(7) << fixed << setprecision(3) << 100.0 * teams[i].wins / teams[i].gamesPlayed << "%" << endl;
    }
    cout << "Total games played: " << numGames << endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值