summaryrefslogtreecommitdiffstats
path: root/4/2.c
diff options
context:
space:
mode:
Diffstat (limited to '4/2.c')
-rw-r--r--4/2.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/4/2.c b/4/2.c
index 0f842e8..2f94001 100644
--- a/4/2.c
+++ b/4/2.c
@@ -22,7 +22,7 @@ int main(int argc, char *argv[]) {
}
double atof(char s[]) {
- int i, sign;
+ int i, j, sign, exp, exp_sign;
double val, power;
for (i = 0; isspace(s[i]); i++)
@@ -44,5 +44,24 @@ double atof(char s[]) {
power *= 10.0;
}
+ if (s[i] == 'e' || s[i] == 'E')
+ i++;
+
+ exp_sign = s[i] == '-' ? -1 : 1;
+
+ for (i++, exp = 0; isdigit(s[i]); i++)
+ exp = 10 * exp + (s[i] - '0');
+
+ if (exp_sign < 0)
+ exp = exp_sign * exp;
+
+ if (exp < 0) {
+ for (j = exp; j < 0; j++)
+ power *= 10.0;
+ } else {
+ for (j = 0; j < exp; j++)
+ power /= 10.0;
+ }
+
return sign * val / power;
} \ No newline at end of file