144 lines
3.2 KiB
C++
144 lines
3.2 KiB
C++
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
|
||
// 判断是否为闰年
|
||
int runnian(int year) {
|
||
return ((year % 4 == 0 && year % 100!= 0) || (year % 400 == 0));
|
||
}
|
||
|
||
// 计算某日期是周几(基姆拉尔森计算公式)
|
||
int howweek(int year, int month, int day) {
|
||
if (month < 3) {
|
||
month += 12;
|
||
year--;
|
||
}
|
||
int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
|
||
return week+1;
|
||
}
|
||
|
||
// 输出单个月份日历(单列输出)
|
||
void dl(int year, int month) {
|
||
int dym;
|
||
if (month == 2) {
|
||
dym = runnian(year)? 29 : 28;
|
||
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
|
||
dym = 30;
|
||
} else {
|
||
dym = 31;
|
||
}
|
||
|
||
int weekDay = howweek(year, month, 1);
|
||
if(weekDay==7)
|
||
weekDay=0;
|
||
printf(" %d年%d月\n", year, month);
|
||
printf(" 日 一 二 三 四 五 六\n");
|
||
|
||
for (int i = 0; i < weekDay; i++) {
|
||
printf(" ");
|
||
}
|
||
for (int i = 1; i <= dym; i++) {
|
||
printf("%2d ", i);
|
||
if ((i + weekDay) % 7 == 0) {
|
||
printf("\n");
|
||
}
|
||
}
|
||
printf("\n");
|
||
}
|
||
|
||
// 输出整年日(单列输出)
|
||
void dl(int year) {
|
||
if(year<=10000&&year>0){
|
||
for (int month = 1; month <= 12; month++) {
|
||
dl(year, month);
|
||
}
|
||
}
|
||
else
|
||
printf("错误");
|
||
}
|
||
|
||
// 显示菜单
|
||
void cd() {
|
||
printf("1. 查询日期是周几及是否闰年\n");
|
||
printf("2. 输出单年日历(单列)\n");
|
||
printf("3. 输出单年日历(双列)\n");
|
||
printf("4. 退出\n");
|
||
printf("请选择操作:");
|
||
}
|
||
int getValidIntInput() {
|
||
int input;
|
||
while (1) {
|
||
if (scanf("%d", &input) == 1) {
|
||
break;
|
||
} else {
|
||
printf("输入错误,请重新输入整数:");
|
||
while (getchar()!= '\n');
|
||
}
|
||
}
|
||
return input;
|
||
}
|
||
|
||
int main() {
|
||
int choice;
|
||
int year, month, day;
|
||
|
||
do {
|
||
cd();
|
||
choice = getValidIntInput();
|
||
|
||
switch (choice) {
|
||
case 1:
|
||
printf("请输入年份:");
|
||
year = getValidIntInput();
|
||
printf("请输入月份:");
|
||
month = getValidIntInput();
|
||
printf("请输入日期:");
|
||
day = getValidIntInput();
|
||
|
||
printf("%d年%d月%d日是星期", year, month, day);
|
||
switch (howweek(year, month, day)) {
|
||
case 0:
|
||
printf("日");
|
||
break;
|
||
case 1:
|
||
printf("一");
|
||
break;
|
||
case 2:
|
||
printf("二");
|
||
break;
|
||
case 3:
|
||
printf("三");
|
||
break;
|
||
case 4:
|
||
printf("四");
|
||
break;
|
||
case 5:
|
||
printf("五");
|
||
break;
|
||
case 6:
|
||
printf("六");
|
||
break;
|
||
}
|
||
printf(",");
|
||
if (runnian(year)) {
|
||
printf("该年是闰年\n");
|
||
} else {
|
||
printf("该年不是闰年\n");
|
||
}
|
||
break;
|
||
case 2:
|
||
printf("请输入年份:");
|
||
year =getValidIntInput();
|
||
dl(year);
|
||
break;
|
||
case 4:
|
||
printf("退出程序\n");
|
||
break;
|
||
default:
|
||
printf("无效的选择,请重新选择\n");
|
||
break;
|
||
}
|
||
} while (choice!= 4);
|
||
|
||
return 0;
|
||
}
|