当前位置:网站首页>Niu Ke swipes the question -- converting a string to an integer

Niu Ke swipes the question -- converting a string to an integer

2022-06-11 18:29:00 HHYX.

Topic link : Convert a string to an integer

Title Description

Convert a string to an integer , Library functions that require that integers cannot be converted using strings . Values for 0 Or if the string is not a valid value, it returns 0

Data range : The string length satisfies 0 \le n \le 100 \0≤n≤100
Advanced : Spatial complexity O(1) \O(1) , Time complexity O(n) \O(n)

Be careful :
① Any symbol... May appear in the string , Occurrence Division +/- Output directly when other symbols 0
② Possible in string +/- And can only appear at the beginning of the string .
Input description :
Enter a string , Including alphanumeric symbols , Can be null
Return value description :
If it is a legal numeric expression, it returns the number , Otherwise return to 0
 Insert picture description here

Topic analysis

It is required to convert the string into an integer , Library functions, etc. cannot be used . If the string contains illegal characters, it returns 0. To implement this transformation, you can first judge the sign's positive and negative , The sign can only appear on the first character , So you can judge first . If there is no sign, the string conversion is performed directly . Use a loop to iterate through the string , To convert characters to numbers in turn, you only need to subtract characters from numeric characters 0 that will do , Then sum it up . The summation formula is sum=sum*10+(str[i]-‘0’), So we can find the sum , Finally, calculate according to the positive and negative . The code implementation is as follows :

Code implementation

int StrToInt(string str) {
    
                int i = 0;
        int sum = 0;// Return value   Integer size 
        int flag = 1;// Positive and negative judgment basis  1 Representative is  0 It means negative 
        for (i = 0; i < str.size(); i++)
        {
    
            if (i != 0 && (str[i] < '0' || str[i]>'9'))// Illegal characters appear 
            {
    
                return 0;
            }
            else
            {
    
                if (i == 0)
                {
    
                    if (str[i] == '+')
                    {
    
                        flag = 1;
                        continue;
                    }
                    else if (str[i] == '-')
                    {
    
                        flag = 0;
                        continue;

                    }
                    else
                    {
    
                        sum = sum * 10 + (str[i] - '0');
                        continue;
                    }
                }
                sum = sum * 10 + (str[i] - '0');
            }

        }
        if (flag == 0)
        {
    
            sum *= -1;
        }
        return sum;
    }

 Insert picture description here

原网站

版权声明
本文为[HHYX.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111814367365.html