Home > Back-end >  C insert has sort of student achievement file for processing. Insert a student's course grade 3
C insert has sort of student achievement file for processing. Insert a student's course grade 3

Time:09-21

Three procedures, 1, 2, no problem, the third can't read the saved data file
First, the first two fine
 
#include
#include
#include
# define N 5
Struct student
{
int id;
Float score [3].
Float ave.
Char name [10].
}
Stu [N].

Int main ()
{
Void the save (void);
int i;
The FILE * fp1;
for(i=0; i{
Printf (" please enter the first % d student student id, name: ", I + 1);
The scanf (" % d, % s ", & amp; Stu [I]. Id, stu [I] name);
Printf (" please input the students of three classes: \ n ", I + 1);
The scanf (" % f, % f, % f ", & amp; Stu [I] score [0], & amp; Stu [I] score [1], & amp; Stu [I] score [2]);
Stu [I] ave=(stu [I] score stu [0] + [I] score stu [1] + [I] score [2])/3;
}
Save ();
Fp1=fopen (" stud "and" rb ");
Printf (" student id, name, grade 1 and grade 2, grade 3, grade point average \ n ");
for(i=0; i{
Fread (& amp; Stu [I], sizeof (struct student), 1, fp1);
Printf (" % 5 d % s % 3.1 f % 3.1 f % 3.1 f % 3.2 f \ n ", stu [I] id, stu [I]. Name, stu [I] score [0], stu [I] score [1], stu [I] score [2], stu [I] ave);
}
The fclose (fp1);
return 0;
}
Void the save (void)
{
The FILE * fp.
int i;
If ((fp=fopen (" stud ", "wb"))==NULL)
{
Printf (" file can't open \ n ");
return;
}
for(i=0; iIf (fwrite (& amp; Stu [I], sizeof (struct student), 1, fp)!
=1){
Printf (" file is written to error \ n ");
return;
}
fclose(fp);
}


 
#include
#include
# define N 5
Struct student
{
int id;
Float score [3].
Float ave.
Char name [10].
}
Stu [N], stu0;

Int main ()
{
The FILE * fp1, * fp2;
Int I, j;
If ((fp1=fopen (" stud ", "rb"))==NULL)
{
Printf (" file can't open \ n ");
return 0;
}
for(i=0; i{
Fread (& amp; Stu [I], sizeof (struct student), 1, fp1);
}
The fclose (fp1);
for(i=0; i{
for(j=0; JIf (stu [j] ave{
Stu0=stu [j];
Stu [j]=stu [j + 1);
Stu [j + 1)=stu0;
}
}

If ((fp2=fopen (" stud_sort ", "rb"))==NULL)
{
Printf (" file can't open \ n ");
return 0;
}
Printf (" student id, name, grade 1 and grade 2, grade 3, grade point average \ n ");
for(i=0; i{
Fread (& amp; Stu [I], sizeof (struct student), 1, fp2);
Printf (" % 5 d % s % 3.1 f % 3.1 f % 3.1 f % 3.2 f \ n ", stu [I] id, stu [I]. Name, stu [I] score [0], stu [I] score [1], stu [I] score [2], stu [I] ave);
}
The fclose (fp2);
return 0;
}
Void the save (void)
{
The FILE * fp.
int i;
If ((fp=fopen (" stud_sort ", "wb"))==NULL)
{
Printf (" file can't open \ n ");
return ;
}
for(i=0; iIf (fwrite (& amp; Stu [I], sizeof (struct student), 1, fp)!
=1){
Printf (" file is written to error \ n ");
return ;
}
fclose(fp);
}

The third
 
#include
#include
# define N 5
Struct student
{
int id;
Float score [3].
Float ave.
Char name [10].
}
Stu [N], stu0;

Int main ()
{

The FILE * fp1, * fp2;
Int I, j;
If ((fp1=fopen (" stud_sort ", "rb"))==NULL)
{
Printf (" file can't open \ n ");
return 0;
}
for(i=0; i{
Fread (& amp; Stu [I], sizeof (struct student), 1, fp1);
}
The fclose (fp1);
Printf (" please enter the new student student id, name: ");
The scanf (" % d, % s ", & amp; Stu [I]. Id, stu [I] name);
Printf (" please enter the three classes grades: \ n ", I + 1);
The scanf (" % f, % f, % f ", & amp; Stu [5]. Score [0], & amp; Stu [5]. Score [1], & amp; Stu [5]. Score [2]);
Stu [5]. Ave=(stu [5]. The score stu [0] + [5]. The score [1] + stu [5]. The score [2])/3;
For (I=4; i> 0; I -)
Stu [5]=stu0;
If (stu [I] ave{

Stu [I]=stu [I + 1);
Stu [0]=stu [I];
}
If ((fp2=fopen (" stud_new ", "rb"))==NULL)
{
Printf (" file can't open \ n "); \ \ the problem here
return 0;
}
Printf (" student id, name, grade 1 and grade 2, grade 3, grade point average \ n ");
for(i=0; i{
Fread (& amp; Stu [I], sizeof (struct student), 1, fp2);
Printf (" % 5 d % s % 3.1 f % 3.1 f % 3.1 f % 3.2 f \ n ", stu [I] id, stu [I]. Name, stu [I] score [0], stu [I] score [1], stu [I] score [2], stu [I] ave);
}
The fclose (fp2);
return 0;
}
Void the save (void)
{
The FILE * fp.
int i;
If ((fp=fopen (" stud_new ", "wb"))==NULL)
{
Printf (" file can't open \ n ");
return ;
}
for(i=0; iIf (fwrite (& amp; Stu [I], sizeof (struct student), 1, fp)!
=1){
Printf (" file is written to error \ n ");
return ;
}
fclose(fp);
}


1, there are five students, each student has 3 course grades, students from the keyboard input data (including student id, name, 3 course grades),/calculate the average score, the original data and calculate the average score in the disk file "stud",
2, the first question "stud" student data in the file, according to the average rank processing, students will be sorted data in a new file "stud_sort",
3, the second question is sort of student achievement file is inserted into the handle and insert a student's course grade 3, program to calculate the new inserted into the student's grade point average, and then insert it according to the record high and low order, to create a new file after insert stud_new,

CodePudding user response:

aaaaas
  • Related