// Pregunta de la entrevista 67: Convertir una cadena en un entero // Pregunta: Escriba una función StrToInt para realizar la función de convertir una cadena en un entero. Por supuesto, no se // puede utilizar atoi u otras funciones de biblioteca similares. #include <cstdio> long long StrToIntCore ( const char * str, bool minus); enum Status {kValid = 0 , kInvalid}; int g_nStatus = kValid; int StrToInt ( const char * str) { long long num = 0 ; g_nStatus = kInvalid; if (str! = nullptr && * str! = ' \ 0 ' ) // La cadena de procesamiento es un puntero nulo o "" { bool minus = false ; // signo negativo if (* str == ' + ' ) ++ str; sino if (* str == ' - ' ) { ++ str; minus = true ; } if (* str! = ' \ 0 ' ) // Manejar el problema del símbolo único num = StrToIntCore (str, minus); } return ( int ) num; } long long StrToIntCore ( const char * digit, bool minus) { long long num = 0 ; while (* digit! = ' \ 0 ' ) { if (* digit> = ' 0 ' && * digit <= ' 9 ' ) { int flag = minus? - 1 : 1 ; num = num * 10 + bandera * (* dígito - '0 ' ); if ((! Menos && num> 0x7FFFFFFF ) // Desbordamiento || (menos && num <(firmado int ) 0x80000000 )) { num = 0 ; break ; } ++ digit; } else // cadena no válida { num = 0 ; break ; } } if (* digit == ' \ 0 ' ) g_nStatus = kValid; retorno num; }
// ==================== 测试 代码 ==================== prueba nula ( const char * cadena ) { int resultado = StrToInt ( cadena ); if (result == 0 && g_nStatus == kInvalid) printf ( " la entrada% s no es válida. \ n " , cadena ); else printf ( "el número para% s es:% d. \ n " , cadena , resultado); } int main ( int argc, char * argv []) { Prueba (nullptr); Prueba ( "" ); Prueba ( " 123 " ); Prueba ( " +123 " ); Prueba ( " -123 " ); Prueba ( " 1a33 " ); Prueba ( " +0 " ); Prueba ( " -0 " ); // 有效 的 最大 正 整数, prueba 0x7FFFFFFF ( " +2147483647 " ); Prueba ( " -2147483647 " ); +2147483648 " ); // Entero negativo mínimo válido, 0x80000000 Prueba ( " -2147483648 " ); Prueba ( " +2147483649 " ); Prueba ( " -2147483649 " ); Prueba ( " + " ); Prueba ( " - " ); devuelve 0 ; }
Análisis: Considere varias entradas accidentales.
clase Solución { public : enum Status {kValid = 0 , kInvalid}; int g_nStatus = kValid; int StrToInt ( string str) { long long num = 0 ; g_nStatus = kInvalid; int longitud = str.length (); if (! str.empty ()) { int current = 0 ; bool menos = falso ; if (str [actual] == ' +' ) ++ actual; sino if (str [current] == ' - ' ) { ++ current; menos = verdadero ; } if (actual! = longitud) num = StrToInt (str, actual, menos); } return ( int ) num; } largo largo StrToInt ( dígito de cadena , int actual, bool menos) { largo largo num = 0 ; int longitud = digit.length (); int i = 0 ; for (i = current; i <length; ++ i) { if (digit [i] <= ' 9 ' && digit [i]> = ' 0 ' ) { int flag = minus? - 1 : 1 ; num = num * 10 + bandera * (dígito [i] - ' 0 ' ); if ((! menos && num> 0x7FFFFFFF) || (menos && num <(firmado int ) 0x80000000 )) { num = 0 ; romper ; } } else { num = 0 ; romper ; } } if (i == length) g_nStatus = kValid; retorno num; } };