-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatof.c
47 lines (34 loc) · 869 Bytes
/
atof.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
//* gcc -o output atof.c && output
#include <ctype.h>
#include <stdio.h>
#define MAXLINE 100
double atof(char s[]);
int main(void) {
// rudimentary calculator:
double sum, atof(char s[]);
char line[MAXLINE];
int getline(char line[], int max);
sum = 0;
while (getline(line, MAXLINE) > 0) {
printf("\t%g\n", sum += atof(line));
}
return 0;
}
double atof(char s[]) {
double val, power;
int i, sign;
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');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
return sign * val / power;
}