MyAtoi

#include <iostream>
#include <cstring>

int MyAtoi(const char* str)
{
    if (str == NULL)
        return 0;

    int slen = strlen(str);
    if(slen < 0)
    {
        return 0;
    }
    const char* c = str;

    int ret = 0;
    int sign=1;
    while(*c == ' ')
    {
        c++;
    }
    if(*c == '+')
    {
        c++;
    }
    else if(*c == '-')
    {
        sign = -1;
        c++;
    }

    int t = 0;
    while((*c >='0')&&(*c <= '9'))
    {
        t = *c-'0';
        ret = ret *10+t;
        c++;
    }
    return ret * sign;
}

int main()
{
    std::cout << MyAtoi("+124") << std::endl;
    std::cout << MyAtoi("-124") << std::endl;
    std::cout << MyAtoi("    124") << std::endl;
    std::cout << MyAtoi("     +124") << std::endl;
    std::cout << MyAtoi("      -124") << std::endl;

    std::cout << MyAtoi(NULL) << std::endl;
    std::cout << MyAtoi("1d24") << std::endl;
    std::cout << MyAtoi("") << std::endl;

    std::cout << MyAtoi("124") << std::endl;

    return 0;
}

62047bf2jw1ej8yw8wh8qj20m80xcanc 2471f1ec8f8544861a6cc4c3cec0871b 8326cffc1e178a82cafdc7a1f403738da877e8ea 8694a4c27d1ed21bf312cfedaf6eddc450da3fdb 45817afc-59b9-4d3a-93ca-6da6726b04c8_size124_w639_h600 59490a7045b9d7007e3c984254de11c1

原文地址:https://www.cnblogs.com/sunyongjie1984/p/4306211.html