C language: There are 5 students, each student has grades of 3 courses, input student data from the keyboard, calculate the average grade

There are 5 students, each student has 3 course grades, input student data (including student number, name, grades of three courses) from the keyboard, calculate the average grade, and store the original data and the calculated average grade on the disk In file "stud"

  The idea for the average grade is: take the average grade as an item of the structure

  When entering the grades of 3 courses to obtain the average grade, it should be noted that in the C language, the grades of the three courses should be entered separately, and cannot be entered together with the student number and name. If you enter it like this, the program will not report an error, but it will never run.

struct student//Define the structure
{
	int num;
	char name[10];
	int score[3];
	float ave;
 }stu[5];
int main(int argc, char *argv[]) {
	int i,j,sum,t;
	FILE *fp;
	struct student p,temp,re[6];
	for(i=0;i<5;i++)//Structure assignment
	{
		printf("Enter the %d record\n",i+1);
		scanf("%d,%s",&stu[i].num,stu[i].name);
		printf("Please enter the grades of three courses\n");
		scanf("%d,%d,%d",&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
		sum=0;
		sum=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
		stu[i].ave=sum/3.0;
	}
	if((fp=fopen("stud.txt","w"))==NULL)//Write the file
	{
		printf("cannot open stud.txt\n");
		exit(0);
	}
	for(i=0;i<5;i++)
	fwrite(&stu[i],sizeof(struct student),1,fp);
	fclose(fp);
	if((fp=fopen("stud.txt","r"))==NULL)//Read the file
	{
		printf("cannot open stud.txt\n");
		exit(0);
	}
	for(i=0;i<5;i++)
	{
		fread(&stu[i],sizeof(struct student),1,fp);	
        printf("%d,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
	}
    fclose(fp);
    printf("\n");}

Sort by average

for(i=0;i<4;i++)//File sorting
    for(j=i+1;j<5;j++)
    if(stu[i].ave>stu[j].ave)
   	{
    	temp = stu [i];
    	stu [i] = stu [j];
    	stu[j]=temp;
	}		
	if((fp=fopen("stu_sort.txt","w"))==NULL)
	{
		printf("cannot open stud.txt\n");
		exit(0);
	}
	for(i=0;i<5;i++)
 	fwrite(&stu[i],sizeof(struct student),1,fp);	
    fclose(fp);	
	if((fp=fopen("stu_sort.txt","r"))==NULL)
	{
		printf("cannot open stud.txt\n");
		exit(0);
	}
		for(i=0;i<5;i++)
	{
		fread(&stu[i],sizeof(struct student),1,fp);	
        printf("%d,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
	}	
    fclose(fp);

Insert new student data

printf("Please enter insert student data:\n");
	scanf("%d,%s",&p.num,p.name);
	printf("Please enter the grades of three courses\n");
	scanf("%d,%d,%d",&p.score[0],&p.score[1],&p.score[2]);
	sum=0;
	sum=p.score[0]+p.score[1]+p.score[2];
	p.ave=sum/3.0;
	
	if((fp=fopen("stu_sort.txt","r"))==NULL)
	{
		printf("cannot open stud.txt\n");
		exit(0);
	}
	for(i=0;i<5;i++)
	{
		fread(&stu[i],sizeof(struct student),1,fp);	
		 if(stu[i].ave>p.ave)
		{
			t=i;break;
		}
		else
		t=5;
	}
	fclose(fp);
	fp=fopen("sdent.txt","a");
	if(t==5)
	{
		for(i=0;i<5;i++)
	    fwrite(&stu[i],sizeof(struct student),1,fp);
     	fwrite(&p,sizeof(struct student),1,fp);
	}
	else if(t==0)
	{
		fwrite(&p,sizeof(struct student),1,fp);
		fwrite(&stu[i],sizeof(struct student),1,fp);
	}
	else
	{
		for(i=0;i<t;i++)
		fwrite(&stu[i],sizeof(struct student),1,fp);
		fwrite(&p,sizeof(struct student),1,fp);
		for(i=t;i<5;i++)
		fwrite(&stu[i],sizeof(struct student),1,fp);
	}
fclose(fp);

read new student data after insert

//read the inserted file
	if((fp=fopen("sdent.txt","r"))==NULL)
	{
		printf("cannot open stud.txt\n");
		exit(0);
	}
		for(i=0;i<6;i++)
	{
		fread(&re[i],sizeof(struct student),1,fp);	
        printf("%d,%s,%d,%d,%d,%6.2f\n",re[i].num,re[i].name,re[i].score[0],re[i].score[1],re[i].score[2],re[i].ave);
	}	
    fclose(fp);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324683959&siteId=291194637