-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.atof.c
48 lines (40 loc) · 788 Bytes
/
4.atof.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
#include "stdio.h"
#define MAX_LEN 1000
double myatof(char s[]) {
int power = 0;
int flag = 0;
int sign = 1;
int i = 0;
while (s[i] == ' ' || s[i] == '\t') {
i++;
}
if (s[i] == '-') {
sign = -1;
i++;
} else if (s[i] == '+') {
sign = 1;
i++;
}
double r = 0.0;
while (s[i] != '\0' && s[i] >= '0' && s[i] <= '9') {
r = r * 10 + s[i] - '0';
i++;
}
if (s[i] == '.') {
i++;
}
while (s[i] != '\0' && s[i] >= '0' && s[i] <= '9') {
r = r * 10 + s[i] - '0';
i++;
power++;
}
while (power > 0) {
r = r / 10;
power--;
}
return r * sign;
}
int main() {
char s[] = "-01.2332.";
printf("%f\n", myatof(s));
}