标准输出某年的日历,要求星期和日期对应
//转自:http://bbs.csdn.net/topics/390284667
#include <stdio.h>
#include <math.h>
void printmonth(int m);
void printhead(int m);
int daysofmonth(int m);
int firstday(int y);
int year,weekday;
int main()
{
int i;
printf("请输入年份:\n");
scanf("%d", &year);
weekday=firstday(year); //计算某年元旦是星期几
printf("\n\n");
printf(" %d年\n",year);
for(i=1;i<=12;i++)
{
printmonth(i);
printf("\n");
}
printf("\n\n");
return 0;
}
void printmonth(int m)
{
int i,days;
printhead(m);
days=daysofmonth(m);
for(i=1;i<=days;i++)
{
printf("%5d",i);
weekday=(weekday+1)%7;
if (weekday==0) printf("\n ");
}
}
void printhead(int m)
{
int i;
printf("\n%d月 日 一 二 三 四 五 六\n",m);
printf(" ");
for(i=0;i<weekday;i++)
printf(" ");
}
int daysofmonth(int m)
{
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:if (((year%4==0 && year%100!=0)||year%400==0))
return 29;
else
return 28;
default: return 0;
}
}
//计算某年元旦是星期几
int firstday(int y)
{
double s;
s = floor((y-1) + (y-1)/4 - (y-1)/100 + (y-1)/400 + 1);
return (int)s%7;
}