第五关:显示月历
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int count(int iy,int im,int id)
{
int c = iy / 100;
int p = iy % 100;
if (im == 1)
{
im = 13;
}
if (im == 2)
{
im = 14;
}
int iw = (c / 4 - 2 * c + p + p / 4 + 13 * (im + 1) / 5 + id - 1) % 7;
return iw;
}
int main()
{
int y, m, d, w = 0;
int maxday = 0;
int i = 0;
int n= 0;
int startday = 0;
int a[6][7] = { 0 };
printf("输入年份、月份:");
scanf("%d %d", &y, &m);
printf("-------------------------------------------------------\n");
printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六 \n");
switch (m)
{
case 4:
maxday = 30;
break;
case 6:
maxday = 30;
break;
case 9:
maxday = 30;
break;
case 11:
maxday = 30;
break;
case 2:
if (y % 4 == 0 && y % 100 != 0)
{
maxday = 29;
}
else
{
maxday = 28;
}
break;
default:
maxday = 31;
break;
}
startday = count(y, m, 1);
if (startday != 0)
{
for (n = 0; n <= startday; n++)
printf(" ");
}
for (d = 1; d <= maxday; d++)
{
w = count(y, m, d);
printf("%d\t",a[i][w] = d);
if (w == 6)
{
i++;
printf("\n");
}
}
printf("\n-------------------------------------------------------");
return 0;
}