关于进制转换
十进制转换为二进制、八进制、十六进制。
我编写了一下请大家看看.
大家可以看到转换二进制和八进制时用了一种方法,转换十六进制时用了另一种.其实我愿
意用第一种可是不好实现.大家有什么好的方法讨论一下.谢谢!
#include<stdio.h>
#include<math.h>
main()
{
int s, n, m, b, y;
long e, x;
e=b=0;
x=1;
printf ("10\t\t2\t\t8\t\t16\n");
for (s=1; s<=20; s++) {
printf ("%d\t\t", s);
n=s;
while (n>=1) {
m=n%2;
n=n/2;
e+=m*x;
x*=10;
}
printf ("%ld\t\t", e);
x=1;
n=s;
while (n>=1) {
m=n%8;
n=n/8;
b*=m*x;
x*=10;
}
printf ("%d\t\t",b);
n=s;
m=3;
while (m>=0) {
y=pow (16, m);
if (n==0)
printf ("%d", n);
if (n>=y) {
x=n/y;
if (x<10&&x>=0)
printf ("%d', x);
if (x<16&&x>9)
printf ("%c", x+55);
n=n%y;
}
m--;
}
printf ("\n");
x=1;
n=s;
e=b=0;
}
return 0;
}