c语言按学号查找学生成绩
时间: 2023-08-24 12:46:52 浏览: 68
好的,你可以参考以下的C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int id;
char name[20];
float score;
};
int main()
{
struct Student students[5] = {
{1001, "张三", 89.5},
{1002, "李四", 78},
{1003, "王五", 92},
{1004, "赵六", 67},
{1005, "钱七", 85.5}
};
int i, id, flag = 0;
printf("请输入学生的学号:");
scanf("%d", &id);
for (i = 0; i < 5; i++)
{
if (students[i].id == id)
{
printf("学生的姓名为:%s\n", students[i].name);
printf("学生的成绩为:%f\n", students[i].score);
flag = 1;
break;
}
}
if (flag == 0)
{
printf("没有找到该学生的信息!\n");
}
system("pause");
return 0;
}
```
以上代码中,我们定义了一个结构体类型 `struct Student`,其中包含了学生的学号、姓名和成绩。然后我们创建了一个包含5个学生信息的数组 `students`,用于测试。然后通过 `scanf` 函数获取用户输入的学号 `id`,并通过循环遍历数组,查找与输入的学号匹配的学生信息。如果找到了对应的学生信息,则打印学生的姓名和成绩;否则提示没有找到该学生的信息。