Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

atoi函数源代码

Posted on 2010-09-06 13:13 Prayer 阅读(7471) 评论(2)  编辑 收藏 引用 所属分类: C/C++

isspace(int x)
{
 if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r')
  return 1;
 else 
  return 0;
}
isdigit(int x)
{
 if(x<='9'&&x>='0')        
  return 1;x`
 else
  return 0;

}
int atoi(const char *nptr)
{
        int c;              /* current char */
        int total;         /* current total */
        int sign;           /* if '-', then negative, otherwise positive */

        /* skip whitespace */
        while ( isspace((int)(unsigned char)*nptr) )
            ++nptr;

        c = (int)(unsigned char)*nptr++;
        sign = c;           /* save sign indication */
        if (c == '-' || c == '+')
            c = (int)(unsigned char)*nptr++;    /* skip sign */

        total = 0;

        while (isdigit(c)) {
            total = 10 * total + (c - '0');     /* accumulate digit */
            c = (int)(unsigned char)*nptr++;    /* get next char */
        }

        if (sign == '-')
            return -total;
        else
            return total;   /* return result, negated if necessary */
}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/udknight/archive/2007/10/22/1836799.aspx

Feedback

# re: atoi函数源代码  回复  更多评论   

2012-04-18 23:20 by 春秋十二月
实现基本可以,但没处理数据溢出和字符集的情况

# re: atoi函数源代码  回复  更多评论   

2014-11-04 21:30 by 张瑞昌
对啊,怎么源码都没处理溢出问题!

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理