您的位置:首页 > 其它

十进制的转换

2016-03-25 18:55 375 查看
#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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: