#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;
}