C 使用atoi错误的atof

C 使用atoi错误的atof,c,while-loop,atoi,atof,C,While Loop,Atoi,Atof,这是一个atof函数,它将字符串转换为float,首先将字符串转换为整数,然后在保存小数点位置后将整数除以10s,得到实数 虽然条件为true,但程序不会进入第二个while循环,该循环负责忽略小数点 我正在ubuntu14.04上使用Geany 1.23.1 #include <stdio.h> #include <math.h> #include <string.h> double atof1(char num[]); //converts from

这是一个
atof
函数,它将字符串转换为
float
,首先将字符串转换为整数,然后在保存小数点位置后将整数除以10s,得到实数

虽然条件为true,但程序不会进入第二个while循环,该循环负责忽略小数点

我正在ubuntu14.04上使用Geany 1.23.1

#include <stdio.h>
#include <math.h>
#include <string.h>
double atof1(char num[]);   //converts from character to float
                            // by converting string  to integer first then divide by
                            //10 to the power of the count of fraction part
int main()
{
    char test[]="123.456";
    printf("%f\n",atof1(test));

    return 0;
}

double atof1(char num[])
{
    int dp=0;               //decimal point position
    int length=strlen(num);
    int i=0;
    while(dp==0)            //increment till you find decimal point
    {
        if(num[i]=='.')
        {
            dp=i;           //decimal point found
            break;
        }
        else i++;

    }
    printf("%d %d\n",i,length);
    while(length>i);            //delete the decimal point to get integer number
    {
        num[i]=num[i+1];            //deletes deicmal point
        i++;
        printf("%d",i);
    }
    int integer_number=atoi(num); //integer number of the string
    double final =integer_number/(pow(10,(length-dp-1)));//divide by 10s to get the float according to position of the '.'

    return final;


}
#包括
#包括
#包括
双atof1(字符数[])//从字符转换为浮点
//首先将字符串转换为整数,然后除以
//10乘以分数部分计数的幂
int main()
{
字符测试[]=“123.456”;
printf(“%f\n”,atof1(测试));
返回0;
}
双atof1(字符数[])
{
int dp=0;//小数点位置
int length=strlen(num);
int i=0;
while(dp==0)//递增直到找到小数点
{
如果(num[i]='.')
{
dp=i;//找到小数点
打破
}
否则i++;
}
printf(“%d%d\n”,i,长度);
while(length>i);//删除小数点以获得整数
{
num[i]=num[i+1];//删除标准点
i++;
printf(“%d”,i);
}
int integer_number=atoi(num);//字符串的整数
double final=整数_数/(pow(10,(length-dp-1));//除以10s,根据“.”的位置得到浮点值
返回最终结果;
}

您有一个额外的
while
语句置于无限循环中

while(length>i);            //delete the decimal point to get integer number
               ^^ This is the problem.

你的问题是什么?有什么问题?