#include <stdio.h>
#include <string.h>
#include <conio.h>
void convto(char *s, int n, int b)
{
char bit[] = {"0123456789ABCDEF"};
int len;
if (n == 0)
{
strcpy(s," ");
return;
}
convto(s, n/b, b);
len = strlen(s);
s[len] = bit[n%b];
s[len + 1] = '\0';
}
int main()
{
char s[80];
int i, base, old;
printf("请输入十进制数:");
scanf("%d", &old);
printf("请输入转换的进制:");
scanf("%d", &base);
convto(s, old, base);
printf("%s\n", s);
getch();
return 1;
}
此代码为数制转换 递归调用;
注意点为往S字符串数组回填转换后数字的时候,为递归调用,
所以其复制顺序为从下标小的往下标大的进行。