Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <float.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- static void statistics(const char *fname, const char *suffix,
- const char *format) {
- char to_name[BUFSIZ];
- sprintf(to_name, "%s.%s", fname, suffix);
- FILE *from = fopen(fname, "r");
- FILE *to = fopen(to_name, "w");
- if (!from || !to) {
- exit(1);
- }
- int n = 0;
- double sum = 0., max = -DBL_MAX, min = DBL_MAX;
- char buf[BUFSIZ];
- while (fgets(buf, BUFSIZ, from)) {
- double e;
- if (sscanf(buf, format, &e) != 1) {
- exit(1);
- }
- ++n;
- sum += e;
- max = fmax(max, e);
- min = fmin(min, e);
- }
- fprintf(to, "%f\t%f\t%f\n", sum / n, max, min);
- fclose(from);
- fclose(to);
- }
- int main(void) {
- struct {
- char *suffix, *format;
- } course[] = {{"physics", "%*s%lf%*lf"}, {"chemistry", "%*s%*lf%lf"}};
- for (size_t i = 0; i < sizeof(course) / sizeof(course[0]); ++i) {
- statistics("result.dat", course[i].suffix, course[i].format);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement