C ++ atoi関数のソースコードの自己実装



C Atoi Function Source Code Self Implementation

int myatoi(const char *nptr) // convert the string to a long integer { int result = 0 const char *p = nptr++//Save the initial address and increment by one while (*nptr!='') { if (*nptr>='0'&&*nptr<='9')//If the second digit starts with characters 0-9 { result *= 10 result+=*nptr-48//Convert to the corresponding int integer nptr++//Continue to increase } else { break//Exit the loop when a non-character 0-9 is encountered } } Output the corresponding result according to the first address value if (*p == '+'||*p=='0') { return result } else if (*p == '-') { return -result } else { return 0 } }//end aoti function