#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INPUTFILE "matches-2019-2020.txt"
#define LINESZ 1024
#define NUMOFTEAMS 14
#define NUMOFMATCHES 182
typedef struct match
{
char day_name[4];
int day;
int month;
float hour;
char team_1[4];
char team_2[4];
int team_1_goal;
int team_2_goal;
int viewer;
} match;
typedef struct team
{
char team_names[4];
int goals_scored;
int goals_conceded;
int points;
} team;
match Match[NUMOFMATCHES];
team Team[NUMOFTEAMS];
int main(void) {
FILE *f;
char line[LINESZ];
int matchno = 0;
f = fopen(INPUTFILE, "r");
if (f ==(FILE *)NULL) {
fprintf(stderr, "Can't open file '%s'\n", INPUTFILE);
exit(-1);
}
/* read matches data from file, and store data into Match struct */
while(1) {
char *str;
char *saveptr;
if (feof(f))
break;
str = fgets(line, LINESZ, f);
if (str != (char *)NULL) {
char *ptr = line;
char delim[] = " ";
/* prepare data */
char *day = strtok_r(ptr, delim, &saveptr);
char *d_m = strtok_r(NULL, delim, &saveptr);
char *time = strtok_r(NULL, delim, &saveptr);
char *home_team = strtok_r(NULL, delim, &saveptr);
char *tmp1 = strtok_r(NULL, delim, &saveptr);
char *away_team = strtok_r(NULL, delim, &saveptr);
char *home_score = strtok_r(NULL, delim, &saveptr);
char *tmp2 = strtok_r(NULL, delim, &saveptr);
char *away_score = strtok_r(NULL, delim, &saveptr);
char *spectators = strtok_r(NULL, delim, &saveptr);
int h_score = atoi(home_score);
int a_score = atoi(away_score);
char tmp_day[3];
char tmp_month[3];
memset(tmp_day, NULL, sizeof(tmp_day));
memset(tmp_month, NULL, sizeof(tmp_month));
strncpy(tmp_day, d_m, 2);
strncpy(tmp_month, d_m+3, 2);
/* store into data structure */
strncpy(Match[matchno].day_name, day, 4);
Match[matchno].day = atoi(tmp_day);
Match[matchno].month = atoi(tmp_month);
Match[matchno].hour = atof(time);
strncpy(Match[matchno].team_1, home_team, 4);
strncpy(Match[matchno].team_2, away_team, 4);
Match[matchno].team_1_goal = h_score;
Match[matchno].team_2_goal = a_score;
Match[matchno].viewer = atoi(spectators);
matchno++;
/* safty check */
if (matchno >= NUMOFMATCHES)
break;
}
}
/*
for (int i=0; i<matchno; i++) {
printf("M: %d, %s/%s => %d/%d\n", i, Match[i].team_1, Match[i].team_2, Match[i].team_1_goal, Match[i].team_2_goal);
}
*/
/* prepare team data struct */
for (int i=0; i<NUMOFTEAMS; i++) {
strcpy(Team[i].team_names, "");
Team[i].goals_scored = 0;
Team[i].goals_conceded = 0;
Team[i].points = 0;
}
for (int i=0; i<matchno; i++) {
/* we separate into 2 cases,
- found team_names in the array,
- or not found
handle for both home team and away team
*/
for (int j=0; j<NUMOFTEAMS; j++) {
if (strcmp(Team[j].team_names, "") == 0) {
int g1 = Match[i].team_1_goal;
int g2 = Match[i].team_2_goal;
strncpy(Team[j].team_names, Match[i].team_1, 4);
Team[j].goals_scored = g1;
Team[j].goals_conceded = g2;
Team[j].points = (g1>g2)?3:(g1==g2)?1:0;
// printf("NEW: %3s\t%3d\t%3d\t%3d\n", Team[j].team_names, Team[j].goals_scored, Team[j].goals_conceded, Team[j].points);
break;
}
if (strcmp(Team[j].team_names, Match[i].team_1) == 0) {
int g1 = Match[i].team_1_goal;
int g2 = Match[i].team_2_goal;
Team[j].goals_scored += g1;
Team[j].goals_conceded += g2;
Team[j].points += (g1>g2)?3:(g1==g2)?1:0;
// printf("...: %3s\t%3d\t%3d\t%3d\n", Team[j].team_names, Team[j].goals_scored, Team[j].goals_conceded, Team[j].points);
break;
}
}
for (int j=0; j<NUMOFTEAMS; j++) {
if (strcmp(Team[j].team_names, "") == 0) {
int g1 = Match[i].team_2_goal;
int g2 = Match[i].team_1_goal;
strncpy(Team[j].team_names, Match[i].team_2, 4);
Team[j].goals_scored = g1;
Team[j].goals_conceded = g2;
Team[j].points = (g1>g2)?3:(g1==g2)?1:0;
// printf("NEW: %3s\t%3d\t%3d\t%3d\n", Team[j].team_names, Team[j].goals_scored, Team[j].goals_conceded, Team[j].points);
break;
}
if (strcmp(Team[j].team_names, Match[i].team_2) == 0) {
int g1 = Match[i].team_2_goal;
int g2 = Match[i].team_1_goal;
Team[j].goals_scored += g1;
Team[j].goals_conceded += g2;
Team[j].points += (g1>g2)?3:(g1==g2)?1:0;
// printf("...: %3s\t%3d\t%3d\t%3d\n", Team[j].team_names, Team[j].goals_scored, Team[j].goals_conceded, Team[j].points);
break;
}
}
}
for (int i=0; i<NUMOFTEAMS; i++) {
printf("%3s\t%3d\t%3d\t%3d\n",
Team[i].team_names,
Team[i].goals_scored,
Team[i].goals_conceded,
Team[i].points
);
}
fclose(f);
}
Comments