-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2012_jun_2.c
94 lines (79 loc) · 1.98 KB
/
2012_jun_2.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_IN "kalendar.txt"
#define FILE_OUT "preklapanja.txt"
#define DESC_LEN 81
#define CHECK_ALLOC(p) if (!(p)) printf("Neuspesna alokacija\n"), exit(1)
#define CHECK_FILE(f) if (!(f)) printf("Neuspesno otvaranje fajla\n"), exit(2)
typedef struct elem {
unsigned start, finish;
char desc[DESC_LEN];
struct elem *next;
} Elem;
void read_activities(Elem **plist, FILE *fin)
{
unsigned sh, sm, fh, fm;
char desc[DESC_LEN];
while (fscanf(fin, "%u:%u-%u:%u %80[^\n]", &sh, &sm, &fh, &fm, desc) == 5) {
Elem *p = malloc(sizeof *p), *curr, *prev;
CHECK_ALLOC(p);
p->start = sh * 60 + sm;
p->finish = fh * 60 + fm;
strcpy(p->desc, desc);
curr = *plist;
prev = NULL;
while (curr && curr->start <= p->start) {
prev = curr;
curr = curr->next;
}
if (prev) {
prev->next = p;
} else {
*plist = p;
}
p->next = curr;
}
}
void remove_overlaps(Elem *list, FILE *fout)
{
Elem *p = list, *q;
while (p) {
q = p->next;
while (q && q->start < p->finish) {
unsigned sh, sm, fh, fm;
p->next = q->next;
sh = q->start / 60, sm = q->start % 60;
fh = q->finish / 60, fm = q->finish % 60;
fprintf(fout, "%u:%u-%u:%u %s\n", sh, sm, fh, fm, q->desc);
free(q);
q = p->next;
}
p = p->next;
}
}
void free_list(Elem **plist)
{
Elem *p;
while (*plist) {
p = *plist;
*plist = (*plist)->next;
free(p);
}
*plist = NULL;
}
int main(void)
{
FILE *fin, *fout;
Elem *list = NULL;
fin = fopen(FILE_IN, "r");
CHECK_FILE(fin);
read_activities(&list, fin);
fclose(fin);
fout = fopen(FILE_OUT, "w");
CHECK_FILE(fout);
remove_overlaps(list, fout);
fclose(fout);
free_list(&list);
return 0;
}