komany 发表于 2010-12-3 09:32:40

万年历

本帖最后由 komany 于 2010-12-8 12:16 编辑

#include "stdio.h"
void main()
{
int leap_year(int y);
int count_leap(long year);
int monthdays(int month,long year);
void monthprint(long year,int month);
int month=0;
long int year=0;
char choose;
clrscr();
do
{printf ("\n\nPlease input one integer number as 'year'(0~3000):\n");
scanf ("%ld",&year);
if (year<0||year>3000) printf ("ERROR,please input again!");
}
while (year<0||year>3000);
printf ("\n\n");

do
{printf ("please input the month(1~12)\n\n");
scanf ("%d",&month);
if (month<=0||month>12) printf ("please input again!");
}
while (month<=0||month>12);
printf ("\n\n\n");

printf("\t\t\t\t%ld\t%d\n\n",year,month);
printf

("\n********************************************************************************\n");
printf("\t\t Sun\t Mon\t Tue\t Wen\t Thu\t Fri\t Sat\n");
monthprint(year,month);
printf("\n\n");
}

int leap_year(int y)
{ int i;
if (y%4==0&&y%100!=0||y%400==0) i=1;
else i=0;
return i;
}
//多谢N大的教导,下面/**/的里面是N大优化的代码
/*int leap_year(int y)
{
    return (!(y%4)&&y%100 )|| !(y%400) ;
}
*/

int count_leap(long year)
{
int i=0,j,min,max;
if(year>2006) {min=2006;max=year;}
else {min=year+1;max=2006;}
for(j=min;j<max;j++)
if(leap_year(j)) i++;
return i;
}

int monthdays(int month,long year)
{
int sum=0,i,j;
if(year>=2006)
{ int t={31,0,31,30,31,30,31,31,30,31,30,31};
j=month-1;
if(leap_year(year)) t=29;
else t=28;
for(i=0;i<j;i++)
sum=sum+t;
}
else
{ int t1={31,30,31,30,31,31,30,31,30,31,0,31};
j=12-month;
if(leap_year(year)) t1=29;
else t1=28;
for(i=0;i<=j;i++)
sum=sum+t1;
}

return sum;

}


void monthprint(long year,int month)
{
int t={31,0,31,30,31,30,31,31,30,31,30,31};
int i,y,weekday=0;
long days=0;
if(leap_year(year)) t=29;
else t=28;
y=t;
if(year>2006)
{
days=(year-2006)*365+count_leap(year)+monthdays(month,year);
weekday=days%7;
}
else if(year<2006)
{days=(2005-year)*365+count_leap(year)+monthdays(month,year);
weekday=7-days%7;
}
else
{days=monthdays(month,year);weekday=days%7;}

for (i=1;i<=weekday+2;i++)
printf ("\t");
for (i=1;i<=y;i++)
{if ((i+weekday-1)%7==0) printf ("\n\n\t\t%3d\t",i);
else printf ("%3d\t",i);};
printf

("\n********************************************************************************\n\n");
}



Nisy 发表于 2010-12-4 00:50:01

不错 ~

int leap_year(int y)
{
    return (!(y%4)&&y%100 )|| !(y%400) ;
}
页: [1]
查看完整版本: 万年历