从字符串中检索出浮点型数的函数

来源:互联网 发布:淘宝卖家收到法院传票 编辑:程序博客网 时间:2024/05/17 23:15

最近写了一段代码,能从字符串检出无符号浮点型的数据……

如下:

double pow(int n)
{
    double ans=1.0;
    for(int i=1;i<=n;i++)
    {
        ans/=10.0;
    }
    return ans;
}

 

double find_double_number(char *str,int &pos,int endpos)
{
     int integer=0,fraction=0,pointpos=0,which=0;

     while( ( str[pos] == '.' || isdigit( str[pos] ) ) && ( pos <endpos ) )
     {
         if( str[pos] == '.' )  which=1;
         else
         {
             if( which ) { fraction=fraction*10+(int)(str[pos]-'0'); pointpos++; }
             else integer=integer*10+(int)(str[pos]-'0');
         }
         pos++;
     }
     return integer*1.0+fraction*1.0*pow(pointpos);
}

 

原创粉丝点击