1、问题
/*
*/
2、算法
#define MAX_LONG 0X7FFFFFFF
long foo(const char* str)
{
int sign = 1 ;
long num = 0 ;
const char* p = str ;
//假设输入的字符串是合法的
if ( *p == ‘-‘ )
{
sign = -1 ;
p++ ;
}else if ( *p == ‘+‘ )
{
sign = 1 ;
p++ ;
}
while(*p)
{
if ( (num > LONG_MAX/10) ||
( (num == LONG_MAX/10) &&
( (-1 == sign && *p > ‘8‘) || (1 == sign && *p > ‘7‘)) ))
{
printf("out of bound") ;
return 0 ;
}
if ( *p >=‘0‘ && *p <=‘9‘ )
{
num = num*10 + (*p - ‘0‘) ;
}
*p++ ;
}
printf("%d", num * sign) ;
return num * sign ;
}
时间: 2024-10-13 18:14:26