[说明] 利用c++的各种控制语句编写一个万年历程序,要求:显示任何年份的日历,日历以月份顺序排列,每月以星期顺序排列,类似于一般挂历上的格式。本程序包含如下两个函数:Leap ()用于判定指定的年份是闰年,Week ()用于计算year年份的1月1日是星期几,其判定规则为:
(1) 如果year 年份为1994年,则为星期六。
(2) 如果year 年份大于1994年,则星期值weekno 按下列公式计算:
differ=(year-1994)*(365%6)+(year-1993)/4-(year-2001)/100+(year-2001)/400 date=6+differ%7
weekno=(date6) date-7:date
(3) 如果year 年份小于1994年,则星期值weekno 按下列公式计算:
differ=(1994-year)*(365%7)+(1996-year)/4-(2001-year)/100+(2000-year)/400 weekno=6-dder%7
# include "iostream. h"
# include "iomanip. h"
int leap(int n)
if( (1) )
return 0
else
return 1;
int week( int year )
int a1, differ, date, weekno;
if (year = = 1994)
a1 =0;
else if (year > 1994)
a1=1;
else a1= -1;
switch(a1)
case 0: return 6; break;
case 1:
(2)
date = 6 + differ% 7; weekno = ( date > 6) date - 7 date;
return weekno; break;
case - 1:
differ = ( 1994 - year) * (365%7) + (1996 - year)/4 - (2001 - year)/100 + (2000 - year)/400;
weekno =6-differ%7; return weekno; break;
void main( )
int i,year,m2,n,j;
cout < < “Please input 某年数:”;
cin> >year;
if ( ! leap(year) )
(3) ;
else
m2 =28;
int month [12]: 31 ,m2,31,30,31,30,31,31,30,31,30,31 ;
(4)
for ( i=0; i<12; i+ + )
cout< < < <end1< <setw(4*n) < <";
for(j=1 ;j< =month [i] ;j+ +)
cout< <setw(4) < <j;
n+ +;
if(n> =7)
(5)
cout < < end1;
参考答案:(n%100 = =0) ||(n%400 = =0)
differ= (year - 1994) * (365%6) + (year - 1993)/4 - (year-2001)/100 + (year-2001 )/400;
m2 =29
n = week( year );
n=0;