Created
November 9, 2009 04:56
-
-
Save shaobin0604/229702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ctype.h> | |
/* | |
* 将十进制字符串转换为数值。可处理前导的 + 和 - | |
* | |
* 如果有空白符,则跳过 | |
* 如果有符号,则读取符号 | |
* 取整数部分,并执行转换 | |
* 当遇到第一个不能转换为数字的字符时,整个处理过程终止 | |
*/ | |
int atoi(char s[]) { | |
int i, n, sign; | |
for (i = 0; isspace(s[i]); i++) | |
; | |
sign = (s[i] == '-') ? -1 : 1; | |
if (s[i] == '+' || s[i] == '-') | |
i++; | |
for (n = 0; isdigit(s[i]); i++) | |
n = 10 * n + (s[i] - '0'); | |
return sign * n; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment