SRC: реализация atof
От: aboo Россия  
Дата: 13.10.02 12:03
Оценка:
Спасибо orangy за реализацию atoi:
http://www.rsdn.ru/Forum/Message.aspx?mid=92149&only=1
Автор: orangy
Дата: 29.08.02


Код:
template<typename iterator>
double new_atof(iterator s)
{
    double sign(1), val(0);
    int d(0);
    if (s) while ((*s < '0' || *s > '9') && *(++s)); else return 0.0;
    sign*=(*s == '-' && ++s) ? -1.0 : ((*s == '+' && ++s),1.0);
    while ((*s >= '0' && *s <= '9') || (*s == '.' && d < 1))
    { 
        val += *s == '.' ? (++d, 0.0) : d > 0 ? ((++d, val*=10.0), *s-'0') : (val*=10.0, *s-'0');
        ++s;
    }
    double den(1);
    if(d > 0) while(--d) den*=10.0;
    return (val*sign)/den;
}


Пример использования:
    char str[] = "Price is 5964.599$. Buy NOW!";
    double g = new_atof<char*>(str);


Буду признателен за любую дельную критику...
Re: SRC: реализация atof
От: Atilla Россия  
Дата: 13.10.02 16:17
Оценка:
а как у Вас с такими тестами?
char str[] = "Price is -5964.599$. Buy NOW!";
char str[] = "Price is .599$. Buy NOW!";
char str[] = "Price is 5964.599E6$. Buy NOW!";
char str[] = "Price is 5964.599E-7$. Buy NOW!";
Кр-ть — с.т.
Re[2]: SRC: реализация atof
От: aboo Россия  
Дата: 13.10.02 17:46
Оценка:
Здравствуйте Atilla, Вы писали:

A>а как у Вас с такими тестами?

A>
A>char str[] = "Price is -5964.599$. Buy NOW!";
A>char str[] = "Price is .599$. Buy NOW!";
A>char str[] = "Price is 5964.599E6$. Buy NOW!";
A>char str[] = "Price is 5964.599E-7$. Buy NOW!";
A>


За первые два — спасибо, недоглядел...
Остальное — лень...

template<typename iterator>
double new_atof(iterator s)
{
 double sign(1), val(0);
 int d(0);
 if (s) while (((*s<'0' || *s>'9') && *s!='-' && *s!='+' && *s!='.') && *(++s)); 
 else return 0.0;
 sign*=(*s == '-' && ++s) ? -1.0 : ((*s == '+' && ++s),1.0);
 while ((*s >= '0' && *s <= '9') || (*s == '.' && d < 1))
 { 
   val += *s == '.' ? (++d, 0.0) : d > 0 ? ((++d, val*=10.0), *s-'0') :(val*=10.0, *s-'0');
   ++s;
 }
 double den(1);
 if(d > 0) while(--d) den*=10.0;
 return (val*sign)/den;
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.