一个超级简单的万年历程序,征求高手意见!
#include <iostream.h>#include <iomanip.h>
int week (int y, int m, int d) //日期换算星期
{
if (m==1||m==2) y=y-1,m=m+12;
return ((d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7);
}
bool leap(int y) //判断闰年
{
return ((y%4==0&&y%100!=0)||(y%400==0));
}
void main()
{
int year,month,day,t,i;
char c;
bool leap(int y);
int week(int y,int m,int d);
N:cout<<"请输入正确的年份和月份(例如:2008 9):";
cin>>year>>month;
if(year<=0||month<=0||month>12) goto N;
cout<<"\n "<<"公元"<<year<<"年"<<month<<"月\n";
cout<<" ***************************************** \n";
if (month==4||month==6||month==8||month==9||month==11) day=30;
else
if(month==2)
if(leap(year)==1) day=29;
else day=28;
else day=31;
cout<<" Mon Tue Wed Thu Fri Sat Sun"<<endl;
t=week(year,month,1)*6+4;
if(t==40) cout<<" "<<setw(2)<<"1"<<endl;
else
{
for (i=1;i<=t;i++) cout<<" ";
cout<<setw(2)<<"1";
}
for(i=2;i<=day;i++)
{
t=week(year,month,i);
if(t==6) cout<<" "<<setw(2)<<i<<endl;
else cout<<" "<<setw(2)<<i;
}
cout<<endl;
cout<<" ***************************************** \n";
while(1)
{
cout<<"\n重新输入(N)| 退出(Q):";
cin>>c;
if (c=='N') goto N;
if (c=='Q') break;
}
}
[[it] 本帖最后由 tinx 于 2008-10-20 22:27 编辑 [/it]]