我发现该程序可以进行基本转换。在下面的程序清单中,以下几行是做什么的?

printf("%c", base_digits[converted_number[index]]);


程序:

#include <stdio.h>

int main(void)
{
  char base_digits[16] =
   {'0', '1', '2', '3', '4','5', '6', '7','8', '9', 'A', 'B', 'C','D', 'E', 'F'};

  int converted_number[64];
  long int number_to_convert;
  int  base, index=0;

/* get the number and base */
 printf("Enter number and desired base: ");
 scanf("%ld %i", &number_to_convert, &base);

/* convert to the indicated base */
while (number_to_convert != 0)
{
 converted_number[index] = number_to_convert % base;
 number_to_convert = number_to_convert / base;
 ++index;
}

/* now print the result in reverse order */
--index;  /* back up to last entry in the array */
printf("\n\nConverted Number = ");
for(  ; index>=0; index--) /* go backward through array */
{
 printf("%c", base_digits[converted_number[index]]);
}
printf("\n");
return 0;
}

最佳答案

converted_number数组包含转换后的数字中每个数字的值。

base_digits数组包含与每个数字相对应的字符。因此,base_digits[converted_number[index]]为指定基数中的每个数字获取正确的字符。

关于c - c中的基本转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44956185/

10-11 23:03
查看更多