#include <stdio.h>#include <string.h>#include <ctype.h>/* For tolower */#define CHARS 30#define TEAMS 4#define STATS 7#define YES 1#define NO 0#define WIN_POINTS 3#define DRAW_POINTS 1#define LOSE_POINTS 0enum{ PLAYED, WON, DRAWN, LOST, FOR, AGAINST, POINTS }; /*define played as 1, won as 2 etc. column numbers */typedef struct{ char name[CHARS]; int stat[STATS];} team;/*struct FOR the team, holding a array FOR the team name of 20 characters, and a array of 7 FOR the stats */char get_char();int get_int();void init(team*);void disp_table(team*);void set_stats(team*, int, int, int);void enter_match(team*);char do_menu();int teams_entered;void sorting(team*);int readfile(int , team*);int savefile(int, team*);int findteamid(int, team* );/*define functions and params */int main(){ team league[TEAMS]; int finished = NO; /*sets up a leage with 4 teams using the struct data type */ { int x =0; for (x = 0; x < TEAMS; x++) { league[x].stat[AGAINST] = 0; league[x].stat[DRAWN] = 0; league[x].stat[FOR] = 0; league[x].stat[LOST] = 0; league[x].stat[PLAYED] = 0; league[x].stat[POINTS] = 0; league[x].stat[WON] = 0; strcpy(league[x].name, "name"); } do { char choice = do_menu(); switch( tolower(choice) ) /*change choice to lower case */ { case 'a': init(league); break; case 'b': disp_table(league); break; case 'c': enter_match(league); break; case'd': savefile(TEAMS,league); break; case'e': readfile(TEAMS,league); break; case 'q': finished = YES; break; default: printf("INVALID OPTION!\n"); } } while ( !finished ); }} char get_char() { char str[CHARS]; gets(str); return *str; }/*take a string input and return the first value of it */int get_int() { int i = 1; do { char str[CHARS]; gets( str); if (!sscanf(str, "%d", &i)) printf("Please enter a integer\n"); else return i ; }while (i); }/*take a string input and return a decimal int from that string */void init(team* league){ int i,j; for( i = 0; i < TEAMS; i++ ) { char str[CHARS]; printf("Enter Team %d Name: ", i); gets(str); strcpy( league[i].name , str ); for( j = 0; j<STATS; j++ ) league[i].stat[j] = 0; }teams_entered = 1;printf("\n");}/*initilise the team and the stats set to 0 for that team. Continue For the value of TEAMS */void disp_table(team* league){ sorting(league); int i,j; printf("\t%-30s%-4c%-4c%-4c%-4c%-4c%-4c%-4c\n", "", 'P', 'W', 'D', 'L', 'F', 'A', 'T' ); /*table headings */ for( i = 0; i < TEAMS; i++ ) { printf("\n%d\t%-30s", i, league[i].name); for( j = 0; j < STATS; j++ ) printf("%-4d", league[i].stat[j] ); }/*print out team name and stats */ printf("\n\n");}void set_stats(team* league, int id, int scored, int conceded){ league[id].stat[FOR] += scored; /*FOR a specific team, add on the goals FOR, against and increment games played by 1*/ league[id].stat[AGAINST] += conceded; league[id].stat[PLAYED] += 1; if(scored > conceded) /*if a team won, then add 3 to the points*/ { league[id].stat[POINTS] += WIN_POINTS; league[id].stat[WON] += 1; } else if (scored == conceded) /*if a team drew, then add 1 to the points*/ { league[id].stat[POINTS] += DRAW_POINTS; league[id].stat[DRAWN] += 1; } else // ( scored < conceded )/*if a team looses then add 1 to their lost total*/ { league[id].stat[POINTS] += LOSE_POINTS; league[id].stat[LOST] += 1; }}void enter_match(team* league){ int t1, t2, g1, g2; disp_table(league); t1 = findteamid(TEAMS,league); printf("Enter goals for team %s : ", league[t1].name ); g1 = get_int(); fflush(stdin); t2 = findteamid(TEAMS,league); printf("Enter goals for team %s : ", league[t2].name ); g2 = get_int(); fflush(stdin); if(t1 == t2) { printf("*******The teams you entered are the same, please try again*******\n"); /*check that the same teams haven't been entered twice*/ } else { set_stats(league, t1, g1, g2);/*set the stats from the data entered, using the set_stats() function */ set_stats(league, t2, g2, g1); } printf("\n");}char do_menu(){ printf("A - Enter Teams\n"); printf("B - Display Table\n"); printf("C - Enter Match Details\n"); printf("D - Save Table\n"); printf("E - Open Table\n"); printf("Q - Quit\n\n"); printf("Enter Choice: "); return get_char();}void sorting(team* league){ int i, j; team tmp; for (i = 0; i < (TEAMS - 1); ++i) { for (j = 0; j < (TEAMS - 1); ++j) { if (league[j].stat[FOR] < league[j+1].stat[FOR]) { tmp = league[j]; league[j] = league[j+1]; league[j+1] = tmp;/*for each teams FOR value, sort them in ascending order*/ } } } for (i = 0; i < (TEAMS - 1); ++i) { for (j = 0; j < (TEAMS - 1); ++j) { if ((league[j].stat[FOR] - league[j].stat[AGAINST]) < (league[j+1].stat[FOR] - league[j+1].stat[AGAINST])) { tmp = league[j]; league[j] = league[j+1]; league[j+1] = tmp;/*for each teams goal difference, sort them in ascending order */ } } } for (i = 0; i < (TEAMS - 1); ++i) { for (j = 0; j < (TEAMS - 1); ++j) { if (league[j].stat[POINTS] < league[j+1].stat[POINTS]) { tmp = league[j]; league[j] = league[j+1]; league[j+1] = tmp;/*for each teams points, sort them in ascending order*/ } } }}/*makes sure that a list is sorted, even if the goal difference and total points are the same */int savefile(int numteams, team* league) { char filename[21], line[21], ext[5];/*set up arrays of characters for the strings filename, line and ext*/char name[16]; strcpy(ext, ".res"); /*set the file extention as .res */int i, ID, P, W, D, L, F, A, T;FILE *out_file;printf("\nEnter filename:\n>"); /*get the file name from the user */ scanf("%s", filename);/*read the filename into filename*/ gets(line); strcat(filename, line); strcat(filename, ext);/*add the ext to the filename*/out_file = fopen(filename, "w");/*open the file for writing*/if (out_file == NULL){printf("\nCannot open: %s", filename);return 0;}for (i = 0; i < numteams; i++) { strcpy(name, league[i].name);/*for each team, write into the file the stats, in a formatted fashion*/ A= league[i].stat[AGAINST]; D= league[i].stat[DRAWN]; F = league[i].stat[FOR]; L = league[i].stat[LOST]; P = league[i].stat[PLAYED]; T = league[i].stat[POINTS]; W = league[i].stat[WON];fprintf(out_file, "%-15s%3d%3d%3d%3d%3d%3d%3d\n", name, P, W, D, L, F, A, T);}fclose (out_file);/*close the file*/return 0;}int readfile(int numteams, team* league) { char filename[21], line[21], name[16], name2[16], ext[5]; strcpy(ext, ".res"); int i, P, W, D, L, F, A, T; FILE *out_file;printf("\nEnter filename:\n>"); scanf("%s", filename); gets(line); strcat(filename, line); strcat(filename, ext);/*get a filename from the user*/out_file = fopen(filename, "r");/*open the filename for reading */if (out_file == NULL){printf("\nCannot open: %s", filename);return 0;}for (i = 0; i < TEAMS; i++) { fgets(name, 16, out_file); fscanf(out_file,"%3d%3d%3d%3d%3d%3d%3d\n", &P, &W, &D, &L, &F, &A, &T); league[i].stat[AGAINST] = A; league[i].stat[DRAWN] = D; league[i].stat[FOR] = F; league[i].stat[LOST] = L; league[i].stat[PLAYED] = P; league[i].stat[POINTS] = T; league[i].stat[WON] = W; strcpy(league[i].name, name); (numteams) = i;/*for each team, read in the data from the file into memory*/}fclose (out_file);return 0;}int findteamid(int numteams, team* league) { char teamname[CHARS]; printf("\nEnter team name:\n"); /*get the team name from the user */ scanf("%s", teamname); int id = 0; int i ; for (i = 0; i < numteams; i++) { if ( strcmp( teamname, league[i].name ) == 0 ) { id = i; fflush(stdin); } //printf("\nYou have not entered a valid team, please try again\n"); //findteamid(TEAMS,league); }return id;}