万年历程序
#include<stdio.h>#include<conio.h>
#define PF printf
#define EP PF("%s%s%.10s%s%s*",s,k,k,k,k)
#define LEAP(y) !(y%(y%100?4:400))
int xqj(int year,int month,int day)//星期几
{
int i,y1,days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
days[2]=28+LEAP(year);
for(i=1;i<month;i++)day+=days[i];
y1=year-1;
day+=y1+y1/4-y1/100+y1/400;
return day%7;
}
void fill(int y,int m,char array[3*7*6])
{
char *pa;
int i,x1,days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
days[2]=28+LEAP(y);
x1 = xqj(y,m,1);
x1 = x1?x1-1:x1+6;
pa = array+3*x1;
for(i=1;i<=3*7*6;i++)array[i-1]=' ';
for(i=1;i<=days[m];i++,pa+=3)sprintf(pa,"%3d",i);
*pa=' ';
}
void half(int year,int m)
{
int i,j;
char ary[6][7*3],bry[6][7*3],cry[6][7*3];
char *k=" ",*s="\n *";
char *xs="********************************";
PF("%s%s %04d %s*",s,xs,year,xs);
for(i=1;i<=2;i++,m+=3)
{
EP;
if(m<10)PF("%s%.10s(%d)%.20s(%d)%.20s(%d)%.11s*",s,k,m,k,m+1,k,m+2,k);
else PF("%s%.10s(%d)%.19s(%d)%.19s(%d)%.10s*",s,k,m,k,m+1,k,m+2,k);
EP;
PF("%s MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU *",s);
fill(year,m ,ary[0]);
fill(year,m+1,bry[0]);
fill(year,m+2,cry[0]);
for(j=0;j<6;j++)
{
PF("%s %.21s %.21s %.21s *",s,ary[j],bry[j],cry[j]);
}
EP;
}
PF("%s%s******%s*",s,xs,xs);
}
main( )
{
int year;
do
{
PF("year: ");
scanf("%d",&year);
}
while(year<=0||year>9999);
half(year,1);//上半年
getch();
PF("\n");
half(year,7);//下半年
getch();
PF("\n");
}