基本功能上是实现了,错误处理很烂.
double atof(const char* str)
{
if (str==0)return 0;
double nRet = 0.0f;
bool bSign = true;
bool bFlag = false;
bool bPoint = false;
bool bNum = false;
double nCount = 1.0f;
int nLen = 0;
nLen = strlen(str);
if (nLen == 0) return 0;
for (int i=0; i<nLen; ++i)
{
if ( str[i] == ' ' && (!bFlag)) continue;
if ( str[i] >= '0' && str[i] <= '9')
{
if (bPoint) { nRet += ( str[i] - '0') / pow(10.0f, nCount); ++nCount; }
else nRet = nRet * 10 + ( str[i] - '0');
bNum = bFlag = true;
continue;
}
if ( (str[i] == '.') && (!bPoint) && bNum) { bPoint = true; continue; }
if ( str[i] == '+' && (!bFlag) ) { bFlag = bSign = true; continue; }
if ( str[i] == '-' && (!bFlag) ) { bFlag = true; bSign = false; continue; }
break;
}
if (!bSign) nRet = -nRet;
return nRet;
}