题目描述:
编写一个函数,输入日期和时间,输出该日期和时间的下一秒。例如输入2004年12月31日23时59分59秒,输出为
2005年1月1日0时0分0秒。
应该是考察日期时间的基本知识以及C/C++的结构化编程。
下面是一段测试代码:
inline bool isLeap(int year){/*判断是否是闰年*/
if(year<0){
year=-year;
}
return (year%4==0 && year%100!=0 || year%400==0)?true:false;
}
inline bool checkBound(int param,int min,int max){/*判断整数范围*/
return (param
}
bool checkParam(int * year,int * month,int* day,int* hour,int* minute,int* second)
{/*参数检查*/
if(NULL==second ||!checkBound(*second,0,59)||/
NULL==minute ||!checkBound(*minute,0,59)||/
NULL==hour|| !checkBound(*hour,0,23)||/
NULL==day ||NULL==month ||NULL==year)
return false;
switch(*month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(!checkBound(*day,1,31))
return false;
break;
case 4:
case 6:
case 9:
case 11:
if(!checkBound(*day,1,30))
return false;
break;
case 2:
if(isLeap(*year)){
if(!checkBound(*day,1,29))
return false;
}
else{
if(!checkBound(*day,1,28))
return false;
}
break;
default:
return false;
}
return true;
}
void nextSecond(int * year,int * month,int* day,int* hour,int* minute,int* second)
{
if(!checkParam(year,month,day,hour,minute,second))return ;
(*second)++;
if(*second == 60){
*second=0;
(*minute)++;
if(*minute == 60){
*minute=0;
(*hour)++;
if(*hour == 24){
*hour=0;
(*day)++;
switch(*month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(*day==32){
*day=1;
(*month)++;
}
break;
case 4:
case 6:
case 9:
case 11:
if(*day==31){
*day=1;
(*month)++;
}
break;
case 2:
if(isLeap(*year)){
if(*day == 30){
*day=1;
(*month)++;
}
}
else{
if(*day==29){
*day=1;
(*month)++;
}
}
break;
}
if(*month==13){
*month=1;
(*year)++;
}
}
}
}
}
还有一种方法是使用C/C++库函数,先把日期和时间转化为从一个起点开始所经历的秒数,然后把该秒数加1
再转化回来。。。