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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <stdio.h> #include <stdlib.h>
struct student { char num[11]; char name[11]; float c[3]; float j; };
void input_data(struct student stu[], int n) { int i = 0; float s = 0; for (; i < n; i++) { s = 0; scanf("%s%s%f%f%f", &stu[i].num, &stu[i].name, &stu[i].c[0], &stu[i].c[1], &stu[i].c[2]); s += stu[i].c[0] + stu[i].c[1] + stu[i].c[2]; stu[i].j = s / 3; } }
void sort(struct student stu[], int n) { struct student t; int i, j; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (stu[i].j > stu[j].j) { t = stu[j]; stu[j] = stu[i]; stu[i] = t; } } } }
void save(struct student stu[], int n, char filename[]) { int i = 0; FILE *fp; fp = fopen(filename, "wb"); for (i = 0; i < n; i++) fwrite(&stu[i], sizeof(struct student), 1, fp); fclose(fp); }
int read(struct student stu[], char filename[]) { int n = 0, i = 0; FILE *fp = fopen(filename, "rb"); while (fread(&stu[n], sizeof(struct student), 1, fp) == 1) { n++; } fclose(fp); return n ; }
int insert(struct student stu[], int n, struct student x) { int i = n - 1; x.j = (x.c[0] + x.c[1] + x.c[2]) / 3;
while (i >= 0 && x.j < stu[i].j) { stu[i + 1] = stu[i]; i--; }
stu[i + 1] = x; return n + 1; }
void print_data(struct student stu[], int n) { int i = 0; for (; i < n; i++) { printf("%s %s %.2f %.2f %.2f %.2f\n", stu[i].num, stu[i].name, stu[i].c[0], stu[i].c[1], stu[i].c[2], stu[i].j); }
}
int main() { int n; scanf("%d", &n); struct student stu[100]; input_data(stu, n); sort(stu, n); save(stu, n, "d:\\data\\sy8-3-in.txt"); print_data(stu, n); struct student x; scanf("%s%s%f%f%f", &x.num, &x.name, &x.c[0], &x.c[1], &x.c[2]); n = insert(stu, n, x); save(stu, n, "d:\\data\\sy8-3-out.txt"); n = read(stu, "d:\\data\\sy8-3-out.txt"); print_data(stu, n); printf("%d", n); return 0; }
|