-
Notifications
You must be signed in to change notification settings - Fork 0
/
calDate.cpp
249 lines (231 loc) · 5.48 KB
/
calDate.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// Created by Mr.GGLS on 2021/4/4.
//
#include "iostream"
using namespace ::std;
struct Date
{
int year;
int month;
int day;
};
Date initDate{1900,1,1};//Monday
int months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
const char* weekdays[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
void addOneDay(Date&);//add one day to the date
void addOneMonth(Date&);//add one month to the date
void addOneYear(Date&);//add one year to the date
bool isLeapYear(int);//is this year a leap year?
bool isLaterDate(Date d1,Date d2);//is d1>d2?
int calPassedDays(Date);//calculate all passed days in this year, including today
int calRestDays(Date);//calculate all rest days in this year, not including today
int calDaysIn2Years(int,int);//calculate all days between two assigned years
int calDaysIn2Dates(Date,Date);//calculate all days between two assigned dates
void printDate(Date& date);//print out date
void printWeekDay(Date& date);//print out the day of the week
void printNextMonday(Date& date);//print out the date of the next Monday
#define DAYS_OF_NORM_YEAR 365;//days of the normal year
#define DAYS_OF_LEAP_YEAR 366;//days of the leap year
int main()
{
Date d1{2016,2,29};
Date d2{2016,4,7};
addOneDay(d1);
printDate(d1);
addOneYear(d1);
printDate(d1);
addOneMonth(d1);
printDate(d1);
printWeekDay(d1);
printNextMonday(d1);
printWeekDay(d2);
printNextMonday(d2);
return 0;
}
void addOneDay(Date& date)
{
++date.day;
if (date.day>months[date.month-1])
{
if(date.month==2)
{
if (date.day==29)
{
if (!isLeapYear(date.year))
{
++date.month;
date.day=1;
}
}
else if (date.day>29)
{
if (isLeapYear(date.year))
date.day-=29;
else
date.day-=28;
++date.month;
}
}
else
{
++date.month;
if (date.month>12)
date.month=1;
date.day=1;
}
}
}
void addOneMonth(Date& date)
{
switch (date.month) {
case 1:{ ++date.month;
if (date.day>28)
{
if(isLeapYear(date.year))
date.day=29;
else
date.day=28;
}
break;
}
case 3:;
case 5:;
case 7:;
case 8:;
case 10:{ ++date.month;
if (date.day==31)
--date.day;
break;
}
case 12:{ ++date.year;
date.month=1;
break;
}
default: ++date.month;break;
}
}
void addOneYear(Date& date)
{
if (isLeapYear(date.year)&&date.day==29)
--date.day;
++date.year;
}
bool isLaterDate(Date d1,Date d2)
{
if (d1.year>d2.year)
return true;
else if(d1.year==d2.year)
{
if(d1.month>d2.month)
return true;
else if(d1.month==d2.month)
{
if(d1.day>d2.day)
return true;
return false;
}
return false;
}
return false;
}
bool isLeapYear(int year)
{
return (year%400==0)||(year%4==0&&year%100!=0);
}
int calPassedDays(Date date)
{
int days=0;
for (int i = 0; i < date.month-1; ++i)
days+=months[i];
days+=date.day;
if ((date.month>2)&&isLeapYear(date.year))
++days;
return days;
}
int calRestDays(Date date)
{
int days=months[date.month-1]-date.day;
for(int i=date.month;i<12;++i)
days+=months[i];
if ((date.month<=2)&&isLeapYear(date.year))
++days;
return days;
}
int calDaysIn2Years(int year1,int year2)
{
int days=0;
for(int i=year1;i<year2;i++)
{
if (isLeapYear(i))
{
days+=DAYS_OF_LEAP_YEAR;
}
else
days+=DAYS_OF_NORM_YEAR;
}
return days;
}
int calDaysIn2Dates(Date d1,Date d2)
{
bool flag=false;
if(isLaterDate(d1,d2))
{
Date tmp=d1;
d1=d2;
d2=tmp;
flag=true;
}
int days=calRestDays(d1);
if(d1.year!=d2.year)
{
if((d2.year-d1.year)>=2)
days+=calDaysIn2Years(d1.year+1,d2.year);
days+=calPassedDays(d2);
}
else
{
days=days-calRestDays(d2);
}
return flag==true?-days:days;
}
void printDate(Date& date)
{
cout<<"date: "<<date.year<<"/"<<date.month<<"/"<<date.day<<endl;
}
void printWeekDay(Date& date)
{
int days=calDaysIn2Dates(initDate,date);
days%=7;
if (days==0)
cout<<weekdays[0]<<endl;
else if (days<0)
{
cout<<weekdays[7+days]<<endl;
}
else
{
cout<<weekdays[days]<<endl;
}
}
void printNextMonday(Date& date)
{
int days=calDaysIn2Dates(initDate,date);
Date tmp=date;
days%=7;
if (days<0)
{
days+=7;
days=7-days;
}
else if (days==0)
{
days+=7;
}
else
{
days=7-days;
}
while (days--)
addOneDay(tmp);
printDate(tmp);
}