Advertisement
chatchai_j

Danish super league 2019-2020 // in c

Nov 27th, 2020
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define INPUTFILE   "matches-2019-2020.txt"
  6. #define LINESZ      1024
  7.  
  8. #define NUMOFTEAMS  14
  9. #define NUMOFMATCHES    182
  10.  
  11. typedef struct match
  12. {
  13.     char day_name[4];
  14.     int day;
  15.     int month;
  16.     float hour;
  17.     char team_1[4];
  18.     char team_2[4];
  19.     int team_1_goal;
  20.     int team_2_goal;
  21.     int viewer;
  22. } match;
  23.  
  24. typedef struct team
  25. {
  26.     char team_names[4];
  27.     int goals_scored;
  28.     int goals_conceded;
  29.     int points;
  30. } team;
  31.  
  32. match   Match[NUMOFMATCHES];
  33. team    Team[NUMOFTEAMS];
  34.  
  35. int main(void) {
  36.     FILE    *f;
  37.     char    line[LINESZ];
  38.     int matchno = 0;
  39.    
  40.     f = fopen(INPUTFILE, "r");
  41.     if (f ==(FILE *)NULL) {
  42.         fprintf(stderr, "Can't open file '%s'\n", INPUTFILE);
  43.         exit(-1);
  44.     }
  45.  
  46.     /* read matches data from file, and store data into Match struct */
  47.     while(1) {
  48.         char    *str;
  49.         char    *saveptr;
  50.  
  51.         if (feof(f))
  52.             break;
  53.  
  54.         str = fgets(line, LINESZ, f);
  55.  
  56.         if (str != (char *)NULL) {
  57.             char *ptr = line;
  58.             char delim[] = " ";
  59.  
  60.             /* prepare data */
  61.             char *day    = strtok_r(ptr, delim, &saveptr);
  62.             char *d_m    = strtok_r(NULL, delim, &saveptr);
  63.             char *time   = strtok_r(NULL, delim, &saveptr);
  64.             char *home_team  = strtok_r(NULL, delim, &saveptr);
  65.             char *tmp1   = strtok_r(NULL, delim, &saveptr);
  66.             char *away_team  = strtok_r(NULL, delim, &saveptr);
  67.             char *home_score = strtok_r(NULL, delim, &saveptr);
  68.             char *tmp2   = strtok_r(NULL, delim, &saveptr);
  69.             char *away_score = strtok_r(NULL, delim, &saveptr);
  70.             char *spectators = strtok_r(NULL, delim, &saveptr);
  71.  
  72.             int h_score = atoi(home_score);
  73.             int a_score = atoi(away_score);
  74.  
  75.             char tmp_day[3];
  76.             char tmp_month[3];
  77.  
  78.             memset(tmp_day, NULL, sizeof(tmp_day));
  79.             memset(tmp_month, NULL, sizeof(tmp_month));
  80.  
  81.             strncpy(tmp_day, d_m, 2);
  82.             strncpy(tmp_month, d_m+3, 2);
  83.  
  84.             /* store into data structure */
  85.             strncpy(Match[matchno].day_name, day, 4);
  86.             Match[matchno].day = atoi(tmp_day);
  87.             Match[matchno].month = atoi(tmp_month);
  88.             Match[matchno].hour = atof(time);
  89.             strncpy(Match[matchno].team_1, home_team, 4);
  90.             strncpy(Match[matchno].team_2, away_team, 4);
  91.             Match[matchno].team_1_goal = h_score;
  92.             Match[matchno].team_2_goal = a_score;
  93.             Match[matchno].viewer = atoi(spectators);
  94.  
  95.             matchno++;
  96.  
  97.             /* safty check */
  98.             if (matchno >= NUMOFMATCHES)
  99.                 break;
  100.         }
  101.     }
  102.  
  103. /*
  104.     for (int i=0; i<matchno; i++) {
  105.         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);
  106.     }
  107. */
  108.    
  109.     /* prepare team data struct */
  110.     for (int i=0; i<NUMOFTEAMS; i++) {
  111.         strcpy(Team[i].team_names, "");
  112.         Team[i].goals_scored = 0;
  113.         Team[i].goals_conceded = 0;
  114.         Team[i].points = 0;
  115.     }
  116.  
  117.     for (int i=0; i<matchno; i++) {
  118.         /* we separate into 2 cases,
  119.             - found team_names in the array,
  120.             - or not found
  121.           handle for both home team and away team
  122.         */
  123.         for (int j=0; j<NUMOFTEAMS; j++) {
  124.             if (strcmp(Team[j].team_names, "") == 0) {
  125.                 int g1 = Match[i].team_1_goal;
  126.                 int g2 = Match[i].team_2_goal;
  127.  
  128.                 strncpy(Team[j].team_names, Match[i].team_1, 4);
  129.                 Team[j].goals_scored = g1;
  130.                 Team[j].goals_conceded = g2;
  131.                 Team[j].points = (g1>g2)?3:(g1==g2)?1:0;
  132. // 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);
  133.                 break;
  134.             }
  135.  
  136.             if (strcmp(Team[j].team_names, Match[i].team_1) == 0) {
  137.                 int g1 = Match[i].team_1_goal;
  138.                 int g2 = Match[i].team_2_goal;
  139.  
  140.                 Team[j].goals_scored += g1;
  141.                 Team[j].goals_conceded += g2;
  142.                 Team[j].points += (g1>g2)?3:(g1==g2)?1:0;
  143. // printf("...: %3s\t%3d\t%3d\t%3d\n", Team[j].team_names, Team[j].goals_scored, Team[j].goals_conceded, Team[j].points);
  144.                 break;
  145.             }
  146.         }
  147.  
  148.         for (int j=0; j<NUMOFTEAMS; j++) {
  149.             if (strcmp(Team[j].team_names, "") == 0) {
  150.                 int g1 = Match[i].team_2_goal;
  151.                 int g2 = Match[i].team_1_goal;
  152.  
  153.                 strncpy(Team[j].team_names, Match[i].team_2, 4);
  154.                 Team[j].goals_scored = g1;
  155.                 Team[j].goals_conceded = g2;
  156.                 Team[j].points = (g1>g2)?3:(g1==g2)?1:0;
  157. // 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);
  158.                 break;
  159.             }
  160.  
  161.             if (strcmp(Team[j].team_names, Match[i].team_2) == 0) {
  162.                 int g1 = Match[i].team_2_goal;
  163.                 int g2 = Match[i].team_1_goal;
  164.  
  165.                 Team[j].goals_scored += g1;
  166.                 Team[j].goals_conceded += g2;
  167.                 Team[j].points += (g1>g2)?3:(g1==g2)?1:0;
  168. // printf("...: %3s\t%3d\t%3d\t%3d\n", Team[j].team_names, Team[j].goals_scored, Team[j].goals_conceded, Team[j].points);
  169.                 break;
  170.             }
  171.         }
  172.     }
  173.  
  174.     for (int i=0; i<NUMOFTEAMS; i++) {
  175.         printf("%3s\t%3d\t%3d\t%3d\n",
  176.             Team[i].team_names,
  177.             Team[i].goals_scored,
  178.             Team[i].goals_conceded,
  179.             Team[i].points
  180.         );
  181.     }
  182.    
  183.     fclose(f);
  184. }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement