实现atoi

原创 2013年12月05日 23:33:45

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

注意点:空白、正负号、无效字符、溢出  

待完善。。。。。。

一、

int atoi(const char *str)
{
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if(str == NULL || *str == '\0')
        return 0;
    long long res = 0;
    while(*str == ' ')
        str++;

    bool ispositive = true;
    if(*str == '-')
    {
        ispositive = false;
        str++;
    }
    else if(*str == '+')
        str++;
    while(*str && isdigit(*str))
    {
        int tmp = *str - '0';
        res = res*10 + tmp;
        str++;
    }
    if(! ispositive)
        res = -res;
    if(res > INT_MAX) 
        res=INT_MAX;
    else if(res < INT_MIN) 
        res = INT_MIN;
    return res;
}
二、
int atoi(const char *str)
{
    if(str == NULL || *str == '\0')
        return 0;
    int num = 0;
    int sign = 1;
    int i = 0;
    int n = strlen(str);
    while (str[i] == ' ')
        i++;
    if (str[i] == '+')
        i++;
    else if (str[i] == '-')
    {
        sign = -1;
        i++;
    }
    for (; i < n; i++)
    {
        if (str[i] < '0' || str[i] > '9')
            break;
        if (num > INT_MAX / 10 || (num == INT_MAX / 10 &&(str[i] - '0') > INT_MAX % 10))
        {
            return sign == -1 ? INT_MIN : INT_MAX;
        }
        num = num * 10 + str[i] - '0';
    }
    return num * sign;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

atoi函数实现

  • 2013-08-24 21:55
  • 484B
  • 下载

C语言itoa()函数和atoi()函数详解(整数转字符C实现)

C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。 1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(...

我实现的 atoi() strcpy()

  • 2011-08-17 12:11
  • 523B
  • 下载

自己实现的atoi函数

  • 2012-03-27 19:47
  • 362KB
  • 下载

C语言实现atoi,itoa和atof

首先是atoi的实现,它的功能是将字符串转化为整数。 #include #include /* atoi : convert s to integer; version 2*/ int ...

C/C++ 实现 atoi 函数

将一段字符串转换为整数数字,最基本的方法就是使用 atoi 函数。如果让我们自己实现一段 atoi 函数,需要注意的细节比较多。原始版本首先想到的就是字符类型之间的差值。可以直接使用字符相减得到差值。...

【字符串1】atoi的实现

【思路】函数功能很简单,但是要考虑各种特殊条件,下面是一组输入和预期输出 【链接】http://blog.csdn.net/v_july_v/article/details/9024123 in...

【练习题】atoi和itoa函数的实现

int atoi (const char * str); //Convert string to integer char * itoa ( int value, char * str, int...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)