#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
HANDLE hHandle=GetStdHandle(STD_OUTPUT_HANDLE);
int DaysPerMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void SetYear();
bool IsLeap(int year);//判断是否闰年
int GetDays(int year,int month,int day);//从1年1月1号开始算的天数
void Print(int year,int month);
bool IsLeap(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return true;
else
return false;
}
int GetDays(int year,int month,int day)
{
int yearDays, monthDays=0,sum;
int total=0;
for(int i=1;i<year;i++)
if(IsLeap(i))
total++;
yearDays=total+(year-1)*365;
if(IsLeap(year)) DaysPerMonth[2]=29;
for(int j=1;j<month;j++)
monthDays+=DaysPerMonth[j];
sum=yearDays+monthDays+day;
return sum;
}
void SetYear()
{
int year,month,day;
printf("欢迎使用万年历,请输入年份(1-9999):\n");
scanf("%d",&year);
while (year<=1||year>9999)
{
printf("请输入年份正确的年份(1-9999)\n");
scanf("%d",&year);
}
month=1;day=1;
for(int i=1;i<=12;i++)
Print(year,i);
}
void Print(int year,int month)
{
int week,day=1;
SetConsoleTextAttribute(hHandle,10);
printf(" 公元%d年%d月\n\n",year,month);
SetConsoleTextAttribute(hHandle,15);
printf(" 日 一 二 三 四 五 六\n");
week=GetDays(year, month,day)%7;
switch(week)
{
case 0:SetConsoleTextAttribute(hHandle,12);printf("%6d",1); break;
case 1:SetConsoleTextAttribute(hHandle,11);printf("%12d",1); break;
case 2:SetConsoleTextAttribute(hHandle,11);printf("%18d",1); break;
case 3:SetConsoleTextAttribute(hHandle,11);printf("%24d",1); break;
case 4:SetConsoleTextAttribute(hHandle,11);printf("%30d",1); break;
case 5:SetConsoleTextAttribute(hHandle,11);printf("%36d",1); break;
case 6:SetConsoleTextAttribute(hHandle,11);printf("%42d",1); break;
}
for(int i=2;i<=DaysPerMonth[month];i++)
{
week=(++week)%7;
if(!week)
{
SetConsoleTextAttribute(hHandle,12);
printf("\n%6d",i);
}
else
{
SetConsoleTextAttribute(hHandle,11);
printf("%6d",i);
}
}
printf("\n\n");
}
int main()
{
SetConsoleTextAttribute(hHandle,15);
SetYear();
SetConsoleTextAttribute(hHandle,15);
system("pause");
return 0;
}