抱歉,只会C++,希望下面的代码对你有所帮助:
// 将进制数转换为任意进制数(基介于2和16之间)
#include
#include
#include
using namespace std;
string dec2base( int, int );
int main()
{
int number, base;
char label = 'y';
string str;
do
{
cout << "Please enter a number and a base you want to change: ";
cin >> number >> base;
str = dec2base( number, base );
for( int i = 0; i < str.length(); i++ )
cout << str[ i ];
cout << "\n\nIf you want to try again, press y to continue, others to end: ";
cin >> label;
cout << endl;
} while( label == 'y' );
return 0;
}
string dec2base( int num, int base )
{
// digitChar为字库字符串,numStr为用于存储输出字符串
string digitChar = "0123456789ABCDEF", numStr = "";
// 用于临时存储输出的堆栈
stack< char > stk;
// 求余直至余数为零
do
{
// 将余数压入堆栈
stk.push( digitChar[ num % base ] );
num /= base;
} while( num != 0 );
// 将余数堆栈出栈并附到结果字符串中
while( !stk.empty() )
{
numStr += stk.top();
stk.pop();
}
return numStr;
}
6╱E╱Based on your subject ,
5╱A╱,
7╱S╱还有别的要求么,可以与我们联系,
1╱Y╱有机会可以帮你,
5╱S╱baidu╱hi,
6╱O╱如果您有与本主题类似的要求,也可以来找我们,
1╱F╱
5╱T╱ES-2010:\\49F0BE76AB52FD28C9DA051452A1649E