Implements the Atoi () function, which returns 0 on error;
1 intAtoiConst Char*str)2 {3 if((str = = NULL) | | (strlen (str) = =0)) {4 return 0;5 }6 7 intValue =0;8 intSign =0;9 intindex =0;Ten intMinmark =0; One Const intMAX =2147483647; A Const intMIN =-2147483648; - while(Str[index]! =' /') { - Charc =Str[index]; the if(c = ='+') || (c = ='-')) { - if(Sign = =0) { -Sign = (c = ='+'?1: -1); -}Else { + return 0; - } +}Else if(c >='0') && (c <='9')) { A if(Value > MAX/Ten) { at return 0; -}Else if(Value = = MAX/Ten) { - ifC'0'> MAX%Ten+1) { - return 0; -}Else if(C-'0'= = MAX%Ten+1) && (sign = =-1) && (Minmark = =0)) { -Minmark =1; inValue = value *Ten+ MAX%Ten; -}Else ifC'0'<= MAX%Ten) { toValue = value *Ten+ C-'0'; +}Else { - return 0; the } *}Else{ $Value = value *Ten+ C-'0';Panax Notoginseng } -}Else { the return 0; + } AIndex + +; the } + if(Minmark = =1) { - returnMIN; $}Else if(Sign = =-1) { $Value *=-1; - } - returnvalue; the}
Atoi () function implementation