This article is intended for practical projects in the basic series of data Structures (11): Files.
"Project 1" Action file
There are a number of students whose results are as follows, save this data in the St array:
学号 姓名 年龄 性别 语文 数学 英语 1 陈华 20 男 78 90 84 5 张明 21 男 78 68 92 8 王英 20 女 86 81 86 3 刘丽 21 女 78 92 88 2 许可 20 男 80 83 78 4 陈军 20 男 78 88 82 7 马胜 21 男 56 67 75
Based on these data, the program implements the following functions:
(1) Write the St array student record to the Stud.dat file
(2) Read the Stud.dat file into the St array
(3) display of student records in the St array
(4) Average student's score and put in the ST1 array
(5) Sort the st1 array in descending order of average
(6) write the ST1 array to the Stud1.dat file
(7) Read the Stud1.dat file into the ST1 array
(8) Display student records in the ST1 array
[Reference Solution]
#include <stdio.h>#include <string.h>#define N //maximum number of students typedef struct{intNo//Study No. Charname[Ten];//Name intAge//Age Charsex[3];//Gender intDEG1,DEG2,DEG3;//Course 1-Course 3 results} Studtype;typedef struct{intNo//Study No. Charname[Ten];//Name intAge//Age Charsex[2];//Gender intDEG1,DEG2,DEG3;//Course 1-Course 3 results DoubleAvg//Average score} StudType1;voidWriteFile (Studtype st[],intN//write the student records in the St array to the Stud.dat file{intI FILE *FP;if((Fp=fopen ("Stud.dat","WB")) ==null) {printf("\ t hint: cannot create Stud.dat file \ n");return; } for(i=0; i<n; i++) fwrite (&st[i],1,sizeof(Studtype), FP); Fclose (FP);printf("\ t hint: file Stud.dat created \ n");}voidWriteFile1 (StudType1 st1[],intN//writes student records in the ST1 array to the Stud1.dat file{intI FILE *FP;if((Fp=fopen ("Stud1.dat","WB")) ==null) {printf("\ t hint: cannot create Stud1.dat file \ n");return; } for(i=0; i<n; i++) fwrite (&st1[i],1,sizeof(Studtype), FP); Fclose (FP);printf("\ t hint: file Stud1.dat created \ n");}voidReadFile (Studtype st[],int&n)//Read the n student records in the Stud.dat file into the St array{FILE *FP;if((Fp=fopen ("Stud.dat","RB")) ==null) {printf("\ t prompt: Cannot open stud.dat file \ n");return; } n=0; while(Fread (&st[n),sizeof(Studtype),1, fp) = =1) n++;printf("\ t hint: file Stud.dat read \ n");}voidReadFile1 (StudType1 st1[],int&n)//Read the n student records in the Stud1.dat file into the st1 array{FILE *FP;if((Fp=fopen ("Stud1.dat","RB")) ==null) {printf("\ t prompt: Cannot open stud1.dat file \ n");return; } n=0; while(Fread (&st1[n),sizeof(Studtype),1, fp) = =1) n++;printf("\ t hint: file Stud1.dat read \ n");}voidDisplay (Studtype st[],intN//Show student Records{intIprintf("----Student grades----\ n");printf("Study number name age gender Chinese math english \ n"); for(i=0; i<n; i++)printf("%5d%10s%6d%5s%5d%5d%5d\n", ST[I].NO,ST[I].NAME,ST[I].AGE,ST[I].SEX,ST[I].DEG1,ST[I].DEG2,ST[I].DEG3);printf("\ n");}voidDisplay1 (StudType1 st1[],intN//Show student records after average score{intIprintf("----The student score table after sorting----\ n");printf("Study number name age gender Chinese math English average score \ n"); for(i=0; i<n; i++)printf("%5d%10s%6d%5s%5d%5d%5d%6.1f\n", ST1[I].NO,ST1[I].NAME,ST1[I].AGE,ST1[I].SEX,ST1[I].DEG1,ST1[I].DEG2,ST1[I].DEG3,ST1[I].AVG);printf("\ n");}voidAverage (Studtype st[],studtype1 st1[],intN//Calculate average score{intI for(i=0; i<n; i++) {st1[i].no=st[i].no;strcpy(St1[i].name,st[i].name); St1[i].age=st[i].age;strcpy(St1[i].sex,st[i].sex); ST1[I].DEG1=ST[I].DEG1; ST1[I].DEG2=ST[I].DEG2; ST1[I].DEG3=ST[I].DEG3; st1[i].avg= (ST1[I].DEG1+ST1[I].DEG2+ST1[I].DEG3)/3.0; }}voidSort (StudType1 st1[],intN//Descending sort by average{intI,j; StudType1 temp; for(i=1; i<n; i++)//Direct Insert Sort{Temp=st1[i]; for(j=i-1; j>=0&& temp.avg>st1[j].avg; j--) st1[j+1]=ST1[J]; st1[j+1]=temp; }}intMain () {intn=8; Studtype st[]= {{1,"Chen Hua", -,"Male", +, -, -}, {5,"Zhang Ming", +,"Male", +, the, the}, {8,"Wang Ying", -,"female", the,Bayi, the}, {3,"Liu Li", +,"female", +, the, the}, {2,"License", -,"Male", the, the, +}, {4,"Chen June", -,"Male", +, the, the}, {7,"Maxim Trader", +,"Male", About, the, the}, {6,"Zeng Qiang", -,"Male", +, the, the} }; StudType1 St1[n];printf("The procedure is as follows: \ n");printf("(1) write the St array secondary student record to the Stud.dat file \ n"); WriteFile (St,n);printf("(2) Read the Stud.dat file into the St array \ n"); ReadFile (St,n);printf("(3) display the student record in the St array \ n"); Display (St,n);printf("(4) The average student's score is placed in the ST1 array \ n"); Average (St,st1,n);printf("(5) sort the st1 array in descending order of average" \ n "); Sort (St1,n);printf("(6) write St1 array secondary student record to stud1.dat file \ n"); WriteFile1 (St1,n);printf("(7) Read the Stud1.dat file into the st1 array \ n"); ReadFile1 (St1,n);printf("(8) display the student record in the ST1 array \ n"); Display1 (St1,n);return 0;}
Data structure Practice--operation file