-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
83 lines (71 loc) · 2.03 KB
/
main.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
#include <stdio.h>
struct date {
int month;
int day;
int year;
};
int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leapyear(int year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
int validdate(struct date now) {
int daysmon;
if (now.year <= 1582) // pre-Gregorian
return 0;
if (now.month < 1 || now.month > 12)
return 0;
daysmon = daysPerMonth[now.month-1];
if (leapyear(now.year) && now.month == 2)
daysmon++;
if (now.day < 1 || now.day > daysmon)
return 0;
return 1;
}
void bumpdate(struct date* now,struct date* Tom,struct date* Yes ) {
int daysmon = daysPerMonth[now->month-1];
int T,Y,To,Ye;
*Tom=*now;
*Yes=*now;
To=daysmon;
Ye=daysmon;
if (leapyear(now->year) && now->month == 2)To++;
//================================
if (++Tom->day > To)
{
Tom->day = 1;
if (++Tom->month > 12)
{
Tom->month = 1;
Tom->year++;
}
}
//================================
if (--Yes->day<1)
{
if(now->month>=2)Yes->day =daysPerMonth[now->month-2];
else {Yes->day =daysPerMonth[now->month+10];Yes->year--;}
if (leapyear(now->year) && now->month == 3)Yes->day++;
if (--Yes->month<1)
{
Yes->month = 12;
}
}
}
int main (void) {
struct date today, tomorrow ,yesterday,tom,yes;
while(1){
printf("Enter today's date (dd/mm/yyyy): ");
if (3 != scanf("%d %*[/-] %d %*[/-] %d", &today.day, &today.month, &today.year)) {
printf ("Need the proper date format\n");
return 1;
}
if (!validdate(today)) {
printf ("Invalid date\n");
return 1;
}
bumpdate(&today,&tom,&yes);
printf("Tomorrow's date is %02d/%02d/%04d\n", tom.day,tom.month, tom.year);
printf("Yesterday's date is %02d/%02d/%04d\n", yes.day,yes.month, yes.year);
//return 0;
}
}