定义一个结构体,包含学生学号、姓名性别和5门课成绩再根据学号查找学生输入5门成绩
时间: 2023-05-19 09:06:59 浏览: 93
数据结构:定义一个包含学生信息(学号、姓名、成绩)的顺序表_数据结构
5星 · 资源好评率100%
好的,以下是定义结构体和查找学生的代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
int id;
char name[20];
char gender[10];
float grades[5];
};
int main() {
struct Student students[MAX_STUDENTS];
int num_students = 0;
// 添加学生信息
students[num_students].id = 1001;
strcpy(students[num_students].name, "张三");
strcpy(students[num_students].gender, "男");
students[num_students].grades[0] = 80.0;
students[num_students].grades[1] = 85.5;
students[num_students].grades[2] = 90.0;
students[num_students].grades[3] = 87.5;
students[num_students].grades[4] = 92.0;
num_students++;
students[num_students].id = 1002;
strcpy(students[num_students].name, "李四");
strcpy(students[num_students].gender, "女");
students[num_students].grades[0] = 75.0;
students[num_students].grades[1] = 82.5;
students[num_students].grades[2] = 88.0;
students[num_students].grades[3] = 90.5;
students[num_students].grades[4] = 85.0;
num_students++;
// 根据学号查找学生
int search_id;
printf("请输入学生学号:");
scanf("%d", &search_id);
int i;
for (i = 0; i < num_students; i++) {
if (students[i].id == search_id) {
printf("学生姓名:%s\n", students[i].name);
printf("学生性别:%s\n", students[i].gender);
printf("学生成绩:");
int j;
for (j = 0; j < 5; j++) {
printf("%.1f ", students[i].grades[j]);
}
printf("\n");
break;
}
}
if (i == num_students) {
printf("未找到该学生!\n");
}
return 0;
}
```
希望这个代码能够帮到你!
阅读全文