-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.c
68 lines (57 loc) · 1.48 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <ctype.h>
/**
* Converts a string to double
*
* @param char[] s The string to be converted
*
* @return double Value of input string
*/
double atof(char s[])
{
double val, power, result;
int i, j, sign, sci_sign, sci_value = 0;
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');
/* decimal part */
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
power *= 10;
}
result = sign * val / power;
/* Checking for scientific notation. */
if (s[i] == 'e')
i++;
sci_sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (sci_value = 0; isdigit(s[i]); i++)
sci_value = 10 * sci_value + (s[i] - '0');
while (sci_value > 0) {
if (sci_sign == -1) {
result /= 10;
} else {
result *= 10;
}
sci_value--;
}
return result;
}
/**
* A program to test string to double conversion.
*/
main ()
{
printf("'%s' => %f\n", "123.45e-6", atof("123.45e-6"));
printf("'%s' => %f\n", "123.45e6", atof("123.45e6"));
printf("'%s' => %f\n", "145e-6", atof("145e-6"));
printf("'%s' => %f\n", "145e6", atof("145e6"));
printf("'%s' => %f\n", "895.56", atof("895.56"));
}