Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct dt dt;
- struct dt {
- int student;
- char course[20];
- int score;
- dt *next;
- };
- static dt *head;
- static void load_file(void) {
- FILE *file = fopen("data.txt", "r");
- int s;
- char c[20];
- int sc;
- head = NULL;
- while (fscanf(file, "%d%s%d", &s, c, &sc) != EOF) {
- dt *new = calloc(sizeof(*new), 1);
- new->student = s;
- strcpy(new->course, c);
- new->score = sc;
- new->next = head;
- head = new;
- }
- }
- static void show_dt(void) {
- for (dt *p = head; p; p = p->next) {
- printf("%d %s %d\n", p->student, p->course, p->score);
- }
- }
- static void remove_one(dt *d) {
- if (head == d) {
- head = head->next;
- return;
- }
- for (dt *p = head->next, *prev = head; p; prev = p, p = p->next) {
- if (p == d) {
- prev->next = p->next;
- return;
- }
- }
- }
- static void sort_list(void) {
- if (!head) {
- return;
- }
- dt *min = head;
- for (dt *p = head->next; p; p = p->next) {
- if (p->student < min->student) {
- min = p;
- }
- }
- remove_one(min);
- sort_list();
- min->next = head;
- head = min;
- }
- static void print_average(void) {
- int count = 0, total = 0;
- for (dt *p = head; p; p = p->next) {
- ++count;
- total += p->score;
- if (p->next && p->next->student == p->student) {
- continue;
- }
- printf("%d %d %d\n", p->student, count, total / count);
- count = total = 0;
- }
- }
- int main(void) {
- load_file();
- sort_list();
- print_average();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement