返回
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
// 万年历
#include <iostream>
#include <string.h>
using namespace std;

int n;

const char *MONTH_NAMES[12] = {" January", " February", " March",
" April", " May", " June",
" July", " August", " September",
" October", " November", " December"};

bool isLeap(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int Wweek(int year, int month, int day)
{
if (month < 3)
{
month += 12;
year--;
}

int h = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;

if (h < 0)
h += 7;

h++;

return h;
}

void Pcalendar(int year, int month)
{
const int DAYS_IN_MONTH[12] = {31, isLeap(year) ? 29 : 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};

printf("%s\n", MONTH_NAMES[month]);
printf(" -%02d- \n", month + 1);
printf(" SUN MON TUE WED THU FRI SAI\n");

int firstDayOfWeek = Wweek(year, month + 1, 1) % 7;

for (int i = 0; i < firstDayOfWeek; ++i)
printf(" ");

for (int day = 1; day <= DAYS_IN_MONTH[month]; ++day)
{
printf("%4d", day);

if ((day + firstDayOfWeek) % 7 == 0)
printf("\n");
}

// if ((DAYS_IN_MONTH[month] + firstDayOfWeek) % 7 != 0)
// {
// for (int i = (DAYS_IN_MONTH[month] + firstDayOfWeek) % 7; i < 7; ++i)
// printf(" ");
//
// printf("\n");
// }

printf("\n============================\n");
}

void Pyear()
{
int year = 0;

printf("Please input the year: ");

scanf("%d", &year);
printf("\n");

printf("Calendar %04d\n", year);

for (int month = 0; month < 12; ++month)
{
Pcalendar(year, month);
}

printf("\n");
}

void Pmonth()
{
char str[15];
int year = 0, month = 0;

printf("Please input the month<YYYY-MM>: ");

scanf("%s", str);
printf("\n");

int cnt = 0;
for (int i = 0; i < strlen(str); ++i)
{
if (str[i] == '-')
{
cnt++;
}
else
{
if (cnt == 0)
year = year * 10 + str[i] - 48;
else if (cnt == 1)
month = month * 10 + str[i] - 48;
}
}

printf("Calendar %04d\n", year);

Pcalendar(year, month - 1);

printf("\n");
}

int main()
{
while (1)
{
printf("1 -This year is leap or not\n");
printf("2 -This day is which day of the week\n");
printf("3 -The Calendar of this year\n");
printf("4 -The Calendar of this month of year\n");
printf("0 -Exit\n\n");
printf("Please select the options: ");

scanf("%d", &n);
printf("\n");

switch (n)
{
case 1:
{
int year = 0;

printf("Please input the year: ");

scanf("%d", &year);
printf("\n");

if (isLeap(year))
printf("The year %d is leap year.\n\n", year);
else
printf("The year %d is not leap year.\n\n", year);
}
break;
case 2:
{
char str[15];
int year = 0, month = 0, day = 0;

printf("Please input the date<YYYY-MM-DD>: ");

scanf("%s", str);
printf("\n");

int cnt = 0;
for (int i = 0; i < strlen(str); ++i)
{
if (str[i] == '-')
{
cnt++;
}
else
{
if (cnt == 0)
year = year * 10 + str[i] - 48;
else if (cnt == 1)
month = month * 10 + str[i] - 48;
else if (cnt == 2)
day = day * 10 + str[i] - 48;
}
}

switch (Wweek(year, month, day))
{
case 1:
printf("This day %d-%02d-%02d is Monday.\n\n", year, month, day);
break;
case 2:
printf("This day %d-%02d-%02d is Tuesday.\n\n", year, month, day);
break;
case 3:
printf("This day %d-%02d-%02d is Wednesday.\n\n", year, month, day);
break;
case 4:
printf("This day %d-%02d-%02d is Thursday.\n\n", year, month, day);
break;
case 5:
printf("This day %d-%02d-%02d is Friday.\n\n", year, month, day);
break;
case 6:
printf("This day %d-%02d-%02d is Saturday.\n\n", year, month, day);
break;
case 7:
printf("This day %d-%02d-%02d is Sunday.\n\n", year, month, day);
break;
}
}

break;
case 3:
Pyear();
break;
case 4:
Pmonth();
break;
case 0:
exit(0);
break;
default:
printf("没有该选项,请重新输入!\n\n");
}
}

return 0;
}