-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkDaysDate.c
152 lines (139 loc) · 2.73 KB
/
kDaysDate.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
// kDaysDate.c: Outputs new date k days after given date.
#include <stdio.h>
typedef struct Date
{
int yr;
char mon;
char day;
} date_t;
/* Prototypes */
// Return date of the next day of given date.
date_t get_next_date(date_t date);
// Return date k days after given date.
date_t get_date_after_k_days(date_t date, int k);
// Print date.
void print_date(date_t date);
// Determine whether a year is leap year.
int is_leap(int yr);
/* Definitions */
int main(void)
{
date_t date;
int k;
// Input.
scanf("%d%d%d%d", &date.yr, &date.mon, &date.day, &k);
// Print results.
print_date(get_date_after_k_days(date, k));
return 0;
}
date_t get_next_date(date_t date)
{
// Find number of days in current month.
int max_days;
if (date.mon == 2) // On February, set max_days according to year.
{
if (is_leap(date.yr))
{
max_days = 29;
}
else
{
max_days = 28;
}
}
else
{
if (date.mon <= 7) // Jan-July. Expt Feb.
{
if (date.mon % 2)
{
// Odd month.
max_days = 31;
}
else
{
// Even month.
max_days = 30;
}
}
else // Aug-Dec.
{
if (date.mon % 2)
{
// Odd month.
max_days = 30;
}
else
{
// Even month.
max_days = 31;
}
}
}
// Finish finding max_days.
// Compare and increase d/m/y.
// Check day carry.
if (date.day == max_days)
{
// Carry exists.
date.day = 1;
// Check month carry.
if (date.mon == 12)
{
// Carry exists.
date.mon = 1;
// Increase year by 1.
date.yr++;
}
else
{
// No carry.
date.mon++;
}
}
else
{
// No carry.
date.day++;
}
// Finish compare and increase d/m/y.
return date;
}
date_t get_date_after_k_days(date_t date, int k)
{
// Iterate k times to simulate k days.
for (int i = 0; i < k; i++)
{
date = get_next_date(date);
}
return date;
}
void print_date(date_t date)
{
printf("%d-%d-%d\n", date.yr, date.mon, date.day);
}
int is_leap(int yr)
{
if (!(yr % 4))
{
if (!(yr % 100))
{
if (!(yr % 400))
{
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
else
{
return 0;
}
}