-
Notifications
You must be signed in to change notification settings - Fork 67
/
ex_4-2.c
88 lines (71 loc) · 1.56 KB
/
ex_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <ctype.h>
/*
Converts string of char into a double.
Supports scientific notation, signed exponents.
Warning: very large or small numbers exibit rounding errors.
*/
double atof(char s[])
{
long double val, power, expower;
int i, sign, exsign;
for(i=0; isspace(s[i]); i++) // skip spaces
;
sign = (s[i] == '-') ? -1 : 1; // mark sign for number
while(!isdigit(s[i])) // skip over signs and non numbers, dollar signs
i++;
val = 0.0;
while(isdigit(s[i])) // calc val of nums before decimal
{
val = 10.0 * val + (s[i] - '0');
i++;
}
if(s[i] == '.') // skip over decimals
i++;
power = 1.0;
while(isdigit(s[i])) // calc val of nums after decimal
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
i++;
}
val = sign * val / power;
// check for exponent
if(s[i] == 'e' || s[i] == 'E')
{
++i; // move past e or E
exsign = (s[i] == '-') ? -1 : 1; // mark sign for number
while(!isdigit(s[i])) // skip over sign(s)
i++;
expower = 0.0;
while(isdigit(s[i]))
{
expower = 10.0 * expower + (s[i] - '0');
++i;
}
}
/* cannot get pow() to work in math.h ... improvising :) */
if(exsign == 1)
{
while(expower > 0) { val = val * 10; --expower; }
} else {
while(expower > 0) { val = val / 10; --expower; }
}
return val;
}
int main()
{
char s[] = "-123.456e-7";
double ans = atof(s);
printf("%f\n", ans);
char r[] = "123.45e-6";
ans = atof(r);
printf("%f\n", ans);
char t[] = "123.45E6";
ans = atof(t);
printf("%f\n", ans);
char u[] = "-123.45e56";
ans = atof(u);
printf("%f\n", ans);
return 0;
}