bool isIn_IntRange( string strInputNum )
{
char cMaxInt[20];
snprintf(cMaxInt, sizeof(cMaxInt)-1, "%d", numeric_limits<int>::max() );
char cMinInt[20];
snprintf(cMinInt, sizeof(cMinInt)-1, "%d", numeric_limits<int>::min() );
int i = 0;
strInputNum=trim(strInputNum);
if( strInputNum.length() > 1 ) //去除开头为0的情况
{
while( i < strInputNum.length() )
{
if( '0' == strInputNum[i] )
{
i++;
}
else
break;
}
}
string strNum = strInputNum.substr(i);
cout << "test" << strNum << endl;
if( '-' == strNum[0] )
{
if( strlen( strNum.c_str() ) > strlen(cMinInt) )
{
return false;
}
else if( strlen( strNum.c_str() ) == strlen( cMinInt ) )
{
if( strcmp(strNum.c_str(), cMinInt) > 0 )
return false;
else
return true;
}
}
else
{
if( strlen( strNum.c_str() ) > strlen(cMaxInt) )
{
return false;
}
else if( strlen( strNum.c_str() ) == strlen( cMaxInt ) )
{
if( strcmp(strNum.c_str(), cMaxInt) > 0 )
return false;
else
return true;
}
}
return true;
}
字符串形式表示的整数的范围判断
最新推荐文章于 2021-05-12 17:14:56 发布