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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <glib.h>
#define bufSize 256
static char abilityStr[27][5] = {
"ls", "st", "rs", "lw", "lf",
"cf", "rf", "rw", "lam", "cam",
"ram", "lm", "lcm", "cm", "rcm",
"rm", "lwb", "ldm", "cdm", "rdm",
"rwb", "lb", "lcb", "cb", "rcb",
"rb", "gk"
};
typedef struct {
char *sofifa_id, *short_name;
double value_eur, wage_eur;
char *club_name;
GHashTable *ability;
bool selected;
} Player;
static Player *player = NULL;
static size_t playerNum = 0;
static size_t playerSize = 0;
static char *cmpPosition;
static double budget[11] = {0};
void printTeam(Player *p, char *position) {
printf("%s/%s/", p->short_name, p->club_name);
double ability = GPOINTER_TO_INT(g_hash_table_lookup(p->ability, position));
g_print("%g/", ability);
printf("%d/%d\n", (int)(p->value_eur), (int)(p->wage_eur));
}
double getAbility(char *str) {
double a = 0, b = 0;
if(strchr(str, '+') != NULL) sscanf(str, "%lf+%lf", &a, &b);
else sscanf(str, "%lf", &a);
return a + b;
}
void parseLine(Player *p, char *token, int cnt) {
if(cnt == 0 || cnt == 2 || cnt == 14) {
char *str = calloc(strlen(token) + 1, sizeof(char));
strcpy(str, token);
if(cnt == 0) p->sofifa_id = str;
else if(cnt == 2) p->short_name = str;
else if(cnt == 14) p->club_name = str;
}
else if(cnt == 7 || cnt == 8) {
char **endPtr = NULL;
bool spaceFlag = true;
size_t tokenLen = strlen(token);
for(int i = 0; i < tokenLen; ++i) {
if(!isspace(token[i])) {
spaceFlag = false;
break;
}
}
double eur = spaceFlag ? -1 : strtod(token, endPtr);
if(cnt == 7) p->value_eur = eur;
else if(cnt == 8) p->wage_eur = eur;
}
else if(cnt >= 78 && cnt <= 104) {
g_hash_table_insert(p->ability, abilityStr[cnt - 78], GINT_TO_POINTER(getAbility(token)));
}
}
void getPlayer(FILE *fp) {
char *line = calloc(bufSize, sizeof(char));
size_t lineCnt = 0;
while((line = fgets(line, bufSize, fp)) != NULL) {
size_t len = strlen(line);
if(len == bufSize - 1 && line[len - 1] != '\n') {
int newLen = len << 1;
line = realloc(line, newLen);
if(!line) {
printf("realloc failed\n");
exit(EXIT_FAILURE);
}
while(fgets(line + len, newLen - len, fp) != NULL) {
len = strlen(line);
if(line[len - 1] == '\n') break;
newLen = len << 1;
line = realloc(line, newLen);
if(!line) {
printf("realloc failed\n");
exit(EXIT_FAILURE);
}
}
}
char *find = strstr(line, ",,");
while(find != NULL) {
char *tmp = calloc(strlen(line) + 2, sizeof(char));
tmp[strlen(line) + 1] = '\0';
strncpy(tmp, line, find - line + 1);
tmp[find - line + 1] = ' ';
strncat(tmp + (find - line) + 2, find + 1, strlen(find + 1));
free(line);
line = tmp;
find = strstr(line, ",,");
}
if(lineCnt == 0) {
++lineCnt;
continue;
}
if(playerNum == playerSize) {
playerSize = playerSize == 0 ? 1 : playerSize << 1;
player = realloc(player, playerSize * sizeof(Player));
if(!player) {
printf("realloc failed\n");
exit(EXIT_FAILURE);
}
for(size_t i = playerNum; i < playerSize; ++i) {
player[i].ability = g_hash_table_new(g_str_hash, g_str_equal);
player[i].selected = false;
}
}
int cnt = 0;
char *token = strtok(line, ",");
while(token != NULL) {
if(token[0] == '\"') {
char *tmp = calloc(strlen(token) + 1, sizeof(char));
strncpy(tmp, token + 1, strlen(token));
tmp[strlen(tmp)] = ',';
token = strtok(NULL, "\"");
tmp = realloc(tmp, strlen(tmp) + strlen(token) + 1);
strncat(tmp, token, strlen(token));
tmp[strlen(tmp)] = '\0';
parseLine(&player[playerNum], tmp, cnt);
free(tmp);
}
else parseLine(&player[playerNum], token, cnt);
token = strtok(NULL, ",");
++cnt;
}
++lineCnt;
++playerNum;
}
free(line);
}
int cmpDouble(const double a, const double b, bool flag) {
if(flag) return (a > b) ? -1 : (a < b) ? 1 : 0; // greater
return (a < b) ? -1 : (a > b) ? 1 : 0; // less
}
int cmpString(const char *a, const char *b, bool flag) {
if(flag) return strcmp(b, a);
return strcmp(a, b);
}
int cmpAbility(const void *a, const void *b) {
const Player *c = (Player *)a;
const Player *d = (Player *)b;
return cmpDouble(
GPOINTER_TO_INT(g_hash_table_lookup(c->ability, cmpPosition)),
GPOINTER_TO_INT(g_hash_table_lookup(d->ability, cmpPosition)),
true
);
}
int cmpBudget(const void *a, const void *b) {
Player *c = (Player *)a;
Player *d = (Player *)b;
return cmpDouble(c->value_eur, d->value_eur, false);
}
int cmpSofifaId(const void *a, const void *b) {
Player *c = (Player *)a;
Player *d = (Player *)b;
return cmpString(c->sofifa_id, d->sofifa_id, false);
}
void getTeam(char *position, int pos) {
qsort(player, playerNum, sizeof(Player), cmpBudget);
Player *tmpPlayer = NULL;
size_t tmpPlayerNum = 0;
size_t tmpPlayerSize = 0;
for(int i = 0; i < playerNum; ++i) {
if(!player[i].selected && player[i].value_eur >= 0 && player[i].value_eur <= budget[pos]) {
if(tmpPlayerNum == tmpPlayerSize) {
tmpPlayerSize = tmpPlayerSize == 0 ? 1 : tmpPlayerSize << 1;
tmpPlayer = realloc(tmpPlayer, tmpPlayerSize * sizeof(Player));
if(!tmpPlayer) {
printf("realloc failed\n");
exit(EXIT_FAILURE);
}
}
tmpPlayer[tmpPlayerNum++] = player[i];
}
}
if(tmpPlayerNum == 0) {
printf("No player can be selected\n");
return;
}
cmpPosition = position;
qsort(tmpPlayer, tmpPlayerNum, sizeof(Player), cmpAbility);
size_t sameAbilityNum = 1;
double maxAbility = GPOINTER_TO_INT(g_hash_table_lookup(tmpPlayer[0].ability, position));
for(int i = 1; i < tmpPlayerNum; ++i) {
double ability = GPOINTER_TO_INT(g_hash_table_lookup(tmpPlayer[i].ability, position));
if(ability == maxAbility) ++sameAbilityNum;
else break;
}
if(sameAbilityNum == 1) printTeam(&tmpPlayer[0], position);
else {
qsort(tmpPlayer, sameAbilityNum, sizeof(Player), cmpBudget);
size_t sameBudgetNum = 1;
double minBudget = tmpPlayer[0].value_eur;
for(int i = 1; i < sameAbilityNum; ++i) {
if(tmpPlayer[i].value_eur == minBudget) ++sameBudgetNum;
else break;
}
if(sameBudgetNum == 1) printTeam(&tmpPlayer[0], position);
else {
qsort(tmpPlayer, sameBudgetNum, sizeof(Player), cmpSofifaId);
printTeam(&tmpPlayer[0], position);
}
}
for(int i = 0; i < playerNum; ++i) {
if(strcmp(player[i].sofifa_id, tmpPlayer[0].sofifa_id) == 0) {
player[i].selected = true;
break;
}
}
free(tmpPlayer);
}
int main() {
char file[100];
printf("Please enter the dataset: ");
fgets(file, 100, stdin);
if(file[strlen(file) - 1] == '\n') file[strlen(file) - 1] = '\0';
FILE *fp = fopen(file, "rt");
if(!fp) exit(EXIT_FAILURE);
getPlayer(fp);
fclose(fp);
char inSquad[100];
printf("Please enter the squad: ");
fgets(inSquad, 100, stdin);
if(inSquad[strlen(inSquad) - 1] == '\n') inSquad[strlen(inSquad) - 1] = '\0';
char **squad = calloc(11, sizeof(char *));
int squadNum = 0;
char *token = strtok(inSquad, " ");
while(token != NULL) {
squad[squadNum] = calloc(strlen(token) + 1, sizeof(char));
strncpy(squad[squadNum], token, strlen(token));
++squadNum;
token = strtok(NULL, " ");
}
if(strcmp(squad[10], "gk") != 0) {
printf("gk must be the last one of the team.\n");
exit(EXIT_FAILURE);
}
printf("Budget:\n");
for(int i = 0; i < 11; ++i) {
printf("\t%s: ", squad[i]);
scanf("%lf", &budget[i]);
}
printf("Team:\n");
for(int i = 0; i < 11; ++i) {
printf("\t%s: ", squad[i]);
getTeam(squad[i], i);
}
return 0;
}
|