#include#include enum Status { kValid = 0, kInvalid };int g_nStatus = kValid;long long Str2IntCore(const char* digit, bool minus);int Str2Int(const char* str) { g_nStatus = kInvalid; long long num = 0; if(str != NULL && *str != '\0') { bool minus = false; if(*str == '+') str++; else if(*str == '-') { minus = true; str++; } if(*str != '\0') num = Str2IntCore(str, minus); } return (int)num;}long long Str2IntCore(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 + flag*(*digit - '0'); if((!minus && num > 0x7FFFFFFF) || (minus && num < (signed int)0x80000000)) { num = 0; break; } digit++; } else { num = 0; break; } } if(*digit == '\0') g_nStatus = kValid; return num;}void Test(const char* testName, const char* str, int expected) { if(testName != NULL) printf("%s begins: ", testName); if(expected == Str2Int(str)) printf("passed.\n"); else printf("failed.\n"); printf("g_nStatus: %d\n", g_nStatus);}//positivevoid Test1() { char* str = "7865"; Test("Test1", str, 7865);}//negativevoid Test2() { char* str = "-7865"; Test("Test2", str, -7865);}//NULLvoid Test3() { Test("Test3", NULL, 0);}//""void Test4() { char* str = ""; Test("Test4", str, 0);}//Invalidvoid Test5() { char* str = "+8j7"; Test("Test5", str, 0);}//beyondvoid Test6() { char* str = "5463728392"; Test("Test6", str, 0);}int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0;}