C⼀C++编程题

2024-12-27 07:26:59
推荐回答(1个)
回答1:

#include
#include
using namespace std;

void ChgTo10(char a[], int x);
int main()
{
int x;
cout << "请输入要转换的进制x(x可以是16和10以下的数字:" << endl;
cin >> x;
char num[20];
cout << "请输入一个" << x << "进制的数字:" << endl;
cin >> num;
ChgTo10(num, x);
return 0;
}

void ChgTo10(char a[], int x)
{
int len = strlen(a);
int sum = 0;
for (int i = 0; i < len; i++)
{
if(a[i] > '9') // a[i]不是数字
{
if(a[i] > 'Z') // a[i]是小写字母
{
sum += (a[i] - 'a' + 10) * pow(x, len - 1 - i);
}
else // a[i]是大写字母
{
sum += (a[i] - 'A' + 10) * pow(x, len - 1 - i);
}
}
else
{
sum += (a[i] - '0') * pow(x, len - 1 - i);
}
}
cout << x << "进制的" << a << "对应的10进制数是:" << sum << endl;
}