C语言实现学生信息管理:查找年龄最大的学生
#include <stdio.h>
#include <string.h>
#define MAX_NUM 100
#define ID_LEN 13
#define NAME_LEN 21
struct Student {
char id[ID_LEN];
char name[NAME_LEN];
int age;
};
int main() {
int n;
struct Student stu[MAX_NUM];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s %s %d", stu[i].id, stu[i].name, &stu[i].age);
}
int max_age = -1;
int max_index = -1;
for (int i = 0; i < n; i++) {
if (stu[i].age > max_age) {
max_age = stu[i].age;
max_index = i;
}
}
printf("%s %s", stu[max_index].id, stu[max_index].name);
return 0;
}
代码功能:
- 定义结构体: 使用
struct Student结构体存储每个学生的信息,包括学号 (id)、姓名 (name) 和年龄 (age)。 - 输入学生信息: 从键盘输入学生人数
n,并循环输入每个学生的学号、姓名和年龄,存储到stu数组中。 - 查找年龄最大的学生: 遍历
stu数组,找出年龄最大的学生,记录其索引max_index。 - 输出结果: 输出年龄最大的学生的学号和姓名。
代码注释:
代码中包含详细的注释,解释了每个代码段的功能。
使用示例:
输入:
3
20221001 张三 20
20221002 李四 19
20221003 王五 21
输出:
20221003 王五
总结:
该代码展示了使用C语言结构体和数组存储学生信息,并查找特定条件下学生信息的示例。通过代码注释和示例,可以更好地理解C语言的基本数据结构和算法应用。
原文地址: http://www.cveoy.top/t/topic/ojH9 著作权归作者所有。请勿转载和采集!