闲来无事,用c++类写了个进制间的转换。
//功 能:1、十进制转换为任意进制 2、任意进制转换为十进制
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <windows.h>
using namespace std;
class SysConvert
{
public:
SysConvert(){}
SysConvert(char *_sysnum,int _sys);
SysConvert(int _dnum,int _sys);
void Convert(); //十进制转换为任意进制
void Convertd(); //任意进制转换为十进制
char* GetRes();
int GetDnum();
private:
int dnum;
int sys;
char res[256];
char sysnum[256];
};
SysConvert::SysConvert(int _dnum,int _sys)
{
if(_sys>26)
{
MessageBox(NULL,TEXT("The system is too large!"),TEXT("ERROR"),MB_ICONERROR);
exit(0);
}
dnum=_dnum;
sys=_sys;
}
SysConvert::SysConvert(char *_sysnum,int _sys)
{
strcpy(sysnum,_sysnum);
sys=_sys;
}
void SysConvert::Convert()
{
char _res[256];
int num(dnum),i(0);
while(num)
{
int temp=num%sys;
if(temp>9)
{
_res[i++]='A'+(temp-10);
}
else _res[i++]=char(temp+'0');
num/=sys;
}
int j;
for(j=0;j<i;j++)
res[j]=_res[i-j-1];
res[j]=0;
}
char* SysConvert::GetRes()
{
return res;
}
int SysConvert::GetDnum()
{
return dnum;
}
void SysConvert::Convertd()
{
int len=strlen(sysnum),sum(0);
for(int i=len-1;i>=0;i--)
{
int temp;
if(sysnum[i]>='A')
{
temp=10+(sysnum[i]-'A');
}
else temp=sysnum[i]-'0';
sum+=temp*pow(sys*1.0,len-i-1);
}
dnum=sum;
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
int x1,y1;
cin>>x1>>y1;
SysConvert sysconvert1(x1,y1);
sysconvert1.Convert();
cout<<sysconvert1.GetRes()<<endl;
char x[234];
int y;
scanf("%s%d",x,&y);
SysConvert sysconvert(x,y);
sysconvert.Convertd();
cout<<sysconvert.GetDnum()<<endl;
}
return 0;
}