-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeague.c
230 lines (199 loc) · 6.14 KB
/
League.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#include "League.h"
#include "sort_league.h"
#include <stdlib.h>
#include <string.h>
// Created by tuli on 01/05/2020.
//
//create league object given two files, teams and matches
League* LeagueCreate(char* league_name, char* teamfile, char* matchfile){
char* name;
League* league_p = (League*)malloc(sizeof(League));
if (!league_p){
LOG_ERR(memory allocation failed )
exit(1);
}
league_p->name= league_name;
read_teams(teamfile, league_p);
read_matches(matchfile, league_p);
return league_p;
}
//free allocated memory for league object and all sub allocated memory
void LeagueDestroy(League* league){
int i;
for (i=0; i<league->num_teams; i++){
TeamDestroy(league->teams[i]);
}
for (i=0; i<league->num_matches; i++){
MatchDestroy(league->matches[i]);
}
free(league);
}
//function reads teams from file, we assume known input configuration, delimiter = \n
//46 using getline to read from file, reallicating memory for each team line by line
void read_teams(char* filename, League* league) {
FILE *fp = fopen(filename, "r");
if (!fp) {
LOG_ERR(FILE NOT FOUND)
exit(1);
}
char *buf = NULL;
size_t buf_size = 0;
league->teams = NULL;
league->num_teams = 0;
while (getline(&buf, &buf_size, fp) != EOF) {
buf[strchr(buf, '\n') - buf] = '\0';
league->teams = ((Team **) realloc(league->teams, sizeof(Team) * (league->num_teams + 1)));
if(!league->teams){
LOG_ERR(MEMORY ALLOCATION FAILED )
exit(1);
}
league->teams[league->num_teams] = TeamCreate(buf);
league->num_teams++;
}
free(buf);
fclose(fp);
}
//function reads matches from file, we assume a known input configuration, delimiter = tab
//83 using getline to read from file, reallicating memory for each match line by line
//90 parsing line and updating league matches respectibly
void read_matches(char *filename, League *league) {
char* team1;
char* team2;
int goals1;
int goals2;
const char* s = "\t";
FILE *fp = fopen(filename, "r");
if (!fp) {
LOG_ERR(FILE NOT FOUND)
exit(1);
}
char *buf = NULL;
size_t buf_size = 0;
league->matches = NULL;
league->num_matches = 0;
while (getline(&buf, &buf_size, fp) != EOF) {
buf[strchr(buf, '\n') - buf] = '\0';
league->matches = ((Match **) realloc(league->matches, sizeof(Match) * (league->num_matches + 1)));
if(!league->matches){
LOG_ERR(MEMORY ALLOCATION FAILED )
exit(1);
}
team1=strtok(buf, s);
team2=strtok(NULL, s);
goals1 = atoi(strtok(NULL,s));
goals2 = atoi(strtok(NULL, s));
league->matches[league->num_matches] = MatchCreate(find_team(team1,league), find_team(team2,league), goals1, goals2);
league->num_matches++;
}
free(buf);
fclose(fp);
}
//function compares given string to all team names in league and returns the respective team object
Team* find_team(char* team, League* league){
int i;
for (i=0; i<league->num_teams; i++){
if (!strcmp((team),league->teams[i]->team_name)){
return league->teams[i];
}
}
LOG_ERR(TEAM DOES NOT EXIST)
exit(1);
}
int num_wins(League* league, Team* team){
int wins = 0;
int i;
for (i=0; i< league->num_matches; i++){
if (team_participated(team,league->matches[i])){
if (team_won(team,league->matches[i])){
wins++;
}
}
}
return wins;
}
int num_draws(League* league, Team* team) {
int draws = 0;
int i;
for (i=0; i< league->num_matches; i++){
if (team_participated(team,league->matches[i])){
if (match_tied(team,league->matches[i])) {
draws++;
}
}
}
return draws;
}
int num_losses(League* league, Team* team) {
int losses = 0;
int i;
for (i=0; i< league->num_matches; i++){
if (team_participated(team,league->matches[i])){
if (team_won(team,league->matches[i])) {
losses++;
}
}
}
return losses;
}
int num_GF(League* league, Team* team) {
int GF = 0;
int i;
for (i=0; i< league->num_matches; i++){
if (team_participated(team,league->matches[i])){
if (league->matches[i]->team1==team) {
GF+=league->matches[i]->goals1;
}
else{GF+=league->matches[i]->goals2;
}
}
}
return GF;
}
int num_GA(League* league, Team* team) {
int GF = 0;
int i;
for (i=0; i< league->num_matches; i++){
if (team_participated(team,league->matches[i])){
if (league->matches[i]->team1==team) {
GF+=league->matches[i]->goals2;
}
else{GF+=league->matches[i]->goals1;
}
}
}
return GF;
}
int num_games(League* league, Team* team) {
int games = 0;
int i;
for (i=0; i< league->num_matches; i++){
if (team_participated(team,league->matches[i])){
games++;
}
}
return games;
}
int num_points(League* league, Team* team){
int points=0;
int i;
for (i=0; i< league->num_matches; i++){
if (team_participated(team,league->matches[i])){
if (team_won(team,league->matches[i])) {
points+=PTS_FOR_WIN;
}
else if (match_tied(team,league->matches[i])){
points+=PTS_FOR_DRAW;
}
}
}
return points;
}
void print_table(League* liga){
int i;
printf("%s", liga->name);
printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t \n", "Teams", "Games", "Wins", "Ties", "Losses", "GF", "GA", "Points");
for (i=0; i<liga->num_teams;i++){
printf("%-10s\t%-10d\t%-10d\t%-10d\t%-10d\t%-10d\t%-10d\t%-10d\t \n", liga->teams[i]->team_name, num_games(liga,liga->teams[i]), num_wins(liga,liga->teams[i]), num_draws(liga,liga->teams[i]), num_losses(liga,liga->teams[i]), num_GF(liga,liga->teams[i]), num_GA(liga,liga->teams[i]), num_points(liga,liga->teams[i]));
}
}