c - 具有递归功能的 10 基的基数转换器 - C

标签 c function recursion process base-conversion

我尝试从 10 进制转换为任意数字进制。在我将两个基数相同的数字相乘后,该函数应该是递归的。

    double convert(int number,int base)
{
    int digit = 1;
    double sum=0;
    int i=0;
    int figure;
    double end;
    if(base==10)
        return number;

    else
    {
        figure = (digit % (digit * 10) - number % digit) / digit;
        end=pow(base,i);
        sum+=figure*end;
        ++i;
        digit *= 10;

        convert(figure,base);

    }
return sum;
}

但我对其他地方感到困惑,它不起作用。我该如何解决?有优惠吗? 谢谢..

最佳答案

例如

#include <stdio.h>

int convert(int number,int base){
    if(number == 0 || base==10)
        return number;

    return (number % base) + 10*convert(number / base, base);
}

int main () {
    int i;
    for(i=2;i<=10;++i)
        printf("%d is %d base(%d)\n", 100, convert(100, i), i);
    return 0;
}

关于c - 具有递归功能的 10 基的基数转换器 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375922/

相关文章:

c - 递归反转一个长度为2^n的整数数组,不修改原数组返回一个新数组

java - 如何转换打印到控制台的递归方法,使其返回字符串?

do-while 中的条件为 ||与&&

c - 如何修复 "I/O warning : failed to load external entity "file.xml“”? libxml2,macOS

c - 对 GLib 友好的 MD5 例程?

linux - C 程序将 $rbp+4 中的函数参数存储在内存中?我的检查失败

c++ - 使用递归在 C++ 中取消混搭字符串

C语言中的字符

javascript - 递归调用javascript函数

javascript - 如何制作函数参数