十进制的转换

版权声明:沉迷代码,难以自拔 https://blog.csdn.net/qq_33846054/article/details/50983335
#include <stdio.h>
int main (void)
{
const char baseDigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int convertedNumber[64];
long int numberToConvert;
int nextDigit, base, index = 0;
// get the number and the base
printf ("Number to be converted? ");
scanf ("%ld", &numberToConvert);
printf ("Base? ");
scanf ("%i", &base);
// convert to the indicated base
do {
convertedNumber[index] = numberToConvert % base;
++index;
numberToConvert = numberToConvert / base;
Character Arrays 111
}
while ( numberToConvert != 0 );
// display the results in reverse order
printf ("Converted number = ");
for (--index; index >= 0; --index ) {
nextDigit = convertedNumber[index];
printf ("%c", baseDigits[nextDigit]);
}
printf ("\n");
return 0;
}

Program 7.7 Output
Number to be converted? 10
Base? 2
Converted number = 1010
Program 7.7 Output (Rerun)
Number to be converted? 128362
Base? 16
Converted number = 1F56A

1.取余数,放进数组,但为逆序
2. 创建一个字符数组16位,放进16base所有数字对应的字母
3.逆序输出时,令nextdigit等于数组中的一个余数,对应字符数组的字符输出即可。
4.对base应该有个判断:若base为零则run time error;base=1则进入无限循环;base超过16则要扩展数组16.

猜你喜欢

转载自blog.csdn.net/qq_33846054/article/details/50983335