从字符串提取数字问题,帮我看一下代码

来源:百度知道 编辑:UC知道 时间:2024/05/23 02:19:45
bool IsDigit(char x)
{
//判断是否是数字
if(x>='0'&&x<='9')
return true;
else return false;
}

float f1(const int a[], int i, int j)
{
/*
计算用数组段[i:j]保存的数子响应的值,如:a[2]=3,a[4]=5,a[5]=1,则返回3*100+5*10+1=351;
*/
int k,m;
k=j-i;
float val=(float)0.0;
while(k>=0)
{
//先求10的k次方
int temp=1;
m=k;
while(m>0)
{
temp*=10;
m--;
}
val+=a[i]*temp;
i++;
k--;
}
return val;
}

float f2(const int a[],int i,int j)
{
/*
计算用数组段[i:j]保存的数字响应的值,如:a[2]=3,a[4]=5,a[5]=1,则返回0.345;
*/
float temp=(float)0.1; //c++中小数默认为double型的,所以编译时有" truncation from 'const double' to 'float' "
float val=(float)0.0;
while(i<j)
{
val+=a[i++]*temp;
temp*=(float)0.1;
}
return val;
}

1111111111111111111111111111111

验证的时候出了什么样的错?把结果贴出来看下
指针检查下,感觉有点乱,要当心越界问题
改了下,看下合不合你意
#include "iostream.h"

bool IsDigit(char x)
{
//判断是否是数字
if(x>='0'&&x<='9')
return true;
else return false;
}

float f1(const int a[], int i, int j)
{
/*
计算用数组段[i:j]保存的数子响应的值,如:a[2]=3,a[4]=5,a[5]=1,则返回3*100+5*10+1=351;
*/
int k,m;
k=j-i;
float val=(float)0.0;
while(k>=0)
{
//先求10的k次方
int temp=1;
m=k;
while(m>0)
{
temp*=10;
m--;
}
val+=a[i]*temp;
i++;
k--;
}
return val;
}

float f2(const int a[],int i,int j)
{
/*
计算用数组段[i:j]保存的数字响应的值,如:a[2]=3,a[4]=5,a[5]=1,则返回0.345;
*/
float temp=(float)0.1; //c++中小数默认为double型的,所以编译时有" truncation from 'const double' to 'f