2 * Exercise 4-2. Extend atof to handle scientific notation of the form
6 * where a floating point number may be followed by e or E and an optionally
16 /* rudimentary calculator */
18 double pow(double x, double y);
23 int getlin(char line[], int max);
25 while (getlin(line, MAXLINE) > 0)
26 printf("\t%f\n", atof(line));
30 /* getlin: get line into s, return length */
31 int getlin(char s[], int lim) {
34 while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
42 /* atof: convert string s to double */
43 double atof(char s[]) {
45 int i, sign, expsign, exp;
47 for (i = 0; isspace(s[i]); i++) /* skip white space */
49 sign = (s[i] == '-') ? -1 : 1;
50 if (s[i] == '+' || s[i] == '-')
52 for (val = 0.0; isdigit(s[i]); i++)
53 val = 10.0 * val + (s[i] - '0');
56 for (power = 1.0; isdigit(s[i]); i++) {
57 val = 10.0 * val + (s[i] - '0');
60 if (s[i] == 'e' || s[i] == 'E') {
62 expsign = (s[i] == '-') ? -1 : 1;
63 if (s[i] == '+' || s[i] == '-')
65 for (exp = 0; isdigit(s[i]); i++)
66 exp = 10 * exp + (s[i] - '0');
71 return sign * val * pow(10,expsign*exp) / power;
75 double atof(char s[]);