递归Dec-to-Bin函数并用C语言将结果保存为字符串

递归Dec-to-Bin函数并用C语言将结果保存为字符串,c,string,recursion,binary,decimal,C,String,Recursion,Binary,Decimal,我有以下练习: 编写一个递归函数recDecToBin,该函数以2个参数作为参数 输入。第一个是需要转换为的自然数 二进制数系统。第二个是一个字符串,结果应该在该字符串中显示 得救 我写了下面的解决方案,但我不喜欢它。有没有更好的方法来处理内存和跟踪存储位置 #include <stdlib.h> #include <stdio.h> #include <string.h> int Global = 0; // this global variable h

我有以下练习:

编写一个递归函数recDecToBin,该函数以2个参数作为参数 输入。第一个是需要转换为的自然数 二进制数系统。第二个是一个字符串,结果应该在该字符串中显示 得救

我写了下面的解决方案,但我不喜欢它。有没有更好的方法来处理内存和跟踪存储位置

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int Global = 0;  // this global variable helps with case of input 0

void recDecToBin(int n, char* result){
    Global++;
    if(Global == 1 && n == 0){    // if input 0 , results will be 0
        result[0] = '0';    
    } else {
        if (n > 0){
            int dig;
            char *a;
            dig = n % 2;    // 0 or 1 to be written
            if (dig == 1){
                a = "1";
            } else {
                a = "0";
            }
            recDecToBin(n/2, result); // recursive call
            strncat(result,a,1);      // adding the results
        }
    }
}

int main(){
    int n;                // user input
    char result[30]="";   // storing string
    scanf("%d", &n);      // reading input 
    recDecToBin(n, result); // calling the function
    printf("%s", result);  // printing resutls
    return 0;           
}
#包括
#包括
#包括
int Global=0;//此全局变量有助于处理输入0的情况
void recDecToBin(整数n,字符*结果){
全球++;
如果(全局==1&&n==0){//如果输入0,结果将为0
结果[0]=“0”;
}否则{
如果(n>0){
内挖;
char*a;
dig=n%2;//要写入的0或1
如果(dig==1){
a=“1”;
}否则{
a=“0”;
}
recDecToBin(n/2,result);//递归调用
strncat(result,a,1);//添加结果
}
}
}
int main(){
int n;//用户输入
char result[30]=“”;//存储字符串
scanf(“%d”,&n);//读取输入
recDecToBin(n,result);//调用函数
printf(“%s”,结果);//打印结果
返回0;
}

您实际上不需要全局变量。此外,全局变量在参数数量上有点作弊,使函数只能使用一次。所以你有理由不喜欢那样

没有它,你的解决方案可以浓缩为

void recDecToBin(int n, char* result){
    if(n<2){
        strcpy(result, n?"1":"0");
    } else {
        recDecToBin(n/2, result); // recursive call
        strncat(result, n%2?"1":"0", 1);      // adding the results
    }
}
void recDecToBin(int n,char*result){
如果(n)
有没有更好的方法来处理内存和跟踪存储位置

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int Global = 0;  // this global variable helps with case of input 0

void recDecToBin(int n, char* result){
    Global++;
    if(Global == 1 && n == 0){    // if input 0 , results will be 0
        result[0] = '0';    
    } else {
        if (n > 0){
            int dig;
            char *a;
            dig = n % 2;    // 0 or 1 to be written
            if (dig == 1){
                a = "1";
            } else {
                a = "0";
            }
            recDecToBin(n/2, result); // recursive call
            strncat(result,a,1);      // adding the results
        }
    }
}

int main(){
    int n;                // user input
    char result[30]="";   // storing string
    scanf("%d", &n);      // reading input 
    recDecToBin(n, result); // calling the function
    printf("%s", result);  // printing resutls
    return 0;           
}
调用方分配是理想的,应该在可行的情况下使用,因为这将内存分配和实际算法这两个不相关的函数分开

在这种情况下,您知道输出的最大大小。它将是
8*sizeof(int)
characters+1个额外的空终止符。(学究般地
CHAR\u BIT*sizeof(int)
),因此您可以将函数编写为:

void recDecToBin(int n,char result[8*sizeof(int)+1])

不过,这只是自文档化代码,与
char*
相比没有技术上的区别

在实现方面,您应该能够摆脱全局变量和缓慢而危险的递归。从一些主流编译器中的代码反汇编来看,它们都无法内联您的代码。这是非常低效的,递归是主要原因。但重复的
strcat
调用也是如此

您应该使用一个简单易读的循环来编写所有这些内容:

const size_t size = 8*sizeof(int);
char bin_str [size+1];
unsigned int val = 123456;  

for(size_t i=0; i<size; i++)
{
  bin_str[size-i-1] = (val & 1) + '0';
  val >>= 1;
}  
bin_str[size] = '\0';
puts(bin_str);

这也消除了一些不必要的分支。

“但目标是练习递归”为什么!为什么目标是编写一个低效、不安全和不可读的程序?问问你的老师。这个练习是一系列练习(总共10个)中的一个,教给我们递归的概念,从著名的(计算阶乘)开始虽然这不是最好的实现,但它会逐渐增加复杂性。自然数最接近的表示形式是无符号整数。