2.如果數字為0,則直接輸出。
3.注意英文轉換
#include<iostream>
#include<string>
using namespace std;
int tenMode(char array[], int num, int len);
void transF(int value, int num);
int main()
{
int B1, B2;
char num[30];
string X1;
getline(cin, X1);
int len = X1.length();
cin >> B1;
cin >> B2;
for (int i = 0; i < len; i++)
num[i] = X1[i];
int X1value = tenMode(num, B1, len);
if (X1value != -1)
{
if (X1value == 0)
cout << "0";
else
transF(X1value, B2);
cout << endl;
}
else
cout << "Input Error" << endl;
return 0;
}
int tenMode(char array[], int num, int len)
{
int value = 0, seat = 1;
for (int i = len - 1; i >= 0; i--)
{
int tmp = 0;
if (array[i] >= 'A' && array[i] <= 'F')
tmp = array[i] - 'A' + 10;
else
tmp = array[i] - '0';
if (tmp >= num)
return -1;
else
value += tmp * seat;
seat *= num;
}
return value;
}
void transF(int value, int num)
{
if (value == 0)
return;
else
{
transF((int)(value / num), num);
if (value % num >= 10)
cout << (char)((value % num) - 10 + 'A');
else
cout << value % num;
}
}
沒有留言:
張貼留言