当前位置:文档之家› C语言程序设计 万年历程序

C语言程序设计 万年历程序

#include"stdio.h"
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
int judge(int year,int month)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month ==12)
return(1);
else if(month==2)
{
if(year%4!=0||year%100==0&&year%400!=0)
return(2);
else return(3);
}
else return(4);
}
void show2()
{
int year,i,j,a,n,m,k;
char ** p;
char * week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char*month[]={"January","February","March","April","May","June","July","Augu st","September","October","November","December"};
printf("please inter the year:");
scanf("%d",&year);
printf("\n");
printf("the calendar of the year%d.",year);
printf("\n");
a=(year+(year-1)/4-(year-1)/100+(year-1)/400)%7;
for(i=0;i<12;i++)
{
n=judge(year,i+1);
p=month+i;
printf("%s\n",*p);
printf("\n");
for(j=0;j<7;j++)
{p=week+j;
printf("%6s",*p);}
printf("\n");
for(k=0;k<a;k++)
printf(" ");
for(m=1;m<32;m++)
{
printf("%6d",m);
if((a+m)%7==0)
printf("\n");
if(n==1&&m==31) break;
else if(n==2&&m==28) break;
else if(n==3&&m==29) break;
else if(n==4&&m==30) break;
}
a=(a+m)%7;/*计算出下个月的第一天是星期几*/
printf("\n");
printf("**************************************************");
printf("\n");
}
}
void show()
{
printf("******************************\n");
printf("1.某年某月某日是星期几\n2.是否闰年\n3.某年某月的最大天数\n4.打印该月日历\n5.退出\n");
printf("******************************\n");
}
int show1(int y,int m,int d)
{int i,R,sum=0;
for(i=0;i<m-1;i++)
sum=sum+mon_day[i];
sum=sum+d;
if((y%4 == 0 &&y%100 != 0)||(y%400 == 0)&&m>2)
sum=sum+1;
R=(5+(y+1)+(y-1)/4-(y-1)/100+(y-1)/400+sum)%7;
if(R==0)
R=7;
return R;
}
int leap(int y)
{
if((y%4 == 0 &&y%100 != 0)||(y%400 == 0))
return 1;
else
return 0;
}
int max_day(int y,int m)
{int X;
if(leap(y)==1&&m==2)
X=29 ;//printf("该年该月最大天数:29");
else
X=mon_day[m-1];//printf("%d",mon_day[m-1]);
return X;
}
void main()
{
int year,month,day,i;
F:
show();
printf("请选择:");
scanf("%d",&i);
switch(i)
{
case 1:
printf("请输入年、月,日:");
scanf("%d%d%d",&year,&month,&day);
printf("%d-%d-%d是星
期%d\n",year,month,day,show1(year,month,day));break;
case 2:
printf("请输入年份:");
scanf("%d",&year);
if(leap(year)==1)
printf("该年是闰年.\n");
else
printf("该年不是闰年\n");break;
case 3:
printf("请输入年、月:");
scanf("%d%d",&year,&month);
printf("该月最大天数:%d",max_day(year,month)); printf("\n");break;
case 4:show2();break;
case 5:return 0;break;
}
goto F;
}。

相关主题