#include<stdio.h>
#include<math.h>
#include<string.h>
//其他任意进制转换为十进制
int main()
{
int trans(char a[],int );
char strupr(char );
char arr[100];
int t;
printf("请输入进制的类型:");
scanf("%d",&t);
printf("请输入该进制的数:");
scanf("%s",arr);
int num = trans(arr,t);
printf("该数的十进制是:%d\n",num);
return 0;
}
char strupr(char ch)//小写字母转换为大写字母
{
if(ch>='a' && ch<='z')
{
ch = ch - 32;
}
printf("%c",ch);
return ch;
}
int trans(char chs[],int m)//转换为十进制
{
char *p = chs;
int len = strlen(p);
int temp,sum=0;
while(len>0)
{
char ss = strupr(*p);
if(ss>='0' && ss<='9')
{
temp = (ss-'0')*pow(m,len-1);//m表示进制类型(2、8、16)
sum = sum + temp;
}
if(ss>='A' && ss<='F')
{
temp = (ss-'A'+10)*pow(m,len-1);
sum = sum + temp;
}
p++;
len--;
}
printf("\n");
return sum;
}