-
Notifications
You must be signed in to change notification settings - Fork 0
/
atof2.c
35 lines (32 loc) · 991 Bytes
/
atof2.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
#include "atof.h"
#include <ctype.h>
#include <math.h>
/* atof:将字符串s转换为双精度浮点数,支持科学表示法 */
double atof(char s[]) {
double val, power;
int i, val_sign, exponent_sign, exponent;
for (i = 0; isspace(s[i]); ++i) /* 跳过空白符 */
;
/* 整数部分 */
val_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.0;
}
/* 指数部分 */
if (s[i] == 'e' || s[i] == 'E')
++i;
exponent_sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
++i;
for (exponent = 0; isdigit(s[i]); ++i)
exponent = 10 * exponent + (s[i] - '0');
return val_sign * val / power * pow(10, exponent_sign * exponent);
}