应用到蔡勒公式(判断某天是周几)链接:https://blog.csdn.net/qq_42804678/article/details/82793438
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
int israin(int year)//判断闰年
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else
return 0;
}
int isweek(int y,int m,int d)//判断为周几,结果为0时,代表周日
{
if(m==1||m==2) m+=12,y=y-1;
int w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
return (w%7+7)%7;
}
int main()
{
int y[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int year;
scanf("%d",&year);
printf("-------%d年日历-------\n",year);
int m=1,d=1;
int w1=isweek(year,1,1);//当前年份的1月1日为周几
int w;//标记这个月第一天为周几
int i;
for(i=1;i<=12;i++)
{
printf("%d月\n",i);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
if(i==1)
w=w1;
int j;
for(j=0;j<w;j++)
{
printf("%-4c",' ');
}
int d1=0;
for(j=w;j<=6;j++)
{
d1++;
printf("%-4d",d1);
}
printf("\n");
int w1;//记录月末为周几
int sum,answer=0;
for(sum=1;sum<=5;sum++)//控制一个月的周数
{
for(j=0;j<=6;j++)//循环为每一周
{
d1++;
printf("%-4d",d1);
if(d1==y[israin(year)][i])//到月末时,就停止
{
if(j==6) w=0;//月末为周六,则下个月第一天为周日,即0!!!
else w=j+1;//w为下一个月第一天的周几
answer=1;
break;
}
}
printf("\n");
if(answer==1)
break;
}
printf("\n");
}
}