Enter a string ending with #, which requires filtering out all non-hexadecimal characters (not case), forming a new string representing a hexadecimal number, and then converting it to a decimal number after the output. If the first character of the filtered string is "-", it indicates that the number is negative.
Input format:
The input gives a non-empty string that ends with # in a row.
Output format:
Outputs the converted decimal number in a row. The title ensures that the output is within the long range.
Input Sample:
+p-xf4+-1!#
Sample output:
-3905
#include <iostream> #include <string>using namespace Std;int main () { string s; int i,sum=0; BOOL Flag=false,f=false; Getline (cin,s); for (i=0;s[i]!= ' # '; ++i) { if (s[i]== '-' &&!flag) f=true; else if (s[i]<= ' 9 ' &&s[i]>= ' 0 ') { sum=sum*16+s[i]-' 0 '; flag=true; } else if (s[i]<= ' F ' &&s[i]>= ' a ') { sum=sum*16+s[i]-' a ' +10; flag=true; } else if (s[i]<= ' F ' &&s[i]>= ' a ') { sum=sum*16+s[i]-' a ' +10; flag=true; } } if (f) sum=-sum; cout<<sum<<endl; return 0;}
Array-08. String converted to decimal integer (15)