{
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31}
int y,m,d
if(year%4||year%100&&!(year%400))/前拦/闰年
{
if(b[month-1]>day)
{d=day+1m=monthy=year}
else
{d=1m=month+1y=year}
}
else
{
if(a[month-1]>day)
{d=day+1m=monthy=year}
else
{d=1m=month+1y=year}
}
if(m>做悔州12){m=m-12y++}
printf("%d年纯蔽%d月%d日\n",y,m,d)
}
1. 输入是否合法day_list[12] = {31,28,31,30,31,30,31,31,30,31,30,31}
// 判断条件:
( year>=1920 &&year<=2050 ) &&
(
( month>=1 &&month<=12 &&day>=1 &&day<=day_list[month-1] )||
( month==2 &&isLeap(year) &&day>=1 &&day<=29 )
)
2.
/*
函数说明: int NextDate(int month,int day,int year)
如果还有明天,返回1;
如果输入不合法,返回-1;
如果超闷胡盯出处理范围,返回-2
*/
int bLeapYear
int newY,newM,newD
newY = year
newM = month
newD = day
bLeapYear = ( (year%4)==0 &&(year%100)!=0 ) || (year%400)==0
if ( year>=1920 &&year<=2050 )
{
if ( month>=1 &&month<=12 &&day>=1 )
{
if ( day<day_list[month-1] )
{
newD += 1
}
else if ( day==day_list[month-1] &&!bLeapYear )
{
if ( month==12 &&day==31 )
{
if ( year==2050 ) return -2
else
{
newY += 1newM = 1newD = 1
}
}
else
{
newM += 1newD = 1
}
}
else if ( month==2 &&bLeapYear )
{
if ( day==28 ) newD = 29
else if( day==29 )
{ newM = 3newD = 1}
else return -1
} else return -1
}
}
// 输出
return 1
3. 测试方法:
3.1 闰年二月的最后一天
3.2 闰年二做启月的第28天
3.3 非闰年二月的第28天
3.4 每年的非二月的月末
3.5 每年的十二月的月末
3.6 2050年十二月的月末
3.7
year>2050 || year<1920 || month<1 || month>12 || day<1 ||
(day>day_list[month-1] &&month>=1 &&month<=12 &&month!=2 ) ||
(day>28 &&month==2 &&!bLeapYear ) ||
(day>29 &&month==2 &&bLeapYear )
3.8 闰年非二月的月末 (3.4的分支)
测试出程蚂和序错误:
4.1
else if ( day==day_list[month-1] &&!bLeapYear )
应改为:
else if ( day==day_list[month-1] &&month!=2 )
4.2
else if ( month==2 &&bLeapYear )
{
if ( day==28 ) newD = 29
else if( day==29 )
{ newM = 3newD = 1}
else return -1
}
应改为:
else if ( month==2 )
{
if ( day==28 &&bLeapYear ) newD = 29
else if( day==28 &&!bLeapYear || ( day==29 &&bLeapYear ) )
{ newM = 3newD = 1}
else return -1
}
我写了一个程序,不过,可能有一些地方,我做了一些修改,你看看吧:#include<iostream.h>#include<stdlib.h>
class date{
int year,month,day
public:
date(int y=0,int m=0,int d=0)
{
year=ymonth=mday=d
}
date(char *s)
{
year=atol(s)+2000
month=atol(s)+5
day=atol(s)}
void input()
{
cout<<"请输入期日,依次输入年、月、日:"
cin>>year>>month>>day
}
void ouput()
{
cout<<"日期:"<<year<<"年"<<颂烂颤month<<"月"<<day<<"日\n"野败
}
void nextdte()
{
day++
}
void pirrodate()
{
day--
}
void dateafter(int d)
{
day=day+d
}
void datebefore(int d)
{
day=day-d
}
}
void main()
{
date Date1(2010,12,12),Date2,Date3("8")
Date1.ouput()
Date2.input()
Date2.ouput()
Date2.input()
Date2.nextdte()
cout<<"下一天历粗\n"
Date2.ouput()
cout<<"另一个日期:\n"
Date3.ouput()
Date3.dateafter(6)
cout<<"过6之后的日期是:\n"
Date3.ouput()
Date3.datebefore(3)
cout<<"以上日期的前3天是:\n"
Date3.ouput()}
演示:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)