7.将第6题已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按成绩高低顺序插入,插入后建立一个新文件。

7.将第6题已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按成绩高低顺序插入,插入后建立一个新文件。

#include<stdio.h>
#include<stdlib.h>

#define SIZE 5

struct Student_type
{
	int num;
	char name[10];
	float score1;
	float score2;
	float score3;
	float avg;
}stud[SIZE+1];

void save()
{
	FILE *fp;
	int i;
	if((fp=fopen("students.dat","wb"))==NULL)
	{
		printf("can't open file!\n");
		exit(0);
	}
	for(i=0;i<SIZE;i++)//在for语句中重新把排序好的数组stud的每个元素依次存储到fp指向的文件的中的每个结构体学生数据,每次存储1个数组stud元素 
	{
		if(fwrite(&stud[i],sizeof(struct Student_type),1,fp)!=1)
		{
			printf("file write error\n");
		}
	}
	fclose(fp);
}

int main()
{
    FILE *fp;
	int i,j;
	struct Student_type temp,another;
	printf("请输入插入的第6个学生的学号、姓名和三门课的成绩:"); 
    scanf("%d%s%f%f%f",&another.num,another.name,&another.score1,&another.score2,&another.score3);
	another.avg=(another.score1+another.score2+another.score3)/3;
	printf("新插入的第6个学生的三门课的平均成绩为:%f\n",another.avg);
    fp=fopen("stud.dat","ab");//ab是追加的意思,在这条语句里fp指向二进制文件stud的末尾 
    fwrite(&another,sizeof(struct Student_type),1,fp);//在二进制文件stud的末尾,把结构体变量another的值赋值给fp所指向的文件末尾 
    fclose(fp);
	fp=fopen("stud.dat","rb");
    for(i=0;i<SIZE+1;i++)//在for语句中把fp指向的文件的中的每个结构体学生数据依次存储到数组stud中,每次读入1个sizeof(struct Student_type)到数组f中
    				   //直到for循环终止
	{ 
		fread(&stud[i],sizeof(struct Student_type),1,fp);
	}
	fclose(fp);
	for(i=0;i<SIZE;i++)//按照平均分从小到大的顺序对数组stud排序 
	{
		for(j=i+1;j<SIZE+1;j++)
		{
			if(stud[i].avg>stud[j].avg)
			{
				temp=stud[i];
				stud[i]=stud[j];
				stud[j]=temp;
			}
		}	
	}
	save();
	printf("插入一个学生数据并排序后的的学生数据如下:\n");
	fp=fopen("students.dat","rb");
	for(i=0;i<SIZE+1;i++)//为了验证磁盘文件students中是否已存在此数据,用for语句从students文件中读入数据到stud数组,然后向屏幕上输出 
    {
        fread(&stud[i],sizeof(struct Student_type),1,fp);
        printf("第%d个学生的学号、姓名、三门课的成绩以及平均分为:",i+1);
		printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",stud[i].num,stud[i].name,stud[i].score1,stud[i].score2,stud[i].score3,stud[i].avg);
	}
	fclose(fp);//关闭文件students.dat,防止它被误用 
	return 0;
}

在VS2019下,需将源文件的fopen和scanf做些修改:

#include<stdio.h>
#include<stdlib.h>

#define SIZE 5

struct Student_type
{
	int num;
	char name[10];
	float score1;
	float score2;
	float score3;
	float avg;
}stud[SIZE + 1];

void save()
{
	FILE* fp;
	int i;
	fopen_s(&fp, "students.dat", "wb");
	if (fp == NULL)
	{
		printf("can't open file!\n");
		exit(0);
	}
	for (i = 0; i < SIZE; i++)//在for语句中重新把排序好的数组stud的每个元素依次存储到fp指向的文件的中的每个结构体学生数据,每次存储1个数组stud元素 
	{
		if (fwrite(&stud[i], sizeof(struct Student_type), 1, fp) != 1)
		{
			printf("file write error\n");
		}
	}
	fclose(fp);
}

int main()
{
	FILE* fp;
	int i, j;
	struct Student_type temp, another;
	printf("请输入插入的第6个学生的学号、姓名和三门课的成绩:");
	scanf_s("%d%s%f%f%f", &another.num, another.name, (unsigned int)sizeof(another.name), &another.score1, &another.score2, &another.score3);
	another.avg = (another.score1 + another.score2 + another.score3) / 3;
	printf("新插入的第6个学生的三门课的平均成绩为:%f\n", another.avg);
	fopen_s(&fp, "stud.dat", "ab");//ab是追加的意思,在这条语句里fp指向二进制文件stud的末尾 
	fwrite(&another, sizeof(struct Student_type), 1, fp);//在二进制文件stud的末尾,把结构体变量another的值赋值给fp所指向的文件末尾 
	fclose(fp);
	fopen_s(&fp, "stud.dat", "rb");
	for (i = 0; i < SIZE + 1; i++)//在for语句中把fp指向的文件的中的每个结构体学生数据依次存储到数组stud中,每次读入1个sizeof(struct Student_type)到数组f中
					   //直到for循环终止
	{
		fread(&stud[i], sizeof(struct Student_type), 1, fp);
	}
	fclose(fp);
	for (i = 0; i < SIZE; i++)//按照平均分从小到大的顺序对数组stud排序 
	{
		for (j = i + 1; j < SIZE + 1; j++)
		{
			if (stud[i].avg > stud[j].avg)
			{
				temp = stud[i];
				stud[i] = stud[j];
				stud[j] = temp;
			}
		}
	}
	save();
	printf("插入一个学生数据并排序后的的学生数据如下:\n");
	fopen_s(&fp, "students.dat", "rb");
	for (i = 0; i < SIZE + 1; i++)//为了验证磁盘文件students中是否已存在此数据,用for语句从students文件中读入数据到stud数组,然后向屏幕上输出 
	{
		fread(&stud[i], sizeof(struct Student_type), 1, fp);
		printf("第%d个学生的学号、姓名、三门课的成绩以及平均分为:", i + 1);
		printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n", stud[i].num, stud[i].name, stud[i].score1, stud[i].score2, stud[i].score3, stud[i].avg);
	}
	fclose(fp);//关闭文件students.dat,防止它被误用 
	return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[入数据分析的第一堂课]这是一为数据分析小白量身打造的课程,你从网络或者公众号收集到很多关于数据分析的知识,但是它们零散不成体系,所以第一堂课首要目标是为你介绍:Ø  什么是数据分析-知其然才知其所以然Ø  为什么要学数据分析-有目标才有动力Ø  数据分析的学习路线-有方向走得更快Ø  数据分析的模型-分析之道,快速形成分析思路Ø  应用案例及场景-分析之术,掌握分析方法[哪些同学适合学习这课程]想要转行做数据分析师的,零基础亦可工作中需要数据分析技能的,例如运营、产品等对数据分析感兴趣,想要更多了解的[你的收获]n  会为你介绍数据分析的基本情况,为你展现数据分析的全貌。让你清楚知道自己该如何在数据分析地图上行走n  会为你介绍数据分析的分析方法和模型。这部分是讲数据分析的道,只有学会底层逻辑,能够在面对问时有自己的想法,才能够下一步采取行动n  会为你介绍数据分析的数据处理和常用分析方法。这篇是讲数据分析的术,先有道,后而用术来实现你的想法,得出最终的结论。n  会为你介绍数据分析的应用。学到这里,你对数据分析已经有了初步的认识,并通过一些案例为你展现真实的应用。[专享增值服务]1:一对一答疑         关于课程可以通过微信直接询问老师,获得老师的一对一答疑2:转行问解答         在转行的过程中的相关问都可以询问老师,可获得一对一咨询机会3:打包资料分享         15本数据分析相关的电子书,一次获得终身学习

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值