[Simple Algorithm] 17. String to Integer (atoi)

topic:

Implement atoi to convert strings to integers.

The space character needs to be removed from the string before the first non-blank character is found. If the first non-blank character is a positive sign or a negative sign, select that sign and combine it with as many consecutive digits as possible. This part of the character is the value of the integer. If the first non-blank character is a digit, it is directly combined with subsequent consecutive digit characters to form an integer.

Strings can include extra characters after the characters that form the integer, these characters can be ignored, they have no effect on the function.

No conversion occurs when the first sequence of non-empty characters in the string is not a valid integer; or when the string is empty; or when the string contains only whitespace characters.
Returns 0 
if the function cannot perform a valid conversion .

illustrate:

Suppose our environment can only store 32 -bit signed integers whose values ​​are in the range [− 231 ,   2311 ]. Returns INT_MAX ( 2311 ) or INT_MIN (− 231 ) if the value exceeds the representable range .

Example 1 :

Input: " 42 " 
Output: 42 
Example 2 :

Input: "    -42 " 
Output: - 42 
Explanation: The first non-whitespace character is ' - ' , which is a minus sign.
     We try to combine the minus sign with all consecutive numbers that follow, and we end up with -42 .
Example 3 :

Input: " 4193 with words " 
Output: 4193 
Explanation: Conversion ends at digit ' 3 ' because its next character is not a digit.
Example 4 :

Input: " words and 987 " 
Output: 0 
Explanation: The first non-blank character is ' w ' , but it is not a number or a positive or negative sign.
     Therefore a valid conversion cannot be performed.
Example 5 :

Input: " -91283472332 " 
Output: - 2147483648 
Explanation: The number " -91283472332 " exceeds the 32 -bit signed integer range.
     So INT_MIN (− 231 ) is returned.

1. Problem solving ideas:

This question is not particularly difficult, but be sure to pay attention to the problem of removal, as well as illegal characters. It is not easy to write this question without errors, and the problem of overflow in the test case.

#define INT_MAX 2147483647
#define INT_MIN -2147483648

bool isDigital(char p){    
    return (p>='0'&& p<='9')?true:false;
}

bool isSpace(char p){
    return p == ' '?true:false;
}

bool isFlag(char p){
    return (p == '-'||p == '+')?true:false;
}

int myAtoi(char* str) {
    char *p = str;
    int number = 0;
    char flag = 0;
    bool isCal = false;
    
    if(p == NULL){
        return 0;
    }
    //printf("%s\n\r",str);
     
    while((*p)!='\0'){
        if(isDigital((*p))){
            printf("digital is %c\n\r",*p);
            isCal = true;
            if(number > 214748364){
                if(flag == 1 || flag == 0){
                    return INT_MAX;
                }                  
                if(flag == -1){
                    return INT_MIN;
                }
            }
            
            if(number == 214748364){
                if(flag == 1 || flag == 0){
                    if((*p)>'7'){
                        return INT_MAX;
                    }
                }                
                if(flag == -1){
                    if((*p)>'8'){
                        return INT_MIN;
                    }
                }
            }       
            number = number*10 + (*p) - '0';   
        }else if(isSpace(*p)){
            if(number!=0){
                break;
            }else{
                if(isCal||flag!=0){
                    break;
                }
            }
            while((*p) == ' '){
                p++;
            }
            continue;
        }else if(isFlag(*p)){
            if(flag == 0){
                if((*p) == '-'){
                    flag = -1;
                }else{
                    flag = 1;
                }
            }else{
                break;
            }
        }else{
            break;
        }        
        p++;
    }
    
    if(flag == -1){
        number = number * (-1);
    }        
    return number;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205013&siteId=291194637