To convert a string to an integer to take into account a lot of cases, first of all, the string is a null pointer processing, the string starts with the "+"-"case, the string is not the number of spaces, etc., for the processing of these cases, the main use of the method is to define a global variable, in the case of some special cases , the method of changing global variables, as the identifier, so that you can achieve the desired effect.
int symbol =-1;
int fun (const char *SRC)
{Long Long num = 0;
int flag = 1;
int symbol = 1;
while (src! = NULL && *src! = ') '
{
if (*src = = '-')
{
src++;
flag =-1;
}
if (*src = = ' + ')
src++;
if (*src! = ')
{
if (*src >= ' 0 ' &&*src <= ' 9 ')
{
num = num * + *src-' 0 ';
if ((Flag&&num > 0x7FFFFFFF) | | (!flag&&num < (signed int) 0x80000000))
{
num = 0;
Break
}
}
Else
{
num = 0;
Break
}
src++;
if (*src = = ' + ')
{
symbol =-1;
}
}
}
return num*flag;
}
int main ()
{
Char *arr = "1234";
int ret = fun (arr);
printf ("%d", ret);
System ("pause");
return 0;
}
Convert a string to an integer