谁能看出我的这个程序有何毛病?原意是把一个浮点型数转换成字符数组!
#include <stdio.h>#include <math.h>
void db_2_str(char *str,double num)
{
int n_num,i,j,k,l;
char ptemp;
//printf("%f\n",num);
n_num = (int)floor(num);
//printf("n_num = %d",n_num);
for(i=0;n_num!=0;i++)
{
str[i]=n_num%10+48;
n_num=n_num/10;
}
l=i-1;
for(k=0;k<l;k++,l--)
{
ptemp=str[k];
str[k]=str[l];
str[l]=ptemp;
}
str[i]='.';
num-=n_num;
for(j=i+1;num!=0;j++)
{
n_num=(int)floor(num*10);
str[j]=n_num%10+48;
num=num*10-n_num;
// printf("%d\n",n_num);
}
str[8]='\0';
}
int main()
{
float num;
char str[16];
scanf("%f",&num);
db_2_str(str,num);
printf("str is %s\n",str);
return 0;
}