标签:
long double stod(const char *ptr , bool *bOk = 0 , int *d = 0)
{
assert(ptr != 0);
const char *cursor = ptr;
switch(*cursor)
{
case ‘0‘:
{
++cursor;
switch(*cursor)
{
//! 此时被认为是8进制数
case ‘0‘:
case ‘1‘:
case ‘2‘:
case ‘3‘:
case ‘4‘:
case ‘5‘:
case ‘6‘:
case ‘7‘:
{
long long llRet = *cursor - ‘0‘;
++cursor;
while(*cursor)
{
if (‘0‘ <= *cursor && *cursor <= ‘7‘)
{
llRet *= 8LL;
llRet += *cursor - ‘0‘;
++cursor;
}
else
{
break;
}
}
if (bOk)
*bOk = true;
if (d)
*d = 8;
return static_cast<long double>(llRet);
}
//! 此时被认为是16进制数
case ‘x‘:
case ‘X‘:
{
++cursor;
long long llRet = 0LL;
while(*cursor)
{
if ((‘a‘ <= *cursor && *cursor <= ‘f‘))
{
llRet *= 16LL;
llRet += *cursor - ‘a‘ + 10LL;
++cursor;
}
else if (‘A‘ <= *cursor && *cursor <= ‘Z‘)
{
llRet *= 16LL;
llRet += *cursor - ‘A‘ + 10LL;
++cursor;
}
else if (‘0‘ <= *cursor && *cursor <= ‘9‘)
{
llRet *= 16LL;
llRet += *cursor - ‘0‘;
++cursor;
}
else
{
break;
}
}
if (bOk)
*bOk = true;
if (d)
*d = 16;
return static_cast<long double>(llRet);
}
//! 此时被认为是小数
case ‘.‘:
{
long double ret = 0.0;
long double div = 10.0L;
++cursor;
while(*cursor)
{
if (‘0‘ <= *cursor && *cursor <= ‘9‘)
{
ret += static_cast<long double>(*cursor - ‘0‘) / div;
div *= 10.0L;
++cursor;
}
else
break;
}
if (bOk)
*bOk = true;
if (d)
*d = 10;
return ret;
}
//! 此时被认为是十进制数
case ‘8‘:
case ‘9‘:
{
long long llRet = *cursor - ‘0‘;
++cursor;
while(*cursor)
{
if (‘0‘ <= *cursor && *cursor <= ‘9‘)
{
llRet *= 10LL;
llRet += *cursor - ‘0‘;
++cursor;
}
else if (*cursor == ‘.‘)
{
++cursor;
long double ret = 0.0L;
long double div = 10.0L;
while(*cursor)
{
if (‘0‘ <= *cursor && *cursor <= ‘9‘)
{
ret += static_cast<long double>(*cursor - ‘0‘) / div;
div *= 10.0L;
++cursor;
}
else
{
if (bOk)
*bOk = true;
if (d)
*d = 10;
return static_cast<long double>(ret + llRet);
}
}
}
else
break;
}
if (bOk)
*bOk = true;
if (d)
*d = 10;
return static_cast<long double>(llRet);
}
//! 转换失败
default:
{
if (bOk)
*bOk = false;
return 0.0;
}
}
}
break;
case ‘-‘:
{
++cursor;
long long llRet = 0LL;
while(*cursor)
{
if (‘0‘ <= *cursor && *cursor <= ‘9‘)
{
llRet *= 10LL;
llRet += *cursor - ‘0‘;
++cursor;
}
else if (*cursor == ‘.‘)
{
long double ret = 0.0;
long double div = 10.0L;
++cursor;
while(*cursor)
{
if (isdigit(*cursor))
{
ret += static_cast<long double>(*cursor - ‘0‘) / div;
div *= 10.0L;
++cursor;
}
else
{
if (bOk)
*bOk = true;
if (d)
*d = 10;
return static_cast<long double>(-llRet - ret);
}
}
}
else
break;
}
if (bOk)
*bOk = true;
if (d)
*d = 10;
return static_cast<long double>(-llRet);
}
case ‘.‘:
{
long double ret = 0.0L;
long double div = 10.0L;
++cursor;
while(*cursor)
{
if (isdigit(*cursor))
{
ret += static_cast<long double>(*cursor - ‘0‘) / div;
div *= 10.0L;
++cursor;
}
else
break;
}
if (bOk)
*bOk = true;
if (d)
*d = 10;
return ret;
}
case ‘1‘:
case ‘2‘:
case ‘3‘:
case ‘4‘:
case ‘5‘:
case ‘6‘:
case ‘7‘:
case ‘8‘:
case ‘9‘:
{
long long llRet = *cursor - ‘0‘;
++cursor;
while(*cursor)
{
if (isdigit(*cursor))
{
llRet *= 10LL;
llRet += *cursor - ‘0‘;
++cursor;
}
else if (*cursor == ‘.‘)
{
++cursor;
long double ret = 0.0L;
long double div = 10.0L;
while(*cursor)
{
if(isdigit(*cursor))
{
ret += static_cast<long double>(*cursor - ‘0‘) / div;
div *= 10.0L;
++cursor;
}
else
{
if (bOk)
*bOk = true;
if (d)
*d = 10;
return static_cast<long double>(llRet + ret);
}
}
}
else
break;
}
if (bOk)
*bOk = true;
if (d)
*d = 10;
return static_cast<long double>(llRet);
}
default:
{
if (bOk)
*bOk = false;
return 0.0;
}
}
}
标签:
原文地址:http://www.cnblogs.com/linannk/p/5615406.html