-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
63 lines (53 loc) · 1.6 KB
/
history.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
#ifndef HISTORY_C
#define HISTORY_C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "history.h"
history_t *history_new() {
history_t *history = malloc(sizeof(history_t));
*history = (history_t) {
0,
{ 0, 0 },
{ 0, 0 },
{},
0
};
return history;
}
void history_add_match(history_t *history, match_t *match) {
if (match->score_a > match->score_b) {
history->wons[0]++;
}
else if (match->score_a < match->score_b) {
history->wons[1]++;
}
else {
history->draws++;
}
history->goals[0] += match->score_a;
history->goals[1] += match->score_b;
char* match_score = malloc(sizeof(char) * 6);
snprintf(match_score, 6, "%2d-%2d", match->score_a, match->score_b);
for (int i = 0; i < history->scores_arr_length; i++) {
if (strcmp(history->scores[i].score, match_score) == 0) {
history->scores[i].ocurrences++;
return;
}
}
history->scores[history->scores_arr_length] = (history_score_t) { match_score, 1 };
history->scores_arr_length++;
}
void history_print(history_t *history) {
printf("team a wons: %d\r\n", history->wons[0]);
printf("team b wons: %d\r\n", history->wons[1]);
printf("draws: %d\r\n", history->draws);
printf("team a goals: %d\r\n", history->goals[0]);
printf("team b goals: %d\r\n", history->goals[1]);
printf("scores:\r\n");
for (int i = 0; i < history->scores_arr_length; i++) {
printf(" %s: %d\r\n", history->scores[i].score, history->scores[i].ocurrences);
}
}
#endif