-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_2.c
54 lines (45 loc) · 1.07 KB
/
4_2.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
53
54
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void atof(char s[]);
void atof(char s[]) {
double val, power;
int i, exp, sign, exponential_sign = 1;
for (i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-' ? -1 : 1);
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = val * 10.0 + (s[i] - '0');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
val = sign*val/power;
if (s[i] == 'e' || s[i] == 'E')
i++;
exponential_sign = (s[i] == '-' ? -1 : 1);
if (s[i] == '+' || s[i] == '-')
i++;
while(isdigit(s[i])) {
exp = 10.0*exp + (s[i] - '0');
i++;
}
printf("%lf", val/power);
/* printf("%lf", power); */
/* if (exponential_sign == -1) */
/* printf("%lf, %lf, %lf", val, power, (sign*val/power)); */
/* else */
/* printf("%lf, %lf, %lf", val, power, (sign*val*power)); */
// check for e
// check for sign after e
// if sign set flag
//
}
main() {
char s[1000] = " 45.34e-1 Hello, world!";
atof(s);
}