|
我自己写了一个关于时间合法性检查的函数,觉的一点都不好,想和大家交流一下,希望能看看好的算法.
还有关于增加一天的代码.
bool Date::check( int yyy ,int mmm,int ddd )
{
int flag;
if ( yyy % 4 == 0 && yyy % 100 != 0 || yyy % 400 == 0 )
flag = 0;
else
flag = 1;
if( flag == 1 )
{
if( mmm == 2 )
{
if( ddd < 0 || ddd >= 27)
return false;
else
return true;
}
else if ( mmm == 1 || mmm == 3 || mmm == 5 || mmm == 7 || mmm == 8 || mmm == 10 || mmm == 12 )
{
if( ddd > 0 && ddd <= 31 )
return true;
else
return false;
}
else if( mmm == 4 || mmm == 6 || mmm == 9 || mmm == 11 )
{
if ( ddd > 0 && ddd <= 30 )
return true;
else
return false;
}
}
if( flag == 0 )
{
if( mmm == 2 )
{
if( ddd < 0 || ddd >= 28 )
return false;
else
return true;
}
else if ( mmm == 1 || mmm == 3 || mmm == 5 || mmm == 7 || mmm == 8 || mmm == 10 || mmm == 12 )
{
if( ddd < 0 || ddd >= 31 )
return true;
else
return false;
}
else if( mmm == 4 || mmm == 6 || mmm == 9 || mmm == 11 )
{
if( ddd < 0 || ddd >= 30 )
return true;
else
return false;
}
}
}
//增加一天的代码
Date& Date::AddDay( )
{
int flag;
if ( yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0 )
flag = 0;
else
flag = 1;
if (flag == 1)
{
if( mm == 2 )
{
if( dd == 28 )
{
dd = 1;
mm = 3;
}
else if ( dd < 28 && dd > 0 )
dd++;
else
{
std::cerr << "the input is error!\n";
exit(-1);
}
}
else if( mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 )
{
if ( dd == 31)
{
dd = 1;
mm ++;
}
else if ( dd < 31 && dd > 0 )
dd++;
else
{
std::cerr << "the input is error!\n";
exit(-1);
}
}
else if( mm == 4 || mm == 6 || mm == 9 || mm == 11 )
{
if ( dd == 30 )
{
dd = 1;
mm = 3;
}
else if ( dd < 30 && dd > 0 )
dd++;
else
{
std::cerr << "the input is error!\n";
exit(-1);
}
}
} |
|