输入一个以#结束的字符串,本题要求滤去所有的非十六进制字符(不分大小写),组成一个新的表示十六进制数字的字符串,然后将其转换为十进制数后输出。如果在第一个十六进制字符之前存在字符“-”,则代表该数是负数。
输入格式:
输入在一行中给出一个以#结束的非空字符串。
输出格式:
在一行中输出转换后的十进制数。题目保证输出在长整型范围内。
输入样例:
±P-xf4±1!#
输出样例:
-3905
#include <stdio.h>
int main(){
char t;
long s=0;
int flag=0,sign=1;
scanf("%c",&t);
while(t!='#'){
switch(t){
case '-':if(flag==0){
sign=-1;
};
break;
case '0':
s*=16;
flag=1;
break;
case '1':s=s*16+1;
flag=1;
break;
case '2':s=s*16+2;
flag=1;
break;
case '3':s=s*16+3;
flag=1;
break;
case '4':s=s*16+4;
flag=1;
break;
case '5':s=s*16+5;
flag=1;
break;
case '6':s=s*16+6;
flag=1;
break;
case '7':s=s*16+7;
flag=1;
break;
case '8':s=s*16+8;
flag=1;
break;
case '9':s=s*16+9;
flag=1;
break;
case 'a':
case 'A':s=s*16+10;
flag=1;
break;
case 'b':
case 'B':s=s*16+11;
flag=1;
break;
case 'c':
case 'C':s=s*16+12;
flag=1;
break;
case 'd':
case 'D':s=s*16+13;
flag=1;
break;
case 'e':
case 'E':s=s*16+14;
flag=1;
break;
case 'f':
case 'F':s=s*16+15;
flag=1;
break;
default:
break;
}
scanf("%c",&t);
}
printf("%d",sign*s);
return 0;
}