ch4-atof (String to float)
Chapter_4 Exercise_4-1 | Exercise_4-2 |
atof.c K&R, p. 71 download
#include <stdio.h> // for printf(), scanf()
#include <ctype.h> // for isspace(), isdigit()
#define SIZE 200 // max no of digits before and after decimal point
double atof(char []); // string to double
int main()
{
char fpn[SIZE]; // floating-point number
// valid fpn: 0, +0, -0, 0.0, -0.0, +0., 0., .0, -.0, +.0
printf("Type a floating-point decimal number " // concatenate strings
"(at least 1 digit) [+-]?[0-9]*[.]?[0-9]*\n"); // automatically
scanf("%s", fpn);
printf("%f\n", atof(fpn));
return 0;
}
double atof(char s[]) // string to double
{
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++)
{} // skip whitespace
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
{i++;}
if ((s[i] == '\0') || (s[i] == '.' && s[i+1] == '\0'))
{
printf("\"%s\" is not a valid floating-point decimal number\n", s);
return 0.0; // should signal the error with an exception
}
for (val = 0.0; isdigit(s[i]); i++)
{ // ending '\0' of s[] is not a digit
val = 10.0 * val + (s[i] - '0');
}
if (s[i] == '.') // optional decimal point
{i++;} // skip decimal point, move to fractional part
for (power = 1.0; isdigit(s[i]); i++)
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0; // power = power * 10.0;
}
return sign * val / power;
}
/*
gcc atof.c -o atof
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
.
"." is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
+
"+" is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-
"-" is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
+.
"+." is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-.
"-." is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
+0.2
0.200000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-.5
-0.500000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
12.35
12.350000
/atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
0xa0 // we said decimal
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-2a1.23 // stops at non-decimal 'a'
-2.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-123.12a3 // stops at non-decimal 'a'
-123.120000
*/
Note: The regular_expression for a floating-point_number is quite complicated, so we prefer to write [+-]?[0-9]*[.]?[0-9]* and specify that the user should include at least one digit. In this expression, both the sign and the decimal point are optional, and each of them is followed by zero or more decimal digits (in the range 0 ... 9). As "+", "-", "+.", "-.", and "." are not valid floating-point numbers, we must specify that the user should include at least one decimal digit.
Chapter_4 Exercise_4-1 | BACK_TO_TOP | Exercise_4-2 |
Comments
Post a Comment