高手看过来,帮该个程序,
#include "stdio.h"int main(void)
{
char binaryNum[100]="101011.01011110011101";
char *p = binaryNum;
float decimalNum = 0;
int n = 0, dotsNum = 0, i = 0, j = 0;
for ( ; *p != '\0'; p++, n++) {
if (! (*p == '0' || *p == '1' || *p == '.')) {
printf("bad input!\n");
return 1;
}
if (*p == '.') {
dotsNum++;
if (dotsNum == 1)
i = n;
else {
printf("bad binary number!\n");
return 1;
}
}
}
if (dotsNum)
j = n -1 - i;
else
i = n;
p = binaryNum;
for ( ; i > 0; p++, i--)
if (*p == '1')
decimalNum += pow(2, i -1);
for (p++, i = 1; i <= j; p++, i++)
if (*p == '1')
decimalNum += pow(2, -i);
printf("The converted decimal number is:\n");
printf("%f\n", decimalNum);
return 0;
}
float pow (int m,int n)
{float sum=1;
int i;
if (n>0)
{for (i=1;i<=n;i++)
sum=m*sum;
}
else
{if(n==0)
sum=1;
else{
if(n<0)
{for (i=1;i<=(-n);i++)
sum=sum*(1/m);
}
}
}
return(sum);
}该程序是计算二进制浮点数转化成十进制,不用<MATH.H>涵数,二采用调用(幂),理论结果为方43.23645,可是该程序运行后是43.00000,小数部分没了,该怎样该啊,急急啊,急的不行了.