我正在尝试使用递归将十进制数转换为二进制数。但是我得到了错误的输出......我试图调试程序并被“pow()”卡住了。我插入了许多 printf 语句来查看变量的状态......这是我的程序......
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
int n,k;
int z;
long b,j;
long binary(int,int);
printf("Enter a Number : ");
scanf("%d",&n);
printf("Binary Equivalent is : ");
k=log2(n);
b=binary(n,k);
z=pow(10,k);
printf("\n\nb=%d ,z=%d",b,z);
}
long binary(int n,int c)
{
int a,np;
static long b=0;
if(n<=1){
b=(n%2)*(int)pow(10,c)+b;
printf("\n nmod2=%d\nb=%ld\nc=%d\nn=%d ",(n%2),b,c,n);
return b;
}
else{
a=n%2;
np=pow(10,c);
b=a*np+b;
printf("\n a=%d,np=%d \n nmod2=%d \n b=%ld \n c=%d \n n=%d ",a,np,(n%2),b,c,n);
binary(n/2,--c);
}
}
Output is:
Enter a Number : 5
Binary Equivalent is :
a=1,np=99
nmod2=1
b=99
c=2
n=5
a=0,np=10
nmod2=0
b=99
c=1
n=2
nmod2=1
b=100
c=0
n=1
b=100 ,z=100
为什么当 c=2 等于 99 时 binary() 中的“pow(10,c)”为什么不是 main() 中的 100?