c语言万年历编程
A. 用c语言编写万年历,要求输出任意一年的某月,能显示出这个月的日历
1、首先要判断一个年份是闰年还是平年,用一个子程序来做。
B. C语言编写万年历
#include"stdio.h"
void print(int);//函数声明
int dobb(int);
int date(int,int,int);
void main()
{
int year=0;
printf("输入打印的年份(XXXX)\n");
scanf("%d",&year);
print(year);
}
int dobb(int year)//闰年判断
{
if((year%4==0 && year%100!=0)|| (year%400==0))
return(1);
else return(0);
}
int date(int year,int month,int day)//判断该年某月某日是星期几的计算
{
int leap,cn=0,sum;//cn计算的是从该年1月1日起,到达这一天的天数总和
leap=dobb(year);
switch(month-1)
{
case 11:cn+=30;
case 10:cn+=31;
case 9:cn+=30;
case 8:cn+=31;
case 7:cn+=31;
case 6:cn+=30;
case 5:cn+=31;
case 4:cn+=30;
case 3:cn+=31;
case 2:if(leap) cn+=29;
else cn+=28;
case 1:cn+=31;
default:cn+=day;
}
sum=year-1+(year-1)/4-(year-1)/100+(year-1)/400+cn;//这是核心的一条公式,返回值是星期几(没有为什么)
return(sum%7);
}
void print(int year)//打印的核心算法
{
int i,j,n,leap,days,k,count;
leap=dobb(year);//判断是否闰年
days=date(year,1,1);//计算该年第一天是星期几
printf("%d年\n",year);
for(i=1;i<=12;i++)
{
printf("%3d月份\n",i);
printf("\n");
printf("\t");
printf("%-8s%-8s%-8s%-8s%-8s%-8s%-8s\n","Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
if(days==7)
{
days=0;n=days;
}
else n=days;
printf("\t");
for(j=1;j<=8*days;j++)
printf(" ");
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:count=31;break;
case 4:
case 6:
case 9:
case 11:count=30;break;
case 2:if(leap) count=29;
else count=28; break;
}
for(k=1;k<=count;k++)//以下代码是控制界面的对齐
{
printf("%-8d",k);
n++;
if(n==7)
{printf("\n");printf("\t");n=0;}
}
printf("\n\n");
days=n;
}
}