以下这个是我自己编写的
//---------------------------------------------------------------------------
//字符串转浮点
double Atof(const char* buffer)
{
if( Isdigit(buffer) == false ) return 0;
std::string strMoney(buffer);
double fsum = 0.0;
if( buffer[0] == '-')
{
for(std::size_t index = 1; index < strMoney.length(); ++index)
{
if( strMoney[index] != '.'){
fsum = fsum * 10 + (strMoney[index]-48);
}
}
fsum *= -1;
}
else{
for(std::size_t index = 0; index < strMoney.length(); ++index)
{
if( strMoney[index] != '.'){
fsum = fsum * 10 + (strMoney[index]-48);
}
}
}
int nPos = 0;
if( (nPos = strMoney.find_first_of('.')) != std::string::npos)
{
int nCount = strMoney.length()- nPos;
for( int index = 0; index < nCount-1; ++index)
{
fsum *= 0.1;
}
}
return fsum;
}
//-------------------------------------------------------------------------------
// 判断是否数字
bool Isdigit(const char* str)
{
std::string strNum(str);
if( strNum == "" || strNum == " " || strNum[0]== '.'){
return false;
}
int count = 0;
string::iterator it = strNum.begin();
for(; it!= strNum.end(); ++it)
{
if( (*it <'0'&& *it != '.' && *it != '-') || *it > '9'){
return false;
}
else if( *it == '0' && it == strNum.begin() )
{
if(*(++it) != '.'){
return false;
}
--it;
}
else if( *it == '.' )
{
count++;
}
else if( *it == '-' && it != strNum.begin() )
{
return false;
}
}
if( count > 1){
return false;
}
return true;
}