1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main(){ 5 string s; 6 int i,sum=0; 7 bool flag=false,f=false; 8 getline(cin,s); 9 for(i=0;s[i]!=‘#‘;++i){ 10 if(s[i]==‘-‘&&!flag) 11 f=true; 12 else if(s[i]<=‘9‘&&s[i]>=‘0‘){ 13 sum=sum*16+s[i]-‘0‘; 14 flag=true; 15 } 16 else if(s[i]<=‘f‘&&s[i]>=‘a‘){ 17 sum=sum*16+s[i]-‘a‘+10; 18 flag=true; 19 } 20 else if(s[i]<=‘F‘&&s[i]>=‘A‘){ 21 sum=sum*16+s[i]-‘A‘+10; 22 flag=true; 23 } 24 } 25 if(f) 26 sum=-sum; 27 cout<<sum<<endl; 28 return 0; 29 }
数组-08. 字符串转换成十进制整数(15),布布扣,bubuko.com
原文地址:http://www.cnblogs.com/gnodidux/p/3822819.html