标签:转换 == sig uri 剑指offer clu enum ext ack
1 #include <iostream> 2 using namespace std; 3 class Solution { 4 public: 5 enum status {kValid=0,kInvalid}; 6 int g_nStatus = kValid; 7 int StrToInt(string str) { 8 g_nStatus = kInvalid; 9 long long num = 0; 10 const char *cstr = str.c_str(); 11 if(cstr != NULL && *cstr != ‘\0‘) 12 { 13 int minus = 1;//符号 14 if(*cstr == ‘-‘) 15 { 16 minus = -1; 17 cstr++; 18 } 19 else if(*cstr == ‘+‘) 20 { 21 cstr++; 22 } 23 while(*cstr != ‘\0‘)//记得是*cstr 24 { 25 if(*cstr >= ‘0‘ && *cstr <= ‘9‘) 26 { 27 num = num*10 + *cstr-‘0‘; 28 cstr++; 29 //数字越界 30 if(((minus > 0) && (num > 0x7FFFFFFF)) || ((minus <0) && (num< (signed int)0x80000000)))//记住加signed int 31 { 32 33 //g_nStatus = kInvalid; 34 num = 0; 35 break; 36 } 37 } 38 else 39 { 40 //g_nStatus = kInvalid; 41 num = 0; 42 break; 43 } 44 } 45 if(*cstr == ‘\0‘) 46 { 47 g_nStatus = kValid; 48 num = num*minus; 49 } 50 } 51 return (int)num; 52 } 53 54 }; 55 int main() 56 { 57 string s = "-123"; 58 Solution a; 59 int res = a.StrToInt(s); 60 cout<<res<<endl; 61 return 0; 62 }
标签:转换 == sig uri 剑指offer clu enum ext ack
原文地址:http://www.cnblogs.com/qqky/p/7082575.html