/*
m_atoi
@author arhaiyun
*/
#include<assert.h>
bool g_bInvalid = false;
int m_atoi(char* str)
{
assert(str != NULL);
long long result = 0;
while(*str == ' ' || *str == '\n' || *str == '\t')
str++;
int flag = 1;
if(*str == '+')
{
str++;
}
else if(*str == '-')
{
str++;
flag = -1;
}
if(*str != '\0')
{
result = atoi_core(str, flag);
}
return (int)result;
}
long long atoi_core(char* str, int flag)
{
assert(str != NULL);
long long result = 0;
while(*str != '\0')
{
if(*str >= '0' && *str <= '9')
{
result = result * 10 + flag * (*str - '0');
if(result > 0x7fffffff || result < 0x80000000)
{
result = 0;
g_bInvalid = true;
break;
}
}
else
{
result = 0;
g_bInvalid = true;
break;
}
}
return result;
}
atoi函数 int m_atoi(char* str)
最新推荐文章于 2022-10-19 14:41:23 发布