A
arnuld
This is the example from section 4.2, page 71 of K&R2:
double atof( char s[] )
{
int i, sign;
double val, power;
for( i = 0; isspace( s ); ++i )
{
/* skipe the leading whitespace */
}
sign = ( (s == '-') ? -1 : 1 );
if( s == '+'|| s == '-' )
{
++i;
/* skip the leading sign, of any */
}
for( val = 0.0; isdigit( s ); ++i )
{
val = (10.0 * val) + (s - '0');
/* s - '0' (zero in quotes)
* always gives "int i" as output
*/
}
if( s == '.' )
{
++i;
/* skip the dot(.) of a floating point */
}
for( power = 1.0; isdigit( s ); ++i )
{
val = (10.0 * val) + (s - '0');
power *= 10.0;
}
return sign * val / power;
}
I can't really think of that kind of clever-tricks shown here. checking
for leading space and dot (.) are fine,they are very general things and
easily come to my mind but look at how cleverly the K&R created the
expressions like (val = 10.0 * val) and (power *= 10.0). these loops
using variables "val" and "power" are pretty much the work of people with
high IQs. After some work I can understand them but I can not create them
at very 1st place, I dont have that kind of thinking.
These tricks never came to my mind. Is this what we call programming ? if
yes, it is pretty much harder to come to my mind, may be never. I just do
not think this way.
double atof( char s[] )
{
int i, sign;
double val, power;
for( i = 0; isspace( s ); ++i )
{
/* skipe the leading whitespace */
}
sign = ( (s == '-') ? -1 : 1 );
if( s == '+'|| s == '-' )
{
++i;
/* skip the leading sign, of any */
}
for( val = 0.0; isdigit( s ); ++i )
{
val = (10.0 * val) + (s - '0');
/* s - '0' (zero in quotes)
* always gives "int i" as output
*/
}
if( s == '.' )
{
++i;
/* skip the dot(.) of a floating point */
}
for( power = 1.0; isdigit( s ); ++i )
{
val = (10.0 * val) + (s - '0');
power *= 10.0;
}
return sign * val / power;
}
I can't really think of that kind of clever-tricks shown here. checking
for leading space and dot (.) are fine,they are very general things and
easily come to my mind but look at how cleverly the K&R created the
expressions like (val = 10.0 * val) and (power *= 10.0). these loops
using variables "val" and "power" are pretty much the work of people with
high IQs. After some work I can understand them but I can not create them
at very 1st place, I dont have that kind of thinking.
These tricks never came to my mind. Is this what we call programming ? if
yes, it is pretty much harder to come to my mind, may be never. I just do
not think this way.