有五个学生,每个学生有3门课程的成绩,从键盘输入学生数据(包括学号、姓名、3门课程成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件stud中。

有五个学生,每个学生有3门课程的成绩,从键盘输入学生数据(包括学号、姓名、3门课程成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件stud中。

#include <stdio.h>
#include <string.h>
#define N 5
struct Student{
	int num;
	char name[20];
	float score[3];
	float aver;
}stud[N],stud_1[N];

int main()
{
	FILE *fp;
	int i,j;
	float sum; 
	if((fp=fopen("stud.txt","wb"))==NULL)
		{printf("cannot open stud!\n");
		 exit(0);
		}
	for(i=0;i<N;i++)
		{printf("请输入第%d个学生信息(学号、姓名、3门成绩):\n",i+1);
		 scanf("%d %s %f %f %f",&stud[i].num,stud[i].name,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
		}
	for(i=0;i<N;i++)

		{
			sum=0;
			for(j=0;j<3;j++)
				sum=sum+stud[i].score[j];
			stud[i].aver=sum/3;
		}
	for(i=0;i<N;i++)
		if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
			printf("stud write error!\n");
	fclose(fp);
	
	fp=fopen("stud.txt","rb");
	if(fp==NULL)
		{printf("cannot open stud!\n");
		 exit(0);
		}
	printf("从stud.txt文件中读取到的信息如下:\n");
	printf("\t   学号\t姓名\t3门成绩\t\t\t平均成绩\n");
	for(i=0;i<N;i++)
		{printf("第%d个学生:",i+1);
		 fread(&stud_1[i],sizeof(struct Student),1,fp);
		 printf("%d\t%s\t%.2f %.2f %.2f\t%.2f\n",stud[i].num,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].aver);
		}
	return 0;
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 好的,下面是文回复: 首先,我们需要定义一个学生类,包含学号姓名和三门课成绩的属性,以及计算成绩的方法。然后,我们可以使用循环语句和输入函数从键盘输入每个学生的信息,并计算成绩。最后,我们可以将每个学生的信息和总成绩存储到一个磁盘文件。 具体实现步骤如下: 1. 定义一个学生类,包含学号姓名和三门课成绩的属性,以及计算成绩的方法。 ```python class Student: def __init__(self, id, name, score1, score2, score3): self.id = id self.name = name self.score1 = score1 self.score2 = score2 self.score3 = score3 def calc_total_score(self): return self.score1 + self.score2 + self.score3 ``` 2. 使用循环语句和输入函数从键盘输入每个学生的信息,并计算成绩。 ```python students = [] for i in range(5): id = input("请输入第{}个学生学号:".format(i+1)) name = input("请输入第{}个学生姓名:".format(i+1)) score1 = int(input("请输入第{}个学生的第一门课成绩:".format(i+1))) score2 = int(input("请输入第{}个学生的第二门课成绩:".format(i+1))) score3 = int(input("请输入第{}个学生的第三门课成绩:".format(i+1))) student = Student(id, name, score1, score2, score3) students.append(student) total_score = student.calc_total_score() print("第{}个学生的总成绩为:{}".format(i+1, total_score)) ``` 3. 将每个学生的信息和总成绩存储到一个磁盘文件。 ```python with open("stu.txt", "w") as f: for student in students: total_score = student.calc_total_score() line = "{},{},{},{},{},{}\n".format(student.id, student.name, student.score1, student.score2, student.score3, total_score) f.write(line) ``` 这样,我们就完成了从键盘输入学生信息、计算成绩并将数据存储到磁盘文件的任务。 ### 回答2: 本题目是一道简单的数据存储与计算综合题目,需要使用所学的编程知识进行一些数据存储与计算操作,下面我将逐步介绍整个题目的解决思路。 首先,我们需要设计一种数据结构来存储学生的信息,这里可以使用结构体来实现,其包含以下成员: ```C++ struct Student { int id; // 学号 string name; // 姓名 int scores[3]; // 三门课成绩 int total_score; // 总成绩 }; ``` 接下来,我们需要从键盘输入数据,并计算成绩。这部分代码可以用以下方式实现: ```C++ Student stu[5]; // 存储5个学生的信息 for (int i = 0; i < 5; ++i) { cout << "输入学生" << i+1 << "的信息:" << endl; cout << "学号:"; cin >> stu[i].id; cout << "姓名:"; cin >> stu[i].name; cout << "三门课成绩:"; for (int j = 0; j < 3; ++j) { cin >> stu[i].scores[j]; stu[i].total_score += stu[i].scores[j]; } } ``` 接下来,我们需要将原有数据计算的总分存放磁盘文件stu.txt。这部分代码可以用以下方式实现: ```C++ ofstream out("stu.txt"); // 创建一个名为stu.txt的文件,并打开用于写入 for (int i = 0; i < 5; ++i) { out << "学号:" << stu[i].id << " "; out << "姓名:" << stu[i].name << " "; out << "三成绩:" << stu[i].scores[0] << " " << stu[i].scores[1] << " " << stu[i].scores[2] << " "; out << "总成绩:" << stu[i].total_score << endl; } out.close(); // 关闭文件 ``` 最后附上完整代码实现: ```C++ #include <iostream> #include <string> #include <fstream> using namespace std; struct Student { int id; // 学号 string name; // 姓名 int scores[3]; // 三门课成绩 int total_score; // 总成绩 }; int main() { Student stu[5]; for (int i = 0; i < 5; ++i) { cout << "输入学生" << i+1 << "的信息:" << endl; cout << "学号:"; cin >> stu[i].id; cout << "姓名:"; cin >> stu[i].name; cout << "三门课成绩:"; for (int j = 0; j < 3; ++j) { cin >> stu[i].scores[j]; stu[i].total_score += stu[i].scores[j]; } } ofstream out("stu.txt"); for (int i = 0; i < 5; ++i) { out << "学号:" << stu[i].id << " "; out << "姓名:" << stu[i].name << " "; out << "三成绩:" << stu[i].scores[0] << " " << stu[i].scores[1] << " " << stu[i].scores[2] << " "; out << "总成绩:" << stu[i].total_score << endl; } out.close(); return 0; } ``` ### 回答3: 编程有5个学生每个学生有3门课成绩从键盘输入以上数据包括学号姓名,三门课成绩),计算成绩,将原有数据计算的总分存放磁盘文件stu.txt。 首先,我们需要定义一个包含学号姓名和三门课成绩数据结构。 ``` struct Student{ int id; // 学号 char name[20]; // 姓名 float score[3]; // 三门课成绩 float total; // 总成绩 }; ``` 接下来从键盘输入5个学生的信息,然后计算每个学生的总成绩。 ``` Student stu[5]; // 从键盘输入学生信息 for(int i=0; i<5; i++){ printf("请输入学生%d的信息:\n", i+1); printf("学号:"); scanf("%d", &stu[i].id); printf("姓名:"); scanf("%s", stu[i].name); printf("三门课成绩(用空格分隔):"); scanf("%f %f %f", &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]); // 计算成绩 stu[i].total = stu[i].score[0] + stu[i].score[1] + stu[i].score[2]; } ``` 接着,我们将每个学生的信息和总成绩写入磁盘文件stu.txt。 ``` FILE *fp; // 文件指针 fp = fopen("stu.txt", "w"); // 打开文件,如果文件不存在则创建 // 写入学生信息和总成绩 for(int i=0; i<5; i++){ fprintf(fp, "学号:%d,姓名:%s,三门课成绩:%5.2f %5.2f %5.2f,总成绩:%5.2f\n", stu[i].id, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].total); } fclose(fp); // 关闭文件 ``` 最后,我们完成了如下图所示的程序流程: ![image-20210520175917873](https://i.loli.net/2021/05/20/ZsJTGtKgSiwVe53.png)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值