我想问一下这串代码是什么意思啊?

#include #include #include main(){ float a,d,R=0,L=0,b; /* d is a decimal,b is the change result,binary*/ int i; int j; printf("please input the decimal to change:尀n"); scanf("%f",&d); a=d; /*d to a*/ for(i=0;a>=1;i++){ if(pow(2,i)>a){ a=a-pow(2,i-1); L=L+pow(10,i-1); i=0; } } printf("尀n%f尀n",a); printf("尀n%f尀n",L); for(j=-1;a>0.000000001;j--){ if(pow(2,j)<=a){ a=a-pow(2,j); R=R+pow(10,j); } } b=L+R; printf("尀n the changed result of %f is %f尀n",d,b); printf("尀n %f 尀n",a); printf("尀n %f 尀n",R); }
2026年09月23日 05:49
有1个网友回答
网友(1):

这段代码,其实是分别对小数点前后的数值进行二进制转换
先说第一段吧,他的逻辑是比方有个数63,
首先他小于2的6次
所以 63 = 32(32是2的6次, L在这里是 10的5次,也就是 100000 ) + 31(这个代表循环里的a),
再然后 31 = 16(100000 + 10000) + 15(a)
。。。
最后L也就是111111
同理你也可以去推断小数点后面的