#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 leagueconst int POINTS_WIN = 2; // points earned for a winconst 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(), d(), 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;
}
本文发布于:2024-01-29 13:30:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170650622115615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |