Code:
#include "stdafx.h"
#include "string.h"
#define NUMBER_OF_TEAMS 20 /*The number of teams in the EPL*/
#define NAME_LENGTH 12 /*Maximum length for any team name*/
#define WIN 3 /*Points revieced when a games is won*/
#define DRAW 1 /*Points revieced when a game is drawn*/
#define PLAY 1 /*Add to games played*/
typedef struct{
char name[NAME_LENGTH]; /*Team name*/
int games_played; /*Games played*/
int goals_scored; /*Goals scored*/
int goals_conceded; /*Goals conceded*/
int total_points; /*Point on table*/
int goal_diff; /*Goal difference = (goals_scored - goals_conceded)*/
}team_x;
void get_team_names(team_x *team);
void get_game_results(team_x *team);
void goal_difference(team_x *team);
void teams_a_to_z(team_x *team);
void sort_points(team_x *team);
void sort_goal_diff(team_x *team);
void sort_goals(team_x *team);
void standings_table(team_x *team);
int
main(void)
{
team_x team[NUMBER_OF_TEAMS]={0,0,0,0,0}; /*Array team of type team_x NUMBER_OF_TEAMS big*/
get_team_names(team); /*Function to retrieve the team names i.e. data from input2a.dat*/
get_game_results(team); /*Function to retrieve data from input2b.dat (results) and store these results into team[]*/
goal_difference(team); /*Calculated goal difference for each team*/
teams_a_to_z(team); /*Outputs a table of teams in alphabetical order*/
sort_points(team); /*Sorts teams in order of best performing to worst performing by points*/
sort_goal_diff(team); /*Sorts teams in order of best performing to worst performing by goal difference*/
sort_goals(team); /*Sorts teams in order of best performing to worst performing by which team scored the most goals*/
standings_table(team); /*Outputs a standings table where the team with the most points is on top*/
return 0;
}
void get_team_names(team_x team[])
{
FILE *inp1; /*Data file containing team names*/
int i; /*Variable for loop*/
/*Opens the data file*/
inp1 = fopen("input2a.dat", "r");
/*Creates message if no data is found*/
if (inp1 == NULL){
printf("Error reading 'input2a.dat'\n");
}
/*If data is found, stores in array team[].name*/
else{
for(i=0; i<NUMBER_OF_TEAMS; i+=1){
fscanf(inp1, "%12s", team[i].name);
}
}
/*Close data file*/
fclose(inp1);
}
void get_game_results(team_x team[])
{
FILE *inp2; /*Data file containing results of games*/
int i, k, j; /*Variables for loop*/
int input_status=0; /*Status value to test if data file is valid*/
char team1[NAME_LENGTH], team2[NAME_LENGTH]; /*Temporary places to store teams*/
int score1, score2; /*Temporary places to store score results*/
/*Opens the data file*/
inp2 = fopen("input2b.dat", "r");
/*Reads in first dataset*/
input_status = fscanf(inp2, "%12s %3d %12s %3d", &team1, &score1, &team2, &score2);
/*Test whether dataset is found*/
if (input_status == NULL){
printf("Error reading 'input2b.dat'\n");
}
/*If data is found, run loop until EOF*/
else{
while(input_status != EOF){
/*If name inputted is not an EPL team name from the first dataset then prints error message*/
for(k=0; k<NUMBER_OF_TEAMS; k+=1){
if((strcmp(team[k].name, team1) != 0) || (strcmp(team[k].name, team2) != 0)){
}
else if((strcmp(team[k].name, team1) == 0) || (strcmp(team[k].name, team2) != 0)){
}
else{
printf("ERROR in input2b.dat - Team name entered incorrectly\n");
printf("Inputted values were:\n %12s %3d %12s %3d\n", team1, score1, team2, score2);
}
}
}
/*Checks to see if negative goals were inputted*/
if(score1<0 || score2<0){
printf("\nERROR in input2b.dat - Negative goals entered\n");
printf("Inputted values were:\n %12s %3d %12s %3d\n", team1, score1, team2, score2);
}
/*Stores data in correct place in array*/
else{
for(i=0; i<NUMBER_OF_TEAMS; i+=1){
if(strcmp(team[i].name, team1)==0){
team[i].games_played=team[i].games_played+PLAY; /*Adds 1 to games played for team1*/
team[i].goals_scored=team[i].goals_scored+score1; /*Adds goals scored for team1*/
team[i].goals_conceded=team[i].goals_conceded+score2; /*Adds goals conceded for team1*/
if(score1>score2){
team[i].total_points=team[i].total_points+WIN; /*Adds 3 points to team1 if they won*/
}
if(score1==score2){
team[i].total_points=team[i].total_points+DRAW; /*Adds 1 points to team1 if they drew*/
} /*Doesn't add anything if team1 lost*/
}
else if(strcmp(team[i].name, team2)==0){
team[i].games_played=team[i].games_played+PLAY; /*Adds 1 to games played for team2*/
team[i].goals_scored=team[i].goals_scored+score2; /*Adds goals scored for team2*/
team[i].goals_conceded=team[i].goals_conceded+score1; /*Adds goals conceded for team2*/
if(score2>score1){
team[i].total_points=team[i].total_points+WIN; /*Adds 3 points to team2 if they won*/
}
if(score2==score1){
team[i].total_points=team[i].total_points+DRAW; /*Adds 1 points to team1 if they drew*/
} /*Doesn't add anything if team2 lost*/
}
}
}
/*Scans in dataset for next loop or does nothing if reached EOF*/
input_status = fscanf(inp2, "%12s %3d %12s %3d", &team1, &score1, &team2, &score2);
}
/*Close data file*/
fclose(inp2);
}
void goal_difference(team_x team[])
{
int i; /*Variable for loop*/
for(i=0; i<NUMBER_OF_TEAMS; i+=1){
team[i].goal_diff = team[i].goals_scored - team[i].goals_conceded; /*calculates goal difference and saves into team[].goal_diff*/
}
}
void teams_a_to_z(team_x team[])
{
int i; /*Variable for loop*/
printf("\nTeam Results - Alphabetical Order\n"); /*Table heading*/
printf(" Name Played Points Goal-Diff Goals-Scored\n"); /*Prints headings for table columns*/
printf("-----------------------------------------------------------------\n"); /*Table lines*/
for(i=0; i<NUMBER_OF_TEAMS; i+=1){
/*Prints data in alphabetical order*/
printf("%12s %3d %3d %5d %5d\n", team[i].name, team[i].games_played, team[i].total_points, team[i].goal_diff, team[i].goals_scored);
}
printf("-----------------------------------------------------------------\n"); /*Table lines*/
}
void sort_points(team_x team[])
{
int i,k; /*Variable for loop*/
team_x temp; /*Temporary value*/
/*Loop to sort EPL teams based on total points*/
for(k=0; k<NUMBER_OF_TEAMS; k+=1){
for(i=0; i<NUMBER_OF_TEAMS-1; i+=1){
if (team[i+1].total_points > team[i].total_points){
temp = team[i];
team[i] = team[i+1];
team[i+1] = temp;
}
}
}
}
void sort_goal_diff(team_x team[])
{
int i,k; /*Variable for loop*/
team_x temp; /*Temporary value*/
/*Loop to sort EPL teams based on goal difference*/
for(k=0; k<NUMBER_OF_TEAMS; k+=1){
for(i=0; i<NUMBER_OF_TEAMS-1; i+=1){
if (team[i+1].goal_diff > team[i].goal_diff && team[i+1].total_points==team[i].total_points){
temp = team[i];
team[i] = team[i+1];
team[i+1] = temp;
}
}
}
}
void sort_goals(team_x team[])
{
int i,k; /*Variable for loop*/
team_x temp; /*Temporary value used so variables can swap positions*/
/*Loop to sort EPL teams based on goals scored*/
for(k=0; k<NUMBER_OF_TEAMS; k+=1){
for(i=0; i<NUMBER_OF_TEAMS-1; i+=1){
if (team[i+1].goals_scored > team[i].goals_scored && team[i+1].goal_diff == team[i].goal_diff && team[i+1].total_points==team[i].total_points){
temp = team[i];
team[i] = team[i+1];
team[i+1] = temp;
}
}
}
}
void standings_table(team_x team[])
{
int i; /*Variable for loop*/
printf("\n\nTeam Results - Performance Ranking\n"); /*Table heading*/
printf(" Name Played Points Goal-Diff Goals-Scored\n"); /*Table column headings*/
printf("-----------------------------------------------------------------\n"); /*Table lines*/
for(i=0; i<NUMBER_OF_TEAMS; i+=1){
/*Prints data as a standings (performance) table*/
printf("%12s %3d %3d %5d %5d\n", team[i].name, team[i].games_played, team[i].total_points, team[i].goal_diff, team[i].goals_scored);
}
printf("-----------------------------------------------------------------\n"); /*Table lines*/
}