#include <stdio.h> #include <stdlib.h>
struct Student {char name[10]; int num; float score_1; float score_2; float Score_3; float aver; }stu[6],temp;
void Save () {file*fp; int i; if ((Fp=fopen ("Stu.dat", "WB") ==null) {printf ("Cannot open file1\n"); Return } for (i=0;i<5;i++) if (fwrite (&stu[i],sizeof (struct student), 1,FP)!=1) printf ("File Write error\n"); Fclose (FP); }
void Save_1 () {file*fp; int i; if ((Fp=fopen ("Stu_sort.dat", "WB") ==null) {printf ("Cannot open file4\n"); Return } for (i=0;i<5;i++) if (fwrite (&stu[i],sizeof (struct student), 1,FP)!=1) printf ("File Write error\n"); Fclose (FP); }
void Save_2 () {file*fp; int i; if ((Fp=fopen ("Aver.dat", "WB") ==null) {printf ("Cannot open file7\n"); Return } for (i=0;i<5;i++) if (fwrite (&stu[i],sizeof (struct student), 1,FP)!=1) printf ("File Write error\n"); Fclose (FP); }
void print () {int i; FILE*FP; if ((Fp=fopen ("Stu.dat", "RB")) ==null) {printf ("Cannot open file2\n"); Exit (0); } for (i=0;i<5;i++) {fread (&stu[i],sizeof (struct student), 1,FP); printf ("%s\t%d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", Stu[i].name,stu[i].num,stu[i].score_1,stu[i].score_2,stu[i]. Score_3,stu[i].aver); } fclose (FP); }
void Print_1 () {int i; FILE*FP; if ((Fp=fopen ("Stu_sort.dat", "RB")) ==null) {printf ("Cannot open file5\n"); Exit (0); } printf ("The Aver is\n"); for (i=0;i<5;i++) {fread (&stu[i],sizeof (struct student), 1,FP); printf ("%s\t%d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", Stu[i].name,stu[i].num,stu[i].score_1,stu[i].score_2,stu[i]. Score_3,stu[i].aver); } fclose (FP); }
void Print_2 () {int i; FILE*FP; if ((Fp=fopen ("Aver.dat", "RB")) ==null) {printf ("Cannot open file8\n"); Exit (0); } for (i=0;i<5;i++) {fread (&stu[i],sizeof (struct student), 1,FP); printf ("%s\t%d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", Stu[i].name,stu[i].num,stu[i].score_1,stu[i].score_2,stu[i]. Score_3,stu[i].aver); } fclose (FP); }
Void aver () {int i,j,k,l,m; FILE*FP; if ((Fp=fopen ("Stu.dat", "RB")) ==null) { printf ("Cannot open file3\n"); exit (0);} for (i=0;i<5;i++) fread (&stu[i],sizeof (struct student), 1,FP); Fclose (FP); for (j=0;j<4;j++) { k=j; for (l=j+1;l<5;l++) if (stu[k].aver>stu[l].aver) k=l; if (k!=j) { temp=stu[j]; stu[j]=stu[k]; stu [K]=temp; }}}
void Insert () {int i,j,k; FILE*FP; if ((Fp=fopen ("Stu_sort.dat", "RB")) ==null) { printf ("Cannot open file6\n"); exit (0);} for (i=0;i<5;i++) Fread (&stu[i],sizeof (struct student), 1,FP); Fclose (FP); printf ("Please insert data of students num,score_1,score_2,score_3,name\n"); scanf ("%d,%f,%f,%f,%s", &stu[5].num,&stu[5].score_1,&stu[5].score_2,&stu[5].score_3,stu[5].name ); Stu[5].aver= (Stu[5].score_1+stu[5].score_2+stu[5].score_3)/3; for (j=0;j<5;j++) { if (stu[5].aver<stu[j].aver), break;} Temp=stu[5]; For (k=5;k> (j+1); k--) { stu[k]=stu[k-1];} stu[j+1]=temp; } void Main () { int i; printf ("Please enter data of students num,score_1,score_2,score_3,name\n"); for (i =0;i<5;i++) { scanf ("%d,%f,%f,%f,%s", &stu[i].num,&stu[i].score_1,&stu[i].score_ 2,&stu[i].score_3,stu[i].name); stu[i].aver= (Stu[i].score_1+stu[i].score_2+stu[i].score_3)/3; } save (); print (); Aver (); save_1 (); print_1 (); insert (); save_2 (); print_2 (); }
P354. Enter student information, create a file, implement a sort, insert