Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1. /*Khai Quang Ho, kho19@student.aau.dk, A402, SW */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define FILENAME "kampe-2018-2019.txt"
  7. #define MAX_TEAMS 14
  8. #define MAX_GAMES 182
  9. #define MAX_STR 5
  10. #define MAX_LINE 60
  11.  
  12. typedef struct game{
  13.     char weekday[MAX_STR];
  14.     char date[MAX_STR];
  15.     char time[MAX_STR];
  16.     char teamA_name[MAX_STR];
  17.     char teamB_name[MAX_STR];
  18.     int teamA_goals;
  19.     int teamB_goals;
  20.     int crowd_size;
  21. }game;
  22.  
  23. typedef struct team{
  24.     char name[MAX_STR];
  25.     int points;
  26.     int goalsFor;
  27.     int goalsAgainst;
  28. }team;
  29.  
  30. int read_games(const char *filename, game game_list[]);
  31. int h(const char name[], team team_list[]);
  32. void get_team_stats(team *team_list, const game *game_list);
  33. int sort (const void *a, const void *b);
  34. void print_teams(team team_list[]);
  35.  
  36. int main(void) {
  37.  
  38.     game game_list[MAX_GAMES] = {{{0}}};
  39.     team team_list[MAX_TEAMS] = {{{0}}};
  40.  
  41.     if(read_games(FILENAME, game_list) == -1){
  42.         printf("Can't open file \"%s\"\n", FILENAME);
  43.         return -1;
  44.     }
  45.  
  46.     get_team_stats(team_list, game_list);
  47.     qsort(team_list, MAX_TEAMS, sizeof(team), sort);
  48.     print_teams(team_list);
  49.  
  50.     return 0;
  51. }
  52.  
  53. /* reads file and stores data in game_list */
  54. int read_games(const char filename[], game game_list[]){
  55.  
  56.     int i = 0;
  57.     char str[MAX_LINE];
  58.  
  59.     FILE *fp;
  60.     fp = fopen(filename, "r");
  61.  
  62.     if(fp != NULL){
  63.  
  64.         for(i = 0; fgets(str, MAX_LINE, fp) != NULL; i++){
  65.  
  66.             sscanf(str, "%s %s %s %s - %s %d - %d %d",
  67.                 game_list[i].weekday,
  68.                 game_list[i].date,
  69.                 game_list[i].time,
  70.                 game_list[i].teamA_name,
  71.                 game_list[i].teamB_name,
  72.                 &game_list[i].teamA_goals,
  73.                 &game_list[i].teamB_goals,
  74.                 &game_list[i].crowd_size);
  75.         }
  76.     }
  77.  
  78.     else
  79.         return -1; /* file could not be opened */
  80.  
  81.     fclose(fp);
  82.  
  83.     return 0;
  84. }
  85.  
  86. /* takes data from game_list and store relevant data to team_list*/
  87. void get_team_stats(team *team_list, const game *game_list){
  88.  
  89.     int i;
  90.  
  91.     for(i = 0; i < MAX_GAMES; i++){
  92.  
  93.         /* shorthands*/
  94.         game current = game_list[i];
  95.         int teamA = h(game_list[i].teamA_name, team_list);
  96.         int teamB = h(game_list[i].teamB_name, team_list);
  97.  
  98.         /* copies name into team struct from game struct */
  99.         strcpy(team_list[teamA].name, current.teamA_name);
  100.         strcpy(team_list[teamB].name, current.teamB_name);
  101.  
  102.         /* increment goals for and against and assign name*/
  103.         team_list[teamA].goalsFor += current.teamA_goals;
  104.         team_list[teamA].goalsAgainst += current.teamB_goals;
  105.         team_list[teamB].goalsFor += current.teamB_goals;
  106.         team_list[teamB].goalsAgainst += current.teamA_goals;
  107.  
  108.         /* calc points */
  109.         if(current.teamA_goals > current.teamB_goals){
  110.             team_list[teamA].points += 3;
  111.         }
  112.         else if(current.teamB_goals > current.teamA_goals){
  113.             team_list[teamB].points += 3;
  114.         }
  115.         else if(current.teamA_goals == current.teamB_goals){
  116.             team_list[teamA].points += 1;
  117.             team_list[teamB].points += 1;
  118.         }
  119.     }
  120. }
  121.  
  122. /* convert team name to index */
  123. int h( const char name[], team team_list[]){
  124.  
  125.     int key = (unsigned int)name % MAX_TEAMS; /* modulo can return negative values in c */
  126.     int i = key;
  127.  
  128.     while(strcmp(team_list[i].name, "") != 0 && strcmp(team_list[i].name, name) != 0)
  129.         i = (i + 1) % MAX_TEAMS;
  130.  
  131.     /* if team name doesnt exist in list, insert it*/
  132.     if(strcmp(team_list[i].name, "") == 0)
  133.         strcpy(team_list[i].name, name);
  134.  
  135.     return i;
  136. }
  137.  
  138. /* sorts teams after superliga rules */
  139. int sort (const void *a, const void *b){
  140.  
  141.     const team teamA = *(team *)a;
  142.     const team teamB = *(team *)b;
  143.  
  144.     int valueA = teamA.points;
  145.     int valueB = teamB.points;
  146.  
  147.     if(valueA == valueB){
  148.         valueA = teamA.goalsFor - teamA.goalsAgainst;
  149.         valueB = teamB.goalsFor - teamB.goalsAgainst;
  150.  
  151.         if(valueA == valueB){
  152.             valueA = teamA.goalsFor;
  153.             valueB = teamB.goalsFor;
  154.  
  155.             if(valueA == valueB){
  156.                 valueA = teamB.goalsAgainst;
  157.                 valueB = teamA.goalsAgainst;
  158.             }
  159.         }
  160.     }
  161.  
  162.     return ( ( valueB > valueA ) - ( valueA > valueB )  );
  163. }
  164.  
  165. /* prints out the team standing */
  166. void print_teams(team team_list[]){
  167.  
  168.     int i;
  169.  
  170.     printf("  %4s | %-4s | %-4s | %-4s | %-4s | %-4s |\n\n", "Pos", "Team", "Pts", "GD", "GF", "GA");
  171.  
  172.  
  173.     for(i = 0; i < MAX_TEAMS; i++){
  174.         printf("  %4d | %-4s | %-4d | %-+4d | %-4d | %-4d |\n",
  175.         i + 1,
  176.         team_list[i].name,
  177.         team_list[i].points,
  178.         team_list[i].goalsFor - team_list[i].goalsAgainst,
  179.         team_list[i].goalsFor,
  180.         team_list[i].goalsAgainst);
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement