用C写的日历程序源码
#include<stdio.h>main()
{
int i,j,day,year,temp,temp_i ;
long int year_days ;
long int sumdays(int year);
int IsLeapYear(int year);
int month_day[]=
{
0,31,28,31,30,31,30,31,31,30,31,30,31,29
};
int everyday[6][14];
printf("please enter the year :\n");
scanf("%d",&year);
printf("\n\n");
year_days=sumdays(year);
for(temp=1;temp<=6;temp++)
{
for(i=0;i<6;i++)
for(j=0;j<14;j++)
everyday[i][j]=0 ;
switch(temp)
{
case 1 :
printf("\tJanuary %d\t\t\tFebruary %d\n",year,year);
break ;
case 2 :
printf("\tMarch %d\t\t\tApril %d\n",year,year);
break ;
case 3 :
printf("\tMay %d\t\t\tJune %d\n",year,year);
break ;
case 4 :
printf("\tJuly %d\t\t\tAugust %d\n",year,year);
break ;
case 5 :
printf("\tSeptember %d\t\t\tOctober %d\n",year,year);
break ;
case 6 :
printf("\tNovember %d\t\t\tDecember %d\n",year,year);
break ;
}
printf("Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun");
printf("\n");
i=j=0 ;
for(temp_i=0;temp_i<(year_days%7);temp_i++)
everyday[0][j++]=0 ;
day=1 ;
while(day<=month_day[2*temp-1])
{
if(j%7==0)
j=0,i++;
everyday[i][j++]=day ;
year_days++;
day++;
}
i=0,j=7 ;
for(temp_i=0;temp_i<(year_days%7);temp_i++)
everyday[0][j++]=0 ;
day=1 ;
if(IsLeapYear(year)&&2*temp==2)
while(day<=month_day[13])
{
if(j%14==0)
j=7,i++;
everyday[i][j++]=day ;
year_days++;
day++;
}
else
while(day<=month_day[2*temp])
{
if(j%14==0)
j=7,i++;
everyday[i][j++]=day ;
year_days++;
day++;
}
for(i=0;i<6;i++)
{
for(j=0;j<14;j++)
{
if(everyday[i][j]!=0)
printf("% -4d",everyday[i][j]);
else
printf(" ");
if(j==6)
printf(" ");
}
printf("\n");
}
if(getch()=='q')
exit(0);
}
getch();
}
int IsLeapYear(int year)
{
if((year%4==0)&&(year%100!=0)||(year%400==0))
return 1 ;
else
return 0 ;
}
long int sumdays(int year)
{
int year_start=1,leapsum=0 ;
while(year_start<year)
{
if(IsLeapYear(year_start))
leapsum++;
year_start++;
}
return 366*leapsum+365*(year-leapsum);
}