递归算法实现数制转换

#include <stdio.h>
#include <string.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';
}

void main(void)
{
    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;
}

 

posted @ 2013-06-16 15:55  夜雨阑珊  阅读(523)  评论(0编辑  收藏  举报