用c语言函数根据键盘输入的一个学号,t查找该学生是否存在,若存在,则输出其平均成绩,若不存在,则显示“not Find!”
时间: 2024-05-04 21:21:39 浏览: 29
假设学生信息已经存储在一个结构体数组中,每个结构体包含学号和成绩信息,可以使用如下的C语言代码实现:
```c
#include <stdio.h>
#define MAX_STUDENTS 100 // 最多存储100个学生信息
#define NOT_FOUND -1 // 表示查找不到的返回值
// 学生信息结构体
struct student {
char id[10]; // 学号
float score; // 成绩
};
// 查找学生并返回平均成绩,如果找不到则返回NOT_FOUND
float find_student(struct student students[], int n, char id[]) {
int i;
float total_score = 0;
int count = 0;
for (i = 0; i < n; i++) {
if (strcmp(students[i].id, id) == 0) { // 找到了该学生
return students[i].score; // 返回该学生的成绩
}
}
return NOT_FOUND; // 没有找到该学生
}
int main() {
struct student students[MAX_STUDENTS];
int n; // 学生数量
char id[10]; // 要查找的学生学号
float avg_score; // 平均成绩
int i;
printf("请输入学生数量:");
scanf("%d", &n);
printf("请输入每个学生的学号和成绩:\n");
for (i = 0; i < n; i++) {
scanf("%s%f", students[i].id, &students[i].score);
}
printf("请输入要查找的学生学号:");
scanf("%s", id);
avg_score = find_student(students, n, id);
if (avg_score == NOT_FOUND) {
printf("not Find!\n");
} else {
printf("该学生的平均成绩为:%.2f\n", avg_score);
}
return 0;
}
```
需要注意的是,在实际开发中,应该对用户输入进行合法性验证,比如检查输入的学号是否符合规范。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)