全年版万年历,请高手指教
//全年版万年历源代码#include <iostream.h>
#include <iomanip.h>
#include <windows.h>
#include <math.h>
int week (int y, int m, int d) //日期转换星期的函数,返回0是周一,1是周二,以此类推,6是周日
{
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));
}
int Day (int year,int month) //计算某月天数
{
int day;
bool leap(int y);
if (month==4||month==6||month==9||month==11) day=30;
else
if(month==2)
if(leap(year)==1) day=29;
else day=28;
else day=31;
return day;
}
void print( int y,int m,int d) //输出控制函数
{
int a,t;
int week (int y, int m, int d);
int Day (int year,int month);
t=week(y,m,d);
if(d<=Day(y,m))
{
if(d==1)
{
for(a=1;a<=3*t;a++) cout<<" ";
cout<<setw(2)<<d<<" ";
}
else cout<<setw(2)<<d<<" ";
if(t==6)
{
if(m%3==0) cout<<"*"<<endl;
else cout<<"* ";
}
if(d==Day(y,m)&&t!=6)
{
for(a=1;a<=abs(t-6)*3;a++) cout<<" ";
if (m%3==0) cout<<"*"<<endl;
else cout<<"* ";
}
}
else if(t==6) cout<<" * ";
}
void main()
{
int year,month,i,j,k;
char c;
int week (int,int,int);
void print(int,int,int);
int Day (int,int);
N:cout<<" 请输入年份:";
cin>>year;
if(year<0) goto N;
system("cls");
cout<<" 公元"<<year<<"年 "<<endl;
cout<<endl<<"** ** ** ** ** ** ** * ** ** ** ** ** ** ** * ** ** ** ** ** ** ** *"<<endl;
for(month=1;month<=12;month=month+3)
{
j=0;
k=0;
if(month<10) cout<<" "<<month<<"月 * "<<month+1<<"月 * "<<month+2<<"月 * "<<endl;
else cout<<" "<<month<<"月 * "<<month+1<<"月 * "<<month+2<<"月 * "<<endl;
cout<<" * * *"<<endl;
cout<<"一 二 三 四 五 六 七 * 一 二 三 四 五 六 七 * 一 二 三 四 五 六 七 *"<<endl;
for (i=1;i<=35;i++)
{
print(year,month,i);
if(week(year,month,i)==6||i==Day(year,month))
{
j++;
for (;j<=35;j++)
{
print(year,month+1,j);
if(week(year,month+1,j)==6||j==Day(year,month+1))
{
k++;
for(;k<=35;k++)
{
print(year,month+2,k);
if(week(year,month+2,k)==6||k==Day(year,month+2)) break;
}
break;
}
}
}
}
cout<<endl<<"** ** ** ** ** ** ** * ** ** ** ** ** ** ** * ** ** ** ** ** ** ** *"<<endl;
}
while(1)
{
cout<<endl<< "重新输入(1)|退出(2):";
cin>>c;
if(c=='1') goto N;
if(c=='2') break;
}
}
效果截图
" border="0" />