-
Notifications
You must be signed in to change notification settings - Fork 0
/
402.c
52 lines (43 loc) · 1.14 KB
/
402.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <ctype.h>
/* Exercise 4-2. Extend atof to handle scientific notation of the form
123.45e-6
where a floating-point number may be followed by e or E and an optionally signed exponent. */
double _atof(char s[])
{
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++) /* skip white space */
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
power *= 10;
}
return sign * val / power;
}
double atof(char s[]){
double val;
int i, sign, exponent;
val = _atof(s);
for (i = 0; s[i] != '\0' && s[i] != 'e' && s[i] != 'E'; i++){
;
}
if (s[i] == '\0'){
return val;
} else{
i++;
}
exponent = (int) _atof(&s[i]);
sign = exponent<0 ? -1 : 1;
exponent = exponent<0 ? exponent/-1 : exponent;
for(i=0; i<exponent;i++){
val = (sign == -1) ? val/10 : val*10;
}
return val;
}