#include <stdio.h> #define MAXNMB 200 struct Student { char name[20]; //姓名 int id; //学号 char jg[100]; //籍贯 float score[3];//成绩 float avg; //平均成绩 }; int main() { FILE* fp; char buf[200] ={0}; Student st[MAXNMB]; int i,nmb = 0; //实际人数 if (!(fp = fopen("stu_info1.txt","r"))) { printf("文件打开失败\n"); return 0; } while(!feof(fp)) { fscanf(fp,"%s%d%s",st[nmb].name,&st[nmb].id,st[nmb].jg); nmb++; } fclose(fp); if (!(fp = fopen("stu_info2.txt","w"))) { printf("文件打开失败\n"); return 0; } //录入成绩 printf("请为每位学生输入3门课程的成绩\n"); for (i = 0;i < nmb;i++) { printf("%s的成绩:",st[i].name); scanf("%f %f %f",&st[i].score[0],&st[i].score[1],&st[i].score[2]); st[i].avg = (st[i].score[0]+st[i].score[1]+st[i].score[2])/3; fprintf(fp,"%s%d%s\t%f%f%f%f\n",st[i].name,st[i].id,st[i].jg,st[i].score[0],st[i].score[1],st[i].score[2],st[i].avg); } fclose(fp); return 0; }
时间: 2024-04-28 22:26:20 浏览: 10
这段代码是一个简单的学生信息管理系统,可以读取一个文本文件中的学生信息(包括姓名、学号、籍贯),然后让用户输入每位学生的3门课程的成绩,并计算出平均成绩,最后将所有信息写入另一个文本文件中。代码中使用了结构体来存储每个学生的信息,以及数组来存储多个学生的信息。同时,还使用了文件读写操作来读取和写入文本文件。
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 // 学生信息结构体 typedef struct { char num[10]; // 学号 char name[10]; // 姓名 char clas[10]; // 班级 char sex[10]; // 性别 float score; // 成绩 } Student; // 学生信息数组 Student studentList[MAX_SIZE]; int size = 0; 帮我添加一个可以查询学生成绩最大值和最小值的分函数代码
可以添加以下代码来实现查询学生成绩最大值和最小值的函数:
```c
// 查询学生成绩最大值和最小值
void findMinMaxScore() {
float maxScore = 0, minScore = 100;
for (int i = 0; i < size; i++) {
if (studentList[i].score > maxScore) {
maxScore = studentList[i].score;
}
if (studentList[i].score < minScore) {
minScore = studentList[i].score;
}
}
printf("成绩最高的学生信息:\n");
for (int i = 0; i < size; i++) {
if (studentList[i].score == maxScore) {
printf("学号:%s 姓名:%s 班级:%s 性别:%s 成绩:%.2f\n",
studentList[i].num, studentList[i].name,
studentList[i].clas, studentList[i].sex,
studentList[i].score);
}
}
printf("成绩最低的学生信息:\n");
for (int i = 0; i < size; i++) {
if (studentList[i].score == minScore) {
printf("学号:%s 姓名:%s 班级:%s 性别:%s 成绩:%.2f\n",
studentList[i].num, studentList[i].name,
studentList[i].clas, studentList[i].sex,
studentList[i].score);
}
}
}
```
在主函数中,可以通过调用该函数来查询学生成绩最大值和最小值:
```c
int main() {
// 添加学生信息
// ...
// 查询学生成绩最大值和最小值
findMinMaxScore();
return 0;
}
```
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STUDENTS 100 // 最多支持100个学生 struct date{ int year; int month; int day; }; typedef struct { int num; // 学号 char name[20]; // 姓名 char sex[5]; // 性别 struct date birthday; // 出生日期 int score[3]; // 三门功课成绩 int total; // 总分 double average; // 平均分 } student; student students[MAX_STUDENTS]; // 学生数组 int num_students = 0; // 学生数量
这段代码定义了一个名为student的结构体,其中包含了学生的学号、姓名、性别、出生日期、三门功课成绩、总分和平均分等信息。同时还定义了一个名为date的结构体,用来表示学生的出生日期。
另外还定义了一个名为students的数组来存储学生信息,数组的长度为MAX_STUDENTS,即最多支持100个学生。还定义了一个变量num_students用来记录当前学生数量。
这段代码的作用是定义了一些数据结构和变量,用来存储和处理学生信息。具体的操作需要在其他代码中实现。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)