#include #include /* * A very simple program to read in three columns X, Y, Z * and print out them back out again * */ int main (void) { char line [256]; /* used to buffer a whole line of input */ double in_x, in_y, in_e; /* input variables */ double out_x, out_y, out_e; /* output variables */ double prev_x = 0, prev_y = 0, prev_e = 0; /* transitory variables */ double integral = 0; /* used to calculate the running int(0, sin(x)) */ int counter = 0, rc; /* used for debugging bad data input */ while (fgets (line, 256, stdin)) { /* read a whole line into buffer */ counter++; /* increment line counter */ rc = sscanf (line, "%lf %lf %lf", &in_x, &in_y, &in_e); /* attempt to parse variables */ if (rc != 3) { /* if didn't find all three protest and exit */ fprintf (stderr, "Less than three floats on line %i:\n\"%s\"\n", counter, line); return 1; } out_x = in_x; /* calculate */ out_y = in_y; out_e = in_e; if(counter > 1) { double a, b; /* temp var */ /* calculate the derivative */ out_y = (in_x - prev_x) * (in_y + prev_y) / 2; integral += out_y; a = (in_x - prev_x) * in_e / 2; b = (-prev_x + in_x) * prev_e / 2; out_e = sqrt(a * a + b * b); printf ("%g\t%g\t%g\n", out_x, integral, out_e); /* calculate and output result */ } prev_x = in_x; /* save the previous triplet */ prev_y = in_y; prev_e = in_e; } fprintf (stderr, "Processed %i lines\n", counter); /* babble some statistics */ return 0; }