Created
December 17, 2021 10:52
-
-
Save shubham14bajpai/215ee3c614d37ef3ef6a9837c9e6895d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <map> | |
#include "standings.h" | |
using namespace std; | |
struct Team { | |
int win; | |
int loss; | |
int draw; | |
int goal_scored; | |
int goal_conceded; | |
int score; | |
}; | |
ostream& operator<<(ostream& os, const Team& team) { | |
return os << team.win | |
<< "\t" << team.draw | |
<< "\t" << team.loss | |
<< "\t" << team.goal_scored << "-" << team.goal_conceded | |
<< "\t" << team.score; | |
} | |
void add_scores(map<string, Team> &team_standings) { | |
string home_team, visiting_team; | |
int home_goals, visiting_goals; | |
cout << "Home team? "; | |
cin >> home_team; | |
cout << "Visiting team? "; | |
cin >> visiting_team; | |
cout << "Goals for home team? "; | |
cin >> home_goals; | |
cout << "Goals for visiting team? "; | |
cin >> visiting_goals; | |
if(team_standings.find(home_team) == team_standings.end()) { | |
struct Team t = {0, 0, 0, home_goals, visiting_goals, 0}; | |
team_standings[home_team] = t; | |
} else { | |
team_standings[home_team].goal_scored += home_goals; | |
team_standings[home_team].goal_conceded += visiting_goals; | |
} | |
if(team_standings.find(visiting_team) == team_standings.end()) { | |
struct Team t = {0, 0, 0, visiting_goals, home_goals, 0}; | |
team_standings[visiting_team] = t; | |
} else { | |
team_standings[visiting_team].goal_scored += visiting_goals; | |
team_standings[visiting_team].goal_conceded += home_goals; | |
} | |
if(home_goals > visiting_goals) { | |
team_standings[home_team].win += 1; | |
team_standings[visiting_team].loss += 1; | |
team_standings[home_team].score += 3; | |
cout << home_team << "won " << home_goals << "-" << visiting_goals << "!"; | |
} | |
else if (home_goals < visiting_goals) { | |
team_standings[visiting_team].win += 1; | |
team_standings[home_team].loss += 1; | |
team_standings[visiting_team].score += 3; | |
cout << visiting_team << "won " << visiting_goals << "-" << home_goals << "!"; | |
} | |
else { | |
team_standings[home_team].draw += 1; | |
team_standings[visiting_team].draw += 1; | |
team_standings[home_team].score += 1; | |
team_standings[visiting_team].score += 1; | |
cout << "It was a draw!"; | |
} | |
cout << "Score Added!" << endl; | |
} | |
void print_standings(map<string, Team> &team_standings) { | |
cout << "Team" << "\t\tWin" << "\tDraw" << "\tLost" << "\tGoals" << "\tScore" << endl; | |
map<string, Team>::iterator itr; | |
for(itr = team_standings.begin(); itr != team_standings.end(); ++itr) { | |
cout << itr->first << "\t" << itr->second << endl; | |
} | |
} | |
int main() { | |
int input = 0; | |
map<string, Team> team_standings; | |
while(input != 3) { | |
cout << "Input function (1= add scores, 2= print standings, 3= exit program): "; | |
cin >> input; | |
if(input == 1) { | |
add_scores(team_standings); | |
} | |
if(input == 2) { | |
print_standings(team_standings); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment