Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Khai Quang Ho, kho19@student.aau.dk, A402, SW */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define FILENAME "kampe-2018-2019.txt"
- #define MAX_TEAMS 14
- #define MAX_GAMES 182
- #define MAX_STR 5
- #define MAX_LINE 60
- typedef struct game{
- char weekday[MAX_STR];
- char date[MAX_STR];
- char time[MAX_STR];
- char teamA_name[MAX_STR];
- char teamB_name[MAX_STR];
- int teamA_goals;
- int teamB_goals;
- int crowd_size;
- }game;
- typedef struct team{
- char name[MAX_STR];
- int points;
- int goalsFor;
- int goalsAgainst;
- }team;
- int read_games(const char *filename, game game_list[]);
- int h(const char name[], team team_list[]);
- void get_team_stats(team *team_list, const game *game_list);
- int sort (const void *a, const void *b);
- void print_teams(team team_list[]);
- int main(void) {
- game game_list[MAX_GAMES] = {{{0}}};
- team team_list[MAX_TEAMS] = {{{0}}};
- if(read_games(FILENAME, game_list) == -1){
- printf("Can't open file \"%s\"\n", FILENAME);
- return -1;
- }
- get_team_stats(team_list, game_list);
- qsort(team_list, MAX_TEAMS, sizeof(team), sort);
- print_teams(team_list);
- return 0;
- }
- /* reads file and stores data in game_list */
- int read_games(const char filename[], game game_list[]){
- int i = 0;
- char str[MAX_LINE];
- FILE *fp;
- fp = fopen(filename, "r");
- if(fp != NULL){
- for(i = 0; fgets(str, MAX_LINE, fp) != NULL; i++){
- sscanf(str, "%s %s %s %s - %s %d - %d %d",
- game_list[i].weekday,
- game_list[i].date,
- game_list[i].time,
- game_list[i].teamA_name,
- game_list[i].teamB_name,
- &game_list[i].teamA_goals,
- &game_list[i].teamB_goals,
- &game_list[i].crowd_size);
- }
- }
- else
- return -1; /* file could not be opened */
- fclose(fp);
- return 0;
- }
- /* takes data from game_list and store relevant data to team_list*/
- void get_team_stats(team *team_list, const game *game_list){
- int i;
- for(i = 0; i < MAX_GAMES; i++){
- /* shorthands*/
- game current = game_list[i];
- int teamA = h(game_list[i].teamA_name, team_list);
- int teamB = h(game_list[i].teamB_name, team_list);
- /* copies name into team struct from game struct */
- strcpy(team_list[teamA].name, current.teamA_name);
- strcpy(team_list[teamB].name, current.teamB_name);
- /* increment goals for and against and assign name*/
- team_list[teamA].goalsFor += current.teamA_goals;
- team_list[teamA].goalsAgainst += current.teamB_goals;
- team_list[teamB].goalsFor += current.teamB_goals;
- team_list[teamB].goalsAgainst += current.teamA_goals;
- /* calc points */
- if(current.teamA_goals > current.teamB_goals){
- team_list[teamA].points += 3;
- }
- else if(current.teamB_goals > current.teamA_goals){
- team_list[teamB].points += 3;
- }
- else if(current.teamA_goals == current.teamB_goals){
- team_list[teamA].points += 1;
- team_list[teamB].points += 1;
- }
- }
- }
- /* convert team name to index */
- int h( const char name[], team team_list[]){
- int key = (unsigned int)name % MAX_TEAMS; /* modulo can return negative values in c */
- int i = key;
- while(strcmp(team_list[i].name, "") != 0 && strcmp(team_list[i].name, name) != 0)
- i = (i + 1) % MAX_TEAMS;
- /* if team name doesnt exist in list, insert it*/
- if(strcmp(team_list[i].name, "") == 0)
- strcpy(team_list[i].name, name);
- return i;
- }
- /* sorts teams after superliga rules */
- int sort (const void *a, const void *b){
- const team teamA = *(team *)a;
- const team teamB = *(team *)b;
- int valueA = teamA.points;
- int valueB = teamB.points;
- if(valueA == valueB){
- valueA = teamA.goalsFor - teamA.goalsAgainst;
- valueB = teamB.goalsFor - teamB.goalsAgainst;
- if(valueA == valueB){
- valueA = teamA.goalsFor;
- valueB = teamB.goalsFor;
- if(valueA == valueB){
- valueA = teamB.goalsAgainst;
- valueB = teamA.goalsAgainst;
- }
- }
- }
- return ( ( valueB > valueA ) - ( valueA > valueB ) );
- }
- /* prints out the team standing */
- void print_teams(team team_list[]){
- int i;
- printf(" %4s | %-4s | %-4s | %-4s | %-4s | %-4s |\n\n", "Pos", "Team", "Pts", "GD", "GF", "GA");
- for(i = 0; i < MAX_TEAMS; i++){
- printf(" %4d | %-4s | %-4d | %-+4d | %-4d | %-4d |\n",
- i + 1,
- team_list[i].name,
- team_list[i].points,
- team_list[i].goalsFor - team_list[i].goalsAgainst,
- team_list[i].goalsFor,
- team_list[i].goalsAgainst);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement