-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthdaySort.c
117 lines (99 loc) · 2.68 KB
/
birthdaySort.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
// birthdaySort.c: Print date and name in sorted order.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct infmt
{
char s[51];
int year, month, day;
};
void printSortedBirthdays(struct infmt a[], int n);
int birthdayCompare(const void *a, const void *b);
void parseInput(struct infmt a[], int n);
int main(void)
{
int n;
struct infmt a[50003];
// Input.
scanf("%d", &n);
parseInput(a, n);
printSortedBirthdays(a, n);
return 0;
}
void parseInput(struct infmt a[], int n)
{
char dateBuf[11];
char parseBuf[11];
char *dlim = NULL;
for (int i = 0; i < n; i++)
{
scanf("%s%s", &a[i].s, &dateBuf);
// Find first colon.
dlim = strstr(dateBuf, ":");
// Copy year to buffer.
strncpy(parseBuf, dateBuf, dlim - dateBuf);
// Null terminate.
parseBuf[dlim - dateBuf] = '\0';
a[i].year = atoi(parseBuf);
// Remove year from buffer.
strcpy(dateBuf, dlim + 1);
// Find second colon.
dlim = strstr(dateBuf, ":");
// Copy month to buffer.
strncpy(parseBuf, dateBuf, dlim - dateBuf);
// Null terminate.
parseBuf[dlim - dateBuf] = '\0';
a[i].month = atoi(parseBuf);
// Remove month from buffer.
strcpy(dateBuf, dlim + 1);
// Copy day to struct.
a[i].day = atoi(dateBuf);
}
}
int birthdayCompare(const void *a, const void *b)
{
struct infmt *c, *d;
c = (struct infmt *)a;
d = (struct infmt *)b;
int cToken, dToken;
cToken = c->day + c->month * 100;
dToken = d->day + d->month * 100;
if (cToken == dToken) // Same date, sort by name.
{
for (int i = 0; 1; i++)
{
if (c->s[i] != d->s[i])
{
return c->s[i] - d->s[i];
}
}
}
else
{
return cToken - dToken;
}
}
void printSortedBirthdays(struct infmt a[], int n)
{
// Sort in acsending order.
qsort(a, n, sizeof(struct infmt), birthdayCompare);
int dateToken = a[0].month * 100 + a[0].day;
int lastDateToken;
// Print birthdays.
// Print LF on next output if token is different.
printf("%d:%d %s", a[0].month, a[0].day, a[0].s);
for (int i = 1; i < n; i++)
{
lastDateToken = dateToken;
dateToken = a[i].month * 100 + a[i].day;
if (lastDateToken == dateToken) // Same date.
{
printf(" %s", a[i].s);
}
else
{
printf("\n%d:%d %s", a[i].month, a[i].day, a[i].s);
}
}
printf("\n");
}