标签:style blog color ar div amp log on
StrToInt:字符串转int输出
1 enum Status 2 { 3 kValid = 0, 4 kInvalid = 1, 5 }; 6 7 int StrToInt(const char* str) 8 { 9 g_nStatus = kInvalid; 10 long long num = 0; 11 12 if(str!=NULL && *str != ‘\0‘) 13 { 14 bool minus = false; 15 while(*str =="") 16 str++; 17 if(*str ==‘+‘) 18 { 19 str++; 20 } 21 else if(*str==‘-‘) 22 { 23 str++; 24 minus = true; 25 } 26 27 if(*str !=‘\0‘) 28 { 29 num = StrToIntCore(str, minus); 30 } 31 } 32 return (int)num; 33 } 34 35 int StrToIntCore(const char *digit, bool minus) 36 { 37 long long num = 0; 38 while(*digit !=‘0‘) 39 { 40 if(*digit >=‘0‘ && *digit<=‘9‘) 41 { 42 int flag = minus ? -1:1; 43 num =num*10 + flag*(*digit -‘0‘); 44 45 //判断整数是否发生上溢/下溢 46 if((!minus && num > 0x7FFFFFFF) //最大正数:2^32-1 47 || (minus && num <(signed int)0x80000000))//最小负数:2^32 48 { 49 num = 0; 50 break; 51 } 52 digit++; 53 } 54 else 55 { 56 num = 0; 57 break; 58 } 59 } 60 if(*digit==‘\0‘) 61 { 62 g_nStatus = kValid; 63 } 64 65 return num; 66 }
标签:style blog color ar div amp log on
原文地址:http://www.cnblogs.com/chris-cp/p/3915167.html