blob: 86bd651a02218e2044c2e86bd010836fd8fc3963 (
plain)
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
|
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define MAXLINE 128
int
get_line(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n') {
s[i++] = c;
}
if (c == '\n') {
s[i++] = c;
}
s[i] = '\0';
return i;
}
/* convert string to double-precision floating point */
double
atof(char s[])
{
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); ++i);
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;
}
if (s[i] == 'e' || s[i] == 'E') {
return sign * val / power * pow(10, s[++i] - '0');
}
return sign * val / power;
}
int
main()
{
double sum;
char line[MAXLINE];
sum = 0;
while (get_line(line, MAXLINE) > 0) {
printf("\t%g\n", sum += atof(line));
}
return 0;
}
|