昨天从网上学习了一个关于公历星期的算法写了一个简易的万年历程序和大家分享(在vc6.0中调试通过):
#include<iostream.h>
#include<string.h>
int week(int y,int m,int d)
{
static int r[13]={0,0,3,3,6,1,4,6,2,5,0,3,5};
int c,w;
y%=400;
if((y==0||y%4==0&&y%100!=0)&&m<3)c=5;
else c=6;
w=(y+y/4-y/100+r[m]+d+c)%7;
return w;
}
void main()
{
int k,y,n;
cout<<"请输入年月:";
cin>>y>>k;
switch(k)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
n=32;break;
case 2:
if(y%4==0&&y%100!=0||y%400==0)
n=30;
else
n=29;
break;
default:
n=31;
break;
}
cout<<endl<<endl<<endl;
cout<<" ";
cout<<" 公元 "<<y<<" 年 "<<k<<" 月"<<endl<<endl;
cout<<" ";
cout<<"天 一 二 三 四 五 六"<<endl<<" ";
for(int j=0;j<week(y,k,1)*3;j++)
cout<<" ";
for(int i=1;i<n;i++)
{
if(week(2008,k,i)==0)
cout<<endl<<" ";
if(i<10) cout<<" ";
cout<<i<<" ";
}
cout<<endl;
}