假设有3个学生,每个学生有4门课程的成绩,通过键盘输入这3个学生的有关数据,主要包括有学号、姓名以及4门课程成绩,如表1所示。现要求计算每个学生的总分和平均分,同时把每个学生的数据(学号、姓名、4门课程成绩、总分及平均分)存放在硬盘文件studdata1.dat中。
时间: 2023-12-15 21:11:45 浏览: 90
这是一道编程题,可以使用C语言进行编写。
首先,定义一个结构体存储每个学生的信息:
```c
struct Student {
int id; // 学号
char name[20]; // 姓名
int scores[4]; // 四门课程成绩
int totalScore; // 总分
float avgScore; // 平均分
};
```
然后,定义一个函数用于从键盘输入学生信息:
```c
void inputStudents(struct Student students[], int count) {
for (int i = 0; i < count; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%d", &students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("四门课程成绩:");
for (int j = 0; j < 4; j++) {
scanf("%d", &students[i].scores[j]);
}
}
}
```
接着,定义一个函数用于计算每个学生的总分和平均分:
```c
void calculateScores(struct Student students[], int count) {
for (int i = 0; i < count; i++) {
int total = 0;
for (int j = 0; j < 4; j++) {
total += students[i].scores[j];
}
students[i].totalScore = total;
students[i].avgScore = total / 4.0;
}
}
```
最后,定义一个函数用于将每个学生的信息写入硬盘文件:
```c
void saveStudentsToFile(struct Student students[], int count) {
FILE *fp;
fp = fopen("studdata1.dat", "wb");
fwrite(students, sizeof(struct Student), count, fp);
fclose(fp);
}
```
完整代码如下:
```c
#include <stdio.h>
struct Student {
int id;
char name[20];
int scores[4];
int totalScore;
float avgScore;
};
void inputStudents(struct Student students[], int count) {
for (int i = 0; i < count; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%d", &students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("四门课程成绩:");
for (int j = 0; j < 4; j++) {
scanf("%d", &students[i].scores[j]);
}
}
}
void calculateScores(struct Student students[], int count) {
for (int i = 0; i < count; i++) {
int total = 0;
for (int j = 0; j < 4; j++) {
total += students[i].scores[j];
}
students[i].totalScore = total;
students[i].avgScore = total / 4.0;
}
}
void saveStudentsToFile(struct Student students[], int count) {
FILE *fp;
fp = fopen("studdata1.dat", "wb");
fwrite(students, sizeof(struct Student), count, fp);
fclose(fp);
}
int main() {
struct Student students[3];
inputStudents(students, 3);
calculateScores(students, 3);
saveStudentsToFile(students, 3);
return 0;
}
```
注意:在实际开发中,可能需要对输入的数据进行校验,以确保输入的数据是合法的。例如,可以检查学号是否重复、成绩是否在合理的范围内等。此外,还需要考虑如何从文件中读取学生信息,并进行处理。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)