字符串轉爲int類型

題目:

字符串轉爲int類型

解析:

題目比較簡單,重要的是要思考全面,所以這裏不過多贅述

注意:

1 判斷字符串是否爲空

2 判斷正負號

3 判斷字符串轉換是否超出int類型表示範圍

4 如果轉換出錯,返回什麼(博客中返回0,設置標誌位,判斷0是正常返回0,還是轉換出錯返回0)


算法實現:

enum Status {KVilid = 0,KInVilid};
Status gStatus = KVilid;

int StrToInt(const char* str)
{
    gStatus = KInVilid;
    long long int num = 0;

    //如果字符串爲空,則返回0,gStatus標識設爲KInVilid
    if(nullptr == str || strlen(str) < 1)
    {
        return static_cast<int>(num);
    }

    const char* digit = str;
    bool minus = false;

    //判斷正負號
    if('+' == *digit)
    {
        ++digit;
    }
    else if('-' == *digit)
    {
        minus = true;
        ++digit;
    }


    //str轉換爲數字
    while('\0' != *digit)
    {
        //如果字符串內字符不合法,則num設爲0,gStatus設爲KInVilid
        if(*digit > '9' || *digit < '0')
        {
            num = 0;
            break;
        }

        num = num * 10 + (*digit - '0');

        //判斷是否超出int表示範圍
        if(num > std::numeric_limits<int>::max())
        {
            num = 0;
            break;
        }
        ++digit;
    }

    if('\0' == *digit)
    {
        gStatus = KVilid;
        if(minus)
        {
            num = 0 - num;
        }
    }
    return static_cast<int>(num);
}


算法測試代碼:

#include <iostream>


using namespace std;

enum Status {KVilid = 0,KInVilid};
Status gStatus = KVilid;

int StrToInt(const char* str)
{
    gStatus = KInVilid;
    long long int num = 0;

    //如果字符串爲空,則返回0,gStatus標識設爲KInVilid
    if(nullptr == str || strlen(str) < 1)
    {
        return static_cast<int>(num);
    }

    const char* digit = str;
    bool minus = false;

    //判斷正負號
    if('+' == *digit)
    {
        ++digit;
    }
    else if('-' == *digit)
    {
        minus = true;
        ++digit;
    }


    //str轉換爲數字
    while('\0' != *digit)
    {
        //如果字符串內字符不合法,則num設爲0,gStatus設爲KInVilid
        if(*digit > '9' || *digit < '0')
        {
            num = 0;
            break;
        }

        num = num * 10 + (*digit - '0');

        //判斷是否超出int表示範圍
        if(num > std::numeric_limits<int>::max())
        {
            num = 0;
            break;
        }
        ++digit;
    }

    if('\0' == *digit)
    {
        gStatus = KVilid;
        if(minus)
        {
            num = 0 - num;
        }
    }
    return static_cast<int>(num);
}



void TestStrToInt(const char *str,int n)
{
    if(gStatus == KVilid)
    {
        cout<<"str ="<<str<<" int = "<<n<<endl;
    }
    else
    {
        if(nullptr != str && strlen(str) >= 1)
        {
            cout<<"error ,can't translate str:"<<str<<" to Int"<<endl;
        }
        else
        {
            cout<<"error, str is null"<<endl;
        }
    }
}

int main()
{
    const char* str1 = "";
    const char* str2 = nullptr;
    const char* str3 = "10000000000000000";
    const char* str4 = "+100000000";
    const char* str5 = "-100";
    const char* str6 = "+1000000000000000";
    const char* str7 = "-1000000000000000";

    TestStrToInt(str1,StrToInt(str1));
    TestStrToInt(str2,StrToInt(str2));
    TestStrToInt(str3,StrToInt(str3));
    TestStrToInt(str4,StrToInt(str4));
    TestStrToInt(str5,StrToInt(str5));
    TestStrToInt(str6,StrToInt(str6));
    TestStrToInt(str7,StrToInt(str7));


    return 0;
}

運行結果:

         


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章