#include #include #include "fields.h" #include "sllist.h" typedef struct { char *fname; char *lname; double score; } Person; /* Return the average score in a list of people */ double return_avg(Sllist d) { Sllist tmp; double n, t; Person *p; t = 0.0; n = 0; sll_traverse(tmp, d) { p = (Person *) jval_v(tmp->val); t += p->score; n++; } if (n == 0) return (double) 0; return t/n; } /* Print a list of students, plus the difference between each student's score and the average */ print_list(Sllist d, double avg) { Sllist tmp; double diff; Person *p; sll_traverse(tmp, d) { p = (Person *) jval_v(tmp->val); diff = p->score - avg; printf(" %-10s %-10s %5.2lf %6.2lf\n", p->fname, p->lname, p->score, diff); } } /* The main -- read in the students, calculate the averages and print them */ main() { IS is; Sllist grad, ugrad, gtmp, ugtmp; double uavg, gavg; Person *p; /* Create a list of graduate students. Gtmp will be a pointer to the last node on the list. To append to the list, insert a new node after gtmp, and then set gtmp to be the new end of the list. */ grad = new_sllist(); gtmp = grad; /* Create a list of undergraduates */ ugrad = new_sllist(); ugtmp = ugrad; /* Read in the students and put them into either grad or ugrad */ is = new_inputstruct(NULL); while (get_line(is) >= 0) { if (is->NF != 4) { fprintf(stderr, "Line %d: Bad format for grade file\n", is->line); exit(1); } p = (Person *) malloc(sizeof(Person)); p->fname = strdup(is->fields[0]); p->lname = strdup(is->fields[1]); if (sscanf(is->fields[3], "%lf", &p->score) != 1) { fprintf(stderr, "Line %d: Bad format for grade file\n", is->line); exit(1); } if (strcmp(is->fields[2], "U") == 0) { ugtmp = sll_insert_after(ugtmp, new_jval_v((void *)p)); } else if (strcmp(is->fields[2], "G") == 0) { gtmp = sll_insert_after(gtmp, new_jval_v((void *)p)); } else { fprintf(stderr, "Line %d: Bad format for grade file\n", is->line); exit(1); } } /* Print out the undergraduate average and students */ if (!sll_empty(ugrad)) { uavg = return_avg(ugrad); printf("Undergraduates: Average = %5.2lf\n", uavg); print_list(ugrad, uavg); printf("\n"); } /* Print out the graduate average and students */ if (!sll_empty(grad)) { gavg = return_avg(grad); printf("Graduates: Average = %5.2lf\n", gavg); print_list(grad, gavg); printf("\n"); } }