forked from sushantvaidkar/HacktoberFest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextday.cpp
124 lines (114 loc) · 2.95 KB
/
Nextday.cpp
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
#include <stdio.h>
#include<ctype.h>
typedef enum month { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec } month;
typedef struct date { enum month m; int d; int y; } date;
static const char * const month_name[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
};
void print_month(struct date date)
{ //simple function for displaying month and day
switch (date.m)
{
case jan:
case feb:
case mar:
case apr:
case may:
case jun:
case jul:
case aug:
case sep:
case oct:
case nov:
case dec:
printf("%s %d, %d\n", month_name[date.m], date.d, date.y);
break;
default:
printf("Out of range!\n");
break;
}
}
date next_day(struct date next) { //next month|day function which is the problem.
int last_day;
switch (next.m) {
case jan:
case mar:
case may:
case jul:
case aug:
case oct:
case dec:
last_day = 31;
break;
case apr:
case jun:
case sep:
case nov:
last_day = 30;
break;
case feb:
// see https://stackoverflow.com/questions/3220163/how-to-find-leap-year-programmatically-in-c
if (next.y % 4 == 0 && (next.y % 100 != 0 || next.y % 400 == 0))
last_day = 29;
else
last_day = 28;
break;
default:
return next;
}
if (next.d++ == last_day) {
next.d = 1;
if (next.m++ == dec) {
next.m = jan;
next.y++;
}
}
return next;
}
int main() {
struct date date_1 = { feb, 28, 2020 };
struct date date_2 = { mar, 14, 2020 };
struct date date_3 = { oct, 31, 2020 };
struct date date_4 = { dec, 31, 2020 };
struct date date_5 = { feb, 28, 2021 };
print_month(date_1); // see https://stackoverflow.com/questions/3220163/how-to-find-leap-year-programmatically-in-c
if (next.y % 4 == 0 && (next.y % 100 != 0 || next.y % 400 == 0))
last_day = 29;
else
last_day = 28;
break;
default:
return next;
}
if (next.d++ == last_day) {
next.d = 1;
if (next.m++ == dec) {
next.m = jan;
next.y++;
}
}
return next;
}
int main() {
struct date date_1 = { feb, 28, 2020 };
struct date date_2 = { mar, 14, 2020 };
struct date date_3 = { oct, 31, 2020 };
struct date date_4 = { dec, 31, 2020 };
struct date date_5 = { feb, 28, 2021 };
print_month(date_1);
print_month(date_2);
print_month(date_3);
print_month(date_4);
print_month(date_5);
printf("\n");
print_month(next_day(date_1));
print_month(next_day(date_2));
print_month(next_day(date_3));
print_month(next_day(date_4));
print_month(next_day(date_5));
printf("\n\n");
return 0;
}