148 lines
3.4 KiB
C++
148 lines
3.4 KiB
C++
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
int runYear(int n); //日历不会写痛苦
|
|
int RunYear(int year);
|
|
void printMonth(int year, int month);
|
|
int dayOfWeek(int year, int month, int day);
|
|
void printMonth(int year, int month)
|
|
{
|
|
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
int startDay = dayOfWeek(year, month, 1);
|
|
int days = daysInMonth[month - 1];
|
|
if (month == 2 && RunYear(year))
|
|
{
|
|
days = 29;
|
|
}
|
|
else if (year == 1582 && month == 10)
|
|
{
|
|
days = 21;
|
|
}
|
|
|
|
printf("\n%d年%d月\n", year, month);
|
|
printf("日 一 二 三 四 五 六\n");
|
|
|
|
// 打印空格直到第一天
|
|
for (int i = 0; i < startDay; i++)
|
|
{
|
|
printf(" ");
|
|
}
|
|
|
|
// 打印日期
|
|
for (int day = 1; day <= days; day++)
|
|
{
|
|
printf("%2d ", day);
|
|
if ((day + startDay) % 7 == 0)
|
|
{
|
|
printf("\n");
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int dayOfWeek(int year, int month, int day)
|
|
{
|
|
if (month < 3)
|
|
{
|
|
month += 12;
|
|
year -= 1;
|
|
}
|
|
int k = year % 100;
|
|
int j = year / 100;
|
|
int h = day + 13 * (month + 1) / 5 + k + k / 4 + j / 4 + 5 * j; //zeller公式
|
|
h = h % 7;
|
|
return 0;
|
|
}
|
|
int RunYear(int year)
|
|
{
|
|
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
|
|
}
|
|
int runYear(int n)
|
|
{
|
|
printf("请输入年份\n");
|
|
scanf("%d",&n);
|
|
if(n%4==0&&n%100!=0||n%400==0)
|
|
{
|
|
printf("是闰年\n");
|
|
}
|
|
else
|
|
{
|
|
printf("不是闰年\n");
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int choice,n,year,month,day;
|
|
printf("1.查询对应的年份的日历\n");
|
|
printf("2.查询对应日期的星期\n");
|
|
printf("3.查询对应年份是否为闰年\n");
|
|
printf("4.退出\n");
|
|
printf("请选择序号=");
|
|
scanf("%d",&choice);
|
|
do
|
|
{
|
|
switch(choice)
|
|
{
|
|
case 1:
|
|
{
|
|
int year;
|
|
printf("请输入年份:");
|
|
scanf("%d", &year);
|
|
|
|
// 打印全年的日历
|
|
for (int month = 1; month <= 12; month++)
|
|
{
|
|
printMonth(year, month);
|
|
}
|
|
return 0;
|
|
}
|
|
case 2:
|
|
{
|
|
printf("请输日期(年 月 日)\n");
|
|
scanf("%d %d %d",&year,&month,&day);
|
|
int runYear(year);
|
|
if(0)
|
|
{
|
|
if (month < 3) //平年
|
|
{
|
|
month += 12;
|
|
year -= 1;
|
|
}
|
|
int c = year % 100;
|
|
int y = year / 100;
|
|
int h = day + 13 * (month + 1) / 5 + c + c / 4 + y / 4 + 5 * y; //zeller公式
|
|
h = h % 7;
|
|
printf("星期%d",h);
|
|
}
|
|
else
|
|
{
|
|
if (month < 3) //润年
|
|
{
|
|
month += 12;
|
|
year -= 1;
|
|
}
|
|
int c = year % 100;
|
|
int y = year / 100;
|
|
int h = day-1 + 13 * (month + 1) / 5 + c + c / 4 + y / 4 + 5 * y; //zeller公式
|
|
h = h % 7;
|
|
printf("星期%d",h);
|
|
}
|
|
return 0;
|
|
}
|
|
case 3:
|
|
{
|
|
runYear(n);
|
|
}
|
|
case 4:
|
|
return 0;
|
|
default:
|
|
{
|
|
printf("输入错误!请重新选择\n");
|
|
printf("请选择序号=");
|
|
scanf("%d",&choice);
|
|
}
|
|
}
|
|
} while(choice);
|
|
return 0;
|
|
}
|