int atoi(char *str)
{
assert(str != NULL);//处理空指针
assert(strlen(str) != 0);//处理空字符串
int sign = 1;
int index = 0;
while (str[index] == ' ')
index++;//处理首部为空
if (str[index] == '-')
{
sign = -1;
index++;
}
else if (str[index] == '+')
{
index++;
}
assert(strlen(str + index) != 0);
int result = 0;
while (str[index] != '\0')
{
assert(str[index] >= '0'&&str[index] <= '9');
result = result * 10 + (str[index] - '0');
index++;
}
result *= sign;
return result;
}
atoi函数
最新推荐文章于 2023-09-12 19:14:19 发布