您的位置:首页 > 其它

将字符串转换成对应的整数(简单版,后续会出复杂版的)

2016-04-04 10:09 274 查看
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
int my_atoi(const char *str)
{
assert(str);
int flag = 1;
int ret = 0;
while (isspace(*str))
{
str++;
}
if (*str == '-')
{
flag = -1;
}
if (*str == '+' || *str == '-')
{
str++;
}
while (*str)
{
ret = ret * 10 + (*str - '0');
str++;
}
return ret*flag;
}
int main()
{
char *str = "-12384";
int ret = my_atoi(str);
printf("%d\n", ret);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符 整数 串转换成