10.为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n()
函数接受两个在2~10范围内的参数,然后以第2个参数中指定的进制打印第
1个参数的数值。例如,to_base_n(129, 8)显示的结果为201,也就是129的
八进制数。在一个完整的程序中测试该函数。
#include<stdio.h>
void to_base_n(int u, int v);
int main(void)
{
to_base_n(129,8);
return 0;
}
void to_base_n(int u, int v)
{
int ch;
if (v < 2 || v>10)
printf("error! V must be an integer between 2 and 10, including 2 and 10\n");
else
{
ch = u % v;
if(u>=v)
to_base_n(u / v, v);
printf("%d ", ch);
}
}