可以实现任意进制到任意进制的转换,感兴趣的同学可以做成图形化界面的。
#define _CRT_SECURE_NO_WARNINGS
#include
#include
long long cf(long long, int);
int main(void)
{
long long n, m;
char zf[1000] = { 0 };
printf("请入您要输入的数的进制:");
scanf("%lld", &n);
printf("请输入您要输入的数,十进制以上字母请大写:");
getchar();
gets(zf);
printf("请入您要转换的进制:");
scanf("%lld", &m);
long long zh = 0;
int zf_len = strlen(zf);
int temp = zf_len - 1;
int i;
for (i = 0; i < zf_len; i++)//13579B
{
if ((zf[i] - '0') >= 0 && (zf[i] - '0') <= 9)
{
zh += ((long long)(zf[i] - '0')) * cf(n, temp--);
}
if (zf[i] >= 'A' && zf[i] <= 'F')
{
//printf("%d\n",zf[i]-'A'+10);
zh += ((long long)(zf[i] - 'A' + 10)) * cf(n, temp--);
}
//printf("%d\n",zh);
}
//printf("%d\n",zh);
int sc_len = 0;
char scsz[1000] = { 0 };
while (zh != 0)
{
long long tem = (zh % m);
// printf("%lld \n",tem);
if (tem < 10)
{
scsz[sc_len] = tem + '0';
}
else
{
switch (tem)
{
case 10:scsz[sc_len] = 'A'; break;
case 11:scsz[sc_len] = 'B'; break;
case 12:scsz[sc_len] = 'C'; break;
case 13:scsz[sc_len] = 'D'; break;
case 14:scsz[sc_len] = 'E'; break;
case 15:scsz[sc_len] = 'F'; break;
}
}
sc_len++;
zh /= m;
}
int k;
for (k = sc_len - 1; k >= 0; k--)
{
printf("%c", scsz[k]);
}
return 0;
}
long long cf(long long n, int cs)
{
if (cs == 0)
{
return 1;
}
else
{
long long i = 1;
int j;
for (j = 1; j <= cs; j++)
{
i *= n;
}
return i;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)