Demo entry 6328605

Cool

   

Submitted by anonymous on Nov 27, 2016 at 23:43
Language: C. Code size: 15.0 kB.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NUM_OF_MATCHES 198
#define NUM_OF_ROUNDS 33
#define NUM_OF_MATCHES_PER_ROUND 6
#define NUM_OF_TEAMS 12
#define TEAM_STR_SIZE 4

typedef struct match{
  int roundnum;
  char day[4];
  int date;
  int month;
  int year;
  char time[6];
  char hometeam[TEAM_STR_SIZE];
  char awayteam[TEAM_STR_SIZE];
  int hometeamscore;
  int awayteamscore;
  double spectators;
} match;

typedef struct team{
  char team_name[4];
  int matches;
  int matches_won;
  int matches_lost;
  int matches_draw;
  int goals_for;
  int goals_against;
  int score_difference;
  int points;
} team;

typedef struct round{
  int number;
  int score;
} round;


typedef int (*fn_def)(char *, char *, char *, int, int);

void fill_in_matches_from_file(match *);
void num_draw_matches_10_goals_calc_print(match *);
void num_draw_matches_10_goals(match *, match *, int *);
int is_draw_game(match *);
int goals_in_round(match *);
int total_goals_of_match(match *);
void rounds_with_less_10_goals_calc_print(match *);
int rounds_with_less_10_goals(match *, round *);
int goals_round_rec(match *);
void team_away_wins(match *);
void fill_in_teams(match *, team *);
void more_away_wins(match *, team *, char [][TEAM_STR_SIZE], int *);
int home_away_win_calc(match *, char *);
void least_home_spectators_2015(match *, team *, char* , double *);
double spectators_of_year(match *, char *, int);
void time_span(match *, match *,char *, char *, char*, int*);
void char_replace(match *, char, char);
void spectator_multiplier(match *);
int num_matches(match *, char *);
int match_outcome(match *, char *, fn_def);
int is_won(char *, char *, char *, int , int);
int is_lost(char *, char *, char *, int , int);
int is_draw(char *, char *, char *, int , int);
int goals_for(match *, char *);
int goals_against(match *, char *);
int score_difference(match *, char *);
int calc_points(int, int);
void print_table(team *);
int compare_points(const void *, const void *);
int compare_score_difference(team *, team *);
int compare_most_goals(team *, team *);
void print_match_struct(match *, int);
void print_rounds_struct(round *, int);
void print_str_array(char [][TEAM_STR_SIZE], int);
void print_menu(void);

int main(int argc, char *argv[]){

  match matches[NUM_OF_MATCHES];
  team teams[NUM_OF_TEAMS];
  char lowest_spectator_team[TEAM_STR_SIZE];
  char team_more_wins_away[NUM_OF_TEAMS][TEAM_STR_SIZE];
  char k1[5],
       k2[5],
       weekday[5];
  int round_number = 0, 
      goals_in_round = 0,
      num_of_elements = 0,
      menu_selection = 0;
  double spectators = 0;
  match matches_between_k1_k2[NUM_OF_MATCHES]; 

  fill_in_matches_from_file(matches);
  fill_in_teams(matches, teams);
  qsort(teams, NUM_OF_TEAMS, sizeof(team), compare_points);
  
  if(argc > 1 && strcmp(argv[1], "--print") == 0)
    printf("Works");
  else{
    print_menu();
    scanf(" %d", &menu_selection);

    switch(menu_selection){
      case 1: num_draw_matches_10_goals_calc_print(matches); break;
      case 2: rounds_with_less_10_goals_calc_print(matches); break; 
      case 3: more_away_wins(matches, teams, team_more_wins_away, &num_of_elements); 
              print_str_array(team_more_wins_away, num_of_elements); break;
      case 4: least_home_spectators_2015(matches, teams, lowest_spectator_team, &spectators); 
              printf("Team: %s: %.3lf\n", lowest_spectator_team, spectators); break;     
      case 5: printf("Type in start and end time in HH:MM format and weekday in short format"); 
              scanf(" %s %s %s", k1, k2, weekday);
              time_span(matches, matches_between_k1_k2, k1, k2, weekday, &num_of_elements); 
              print_match_struct(matches_between_k1_k2, num_of_elements); break;
      case 6: print_table(teams); break;
    }
  }
  return 0;
}

void fill_in_matches_from_file(match *matches){
  FILE *ifp = fopen("superliga-2015-2016.txt", "r");
  int i = 0;
    while(fscanf(ifp," R%d %s %d/%d/%d %s %s - %s %d - %d %lf\n",
	 &matches[i].roundnum,
         matches[i].day,
 	 &matches[i].date,
	 &matches[i].month,
	 &matches[i].year,
	 matches[i].time,
         matches[i].hometeam,
         matches[i].awayteam,
         &matches[i].hometeamscore,
         &matches[i].awayteamscore,
	 &matches[i].spectators) != EOF)
      i++;
    fclose(ifp);
    char_replace(matches, '.', ':');
    spectator_multiplier(matches);
}

void char_replace(match *matches, char substitute, char substitutor){
  int i = 0;
  char* ptc;
  for(i = 0; i < NUM_OF_MATCHES; i++){
    ptc = strchr(matches[i].time, substitute);
    *ptc = substitutor;
  }
}

void spectator_multiplier(match *matches){
  int i = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++)
    matches[i].spectators *= 1000;
}

void fill_in_teams(match *matches, team *teams){
  int i = 0;
  for(i = 0; i < NUM_OF_MATCHES_PER_ROUND; i++){
    strcpy(teams[i].team_name, matches[i].hometeam); 
    strcpy(teams[NUM_OF_MATCHES_PER_ROUND+i].team_name, matches[i].awayteam);
  }
  for(i = 0; i < NUM_OF_TEAMS; i++){
    teams[i].matches = num_matches(matches, teams[i].team_name);
    teams[i].matches_won = match_outcome(matches, teams[i].team_name, is_won);
    teams[i].matches_lost = match_outcome(matches, teams[i].team_name, is_lost);
    teams[i].matches_draw = match_outcome(matches, teams[i].team_name, is_draw);
    teams[i].goals_for = goals_for(matches, teams[i].team_name);
    teams[i].goals_against = goals_against(matches, teams[i].team_name);
    teams[i].score_difference = score_difference(matches, teams[i].team_name);
    teams[i].points = calc_points(teams[i].matches_won, teams[i].matches_draw);
  }
}

int num_matches(match *matches, char *team_name){
  int i = 0, result = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++)
    result += !strcmp(matches[i].hometeam, team_name) || !strcmp(matches[i].awayteam, team_name);
  return result;
}

int match_outcome(match *matches, char *team_name, fn_def outcome_fn){
  int i = 0, result = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++)
    result += outcome_fn(team_name, matches[i].hometeam, matches[i].awayteam, matches[i].hometeamscore, matches[i].awayteamscore);
  return result;   
}

int is_won(char *team_check, char *team_home, char *team_away, int score_a, int score_b){
  return (!strcmp(team_check, team_home) && score_a > score_b) || (!strcmp(team_check, team_away) && score_a < score_b);
}

int is_lost(char *team_check, char *team_home, char *team_away, int score_a, int score_b){
  return (!strcmp(team_check, team_home) && score_a < score_b) || (!strcmp(team_check, team_away) && score_a > score_b);   
}

int is_draw(char *team_check, char *team_home, char *team_away, int score_a, int score_b){
  return (!strcmp(team_check, team_home) && score_a == score_b) || (!strcmp(team_check, team_away) && score_a == score_b);    
}

int goals_for(match *matches, char *team_name){
  int i = 0, result = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++)
    if(!strcmp(matches[i].hometeam, team_name))
      result += matches[i].hometeamscore;
    else if(!strcmp(matches[i].awayteam, team_name))
      result += matches[i].awayteamscore;
  return result;
}

int goals_against(match *matches, char *team_name){
  int i = 0, result = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++)
    if(!strcmp(matches[i].hometeam, team_name))
      result += matches[i].awayteamscore;
    else if(!strcmp(matches[i].awayteam, team_name))
      result += matches[i].hometeamscore;
  return result; 
}

int score_difference(match *matches, char *team_name){
  return goals_for(matches, team_name) - goals_against(matches, team_name);
}

int calc_points(int wins, int draws){
  return 3 * wins + draws;
}

/* Opgave 1 via parametre------------------------------------------------------------ */
void num_draw_matches_10_goals_calc_print(match *matches){
  int num_of_elements = 0;
  match draw_matches[NUM_OF_MATCHES];
  num_draw_matches_10_goals(matches, draw_matches, &num_of_elements);
  print_match_struct(draw_matches, num_of_elements);  
}

void num_draw_matches_10_goals(match *matches, match *draw_matches, int *num_of_matches){
  int i = 0, j = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++)
    if(is_draw_game(matches+i) && total_goals_of_match(matches+i) >= 4){
      draw_matches[j] = matches[i];
      j++;
    } 
  *num_of_matches = j;
}

int is_draw_game(match *matches){
  return matches->hometeamscore == matches->awayteamscore;
}

int total_goals_of_match(match *matches){
  return matches->hometeamscore + matches->awayteamscore;
}

/* Opgave 2  via parametre------------------------------------------------------------- */
void rounds_with_less_10_goals_calc_print(match *matches){
  round rounds[NUM_OF_ROUNDS];
  int num_rounds = 0;
  num_rounds = rounds_with_less_10_goals(matches, rounds);
  print_rounds_struct(rounds, num_rounds); 
}

int rounds_with_less_10_goals(match *matches, round *rounds){
  int goals = goals_round_rec(matches);
  if (matches->roundnum == NUM_OF_ROUNDS && goals < 10){

    rounds->number = matches->roundnum;
    rounds->score = goals;
    return 1;
  }
  else if(matches->roundnum == NUM_OF_ROUNDS)
    return 0;
  else if(goals < 10){
    rounds->number = matches->roundnum;
    rounds->score = goals;
    return 1 + rounds_with_less_10_goals(matches+6, rounds + 1);
  }
  else
    return rounds_with_less_10_goals(matches+6, rounds);
}

int goals_round_rec(match *matches){
  if(matches->roundnum != (matches+1)->roundnum)
    return total_goals_of_match(matches);
  else
    return total_goals_of_match(matches) + goals_round_rec(matches+1);
}

/* Opgave 3 -------------------------------------------------------------- */
void team_with_more_away_wins(match *matches, team *teams){
  

}

void more_away_wins(match *matches, team *teams, char team_array[][TEAM_STR_SIZE], int *num_of_elements){
  int i = 0, j = 0;;
  for(i = 0; i < NUM_OF_TEAMS; i++){
    if(home_away_win_calc(matches, teams[i].team_name)<0){
      strcpy(team_array[j], teams[i].team_name);
      j++;
    }
  }
  *num_of_elements = j;
}

int home_away_win_calc(match *matches, char *team_name){
  int i = 0, result = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++){
    if((strcmp(matches[i].hometeam,team_name) == 0) && (matches[i].hometeamscore > matches[i].awayteamscore))
      result++;
    else if((strcmp(matches[i].awayteam,team_name) == 0) && (matches[i].hometeamscore < matches[i].awayteamscore))
      result--;
  }
  return result;
}

/* Opgave 4 ---------------------------------------------------------------- */
void least_home_spectators_2015(match *matches, team *teams, char* lowest_spectator_team, double *spectators){
  int i = 0;
  double new_spectator = 0;
  double current_spectator = 0;

  current_spectator = spectators_of_year(matches, teams[i].team_name, 2015);
  for(i = 0; i < NUM_OF_TEAMS; i++){
    new_spectator = spectators_of_year(matches, teams[i].team_name, 2015);
    if(current_spectator > new_spectator){
      current_spectator = new_spectator;
      strcpy(lowest_spectator_team, teams[i].team_name);
    }
  } 
  *spectators =  current_spectator;
}

double spectators_of_year(match *matches, char *team_name, int year){
  int i = 0;
  double result = 0;
  while(matches[i].year == year){
    if(strcmp(matches[i].hometeam, team_name) == 0)
      result += matches[i].spectators;
    i++;
  }
  return result;
}

/* Opgave 5 -------------------------------------------------------------------- */
void time_span(match *matches, match *matches_between_k1_k2, char *k1, char *k2, char *u, int *num_of_elements){
  int i = 0, j = 0;
  for(i = 0; i < NUM_OF_MATCHES; i++){
    if((strcmp(k1, matches[i].time) <= 0 && strcmp(k2, matches[i].time) >= 0) && strcmp(u, matches[i].day) == 0){
      matches_between_k1_k2[j] = matches[i];
      j++;
    }
  }
  *num_of_elements = j;
}

/* Compare functions for qsort() ------------------------------------------------ */
int compare_points(const void *a, const void *b){
  int result = 0;
  team *team_a = (team *)a,
       *team_b = (team *)b;
  if (team_a->points > team_b->points)
    result = -1;
  else if (team_a->points < team_b->points)
    result = 1;
  else
    result = compare_score_difference(team_a, team_b);
  return result;
}

int compare_score_difference(team *team_a, team *team_b){
  int result = 0;
  if (team_a->score_difference > team_b->score_difference)
    result = -1;
  else if (team_a->score_difference < team_b->score_difference)
    result = 1;
  else
    result = compare_most_goals(team_a, team_b);
  return result;
}

int compare_most_goals(team *team_a, team *team_b){
  int result = 0;
  if (team_a->goals_for > team_b->goals_for)
    result = -1;
  else if (team_a->goals_for < team_b->goals_for)
    result = 1;
  else
    result = strcmp(team_a->team_name, team_b->team_name);
  return result;
}

/* Print functions ------------------------------------------------------------ */
void print_str_array(char team_array[][TEAM_STR_SIZE], int num_of_elements){
  int i = 0;
  for(i = 0; i < num_of_elements; i++)
    printf("%s\n", team_array[i]);
}

void print_table(team *teams){
  int i = 0;
  for(i = 0; i < NUM_OF_TEAMS; i++)
    printf("%2d. %-3s | %3d | %3d | %3d | %3d | %3d | %3d | %3d | %3d |\n",
	 i+1,
	 teams[i].team_name,
	 teams[i].matches,
         teams[i].matches_won,
	 teams[i].matches_lost,
         teams[i].matches_draw,
	 teams[i].goals_for,
	 teams[i].goals_against,
         teams[i].score_difference,
	 teams[i].points);        
}

void print_match_struct(match *matches, int num_of_elements){
  int i = 0;
  for(i = 0; i < num_of_elements; i++)
    printf("R%d %s %d/%d/%d %s %s - %s %d - %d %.0lf\n",
	 matches[i].roundnum,
         matches[i].day,
 	 matches[i].date,
	 matches[i].month,
	 matches[i].year,
	 matches[i].time,
         matches[i].hometeam,
         matches[i].awayteam,
         matches[i].hometeamscore,
         matches[i].awayteamscore,
	 matches[i].spectators);
}
void print_rounds_struct(round *rounds, int num_of_elem){
  int i = 0;
  for(i = 0; i < num_of_elem; i++)
    printf("Round: %d with %d goals\n", rounds[i].number, rounds[i].score);
}

void print_menu(void){
  printf("Select a number for what you want to do\n");
  printf("[1] Show matches that drawed and had 4 and more goals\n");
  printf("[2] Show rounds where the overall score i less than 10\n");
  printf("[3] Show teams that wins more away than home\n");
  printf("[4] Show the team with less spectators at home in 2015\n");
  printf("[5] Show matches between a chosen time period and weekday\n");
  printf("[6] Show a table of the league standing\n");
}

This snippet took 0.04 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).