【发布时间】:2017-03-15 04:05:30
【问题描述】:
我正在尝试重新创建 atoi,但我想知道为什么我的函数有效。我最终将三个顶级语句的它更改为 str[i],因为它对我来说很有意义,但它通过了我扔给它的所有内容。
i = 0;
result = 0;
negative = 1;
if (str[0] == '-')
{
negative = -1;
i++;
}
if (str[0] == '+')
i++;
while (str[0] <= ' ')
i++;
while (str[i] != '\0')
if (str[i] >= '0' && str[i] <= '9')
{
result = result * 10 + str[i] - '0';
++i;
}
return (result * negative);
【问题讨论】:
-
在最后的 while 循环中,您忽略了任何不是数字的内容。这是你的意思还是应该抛出错误?
12A3现在将返回123。
标签: string loops if-statement while-loop