aboutsummaryrefslogtreecommitdiff
path: root/challenge-113/abigail/c/ch-2.c
blob: fa2f79c27a228c2d96094ef88e2e090ffc28f928 (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
# include <stdlib.h>
# include <stdio.h>
# include <string.h>

/*
 * See ../README.md
 */

/*
 * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
 */

typedef long long number;

int main (void) {
    char *  line = NULL;
    size_t  len  = 0;

    while (getline (&line, &len, stdin) != -1) {
        size_t offset = 0;
        int skip;
        long long n, sum;

        /*
         * Read the numbers, calculate the sum.
         */
        sum = 0;
        while (sscanf (line + offset, "%lld%n", &n, &skip) == 1) {
            sum    += n;
            offset += skip;
        }

        /*
         * Read the numbers again, write output.
         */
        offset = 0;
        while (sscanf (line + offset, "%lld%n", &n, &skip) == 1) {
            if (offset) {
                printf (" ");
            }
            printf ("%lld", sum - n);
            offset += skip;
        }
        printf ("\n");
    }
    free (line);

    return (0);
}