int Myatoi(const char* str)
{
//检查参数合法性
assert(str != NULL);
if (str == NULL)
{
return 0;
}
int s = 0;//保存整型数值
int tag = 1;//标记
//碰到空格继续往前走
while (*str == ' ')
{
str++;
}
//处理符号
if (*str == '-')
{
tag = -1;//如果是-号标记变为-1
str++;
}
else if (*str == '+')
{
str++;
}
//处理数字字符
while (isdigit(*str))
{
s = s * 10 + (*str - '0');
str++;
}
return s*tag;
}
C语言实现myatoi函数
最新推荐文章于 2024-03-05 12:38:02 发布