#include <stdio.h>
#include <string.h>
#include <conio.h>
int main(int argc, char * argv[]); /* 主入口函数 */
void printmonth(int m); /* 函数说明 */
void printhead(int m); /* 函数说明 */
int daysofmonth(int m); /* 函数说明 */
int isleap(int y); /* 函数说明 */
int firstday(int y); /* 函数说明 */
void month(int m,char e[]); /* 函数说明 */
int year,weekday; /* 全局变量定义 */
int main(int argc, char * argv[])
{
int i;
printf("Input which Year:");
scanf("%d",&year);
weekday=firstday(year); /* weekday 为 year 年份元月一日的星期号 */
printf(" %d year\n",year); /* 17个空格 */
for(i=1;i<=12;i++) /* 输出 year 年份 12 个月的日历信息 */
{
printmonth(i);
printf("\nPress any key to continue...\n");
getch();
}
printf("\n\n");
return 0;
}
void printmonth(int m) /* 输出第 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 "); /* 3 个空格 */
}
}
}
void printhead(int m) /* 输入第 m 月份的头部信息 */
{
int i;
char e[5];
month(m,e);
printf("\n %s Sun Mon Tue Wed Thu Fri Sat\n",e);
printf(" "); /* 3 个空格 */
for(i=0;i<weekday;i++)
printf(" "); /* 5 个空格 */
}
int daysofmonth(int m) /* 返回 year 年 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(isleap(year))
return 29;
else
return 28;
}
return 0;
}
int isleap(int y) /* 判断 y 年是否为闰年 */
{
return((y%4==0 && y%100!=0) || y%400==0);
}
int firstday(int y) /* 从公元第 1 天为星期天算出 y 年第 1 天是星期几 */
{
long n=y*365+1;
int i;
for(i=1;i<y;i++) /* 补上所有闰年的天数 */
n+=isleap(i);
return n%7;
}
void month(int m,char e[5]) /* 返回 m 月的名称 */
{
switch(m)
{
case 1:
strcpy(e, "JAN");
break;
case 2:
strcpy(e, "FEB");
break;
case 3:
strcpy(e, "MAR");
break;
case 4:
strcpy(e, "APR");
break;
case 5:
strcpy(e, "MAY");
break;
case 6:
strcpy(e, "JUN");
break;
case 7:
strcpy(e,"JUL");
break;
case 8:
strcpy(e,"AUG");
break;
case 9:
strcpy(e,"SEP");
break;
case 10:
strcpy(e,"OCT");
break;
case 11:
strcpy(e,"NOV");
break;
case 12:
strcpy(e,"DEC");
break;
}
}
参考资料:我大一时候写的,需要源代码文件给我邮件——[email protected]