c语言编写程序从键盘输入5个学生的信息,包括学生学号、姓名、性别、出生年月及成绩,用结构体数组保存学生的信息,要求用函数实现结构体数组的输入输出及求成绩最高学生的序号。
时间: 2023-12-16 15:15:43 浏览: 392
好的,这是一个关于C语言结构体数组的程序,可以实现输入5个学生的信息,包括学生学号、姓名、性别、出生年月及成绩,并用结构体数组保存学生的信息。同时,还可以通过函数实现结构体数组的输入输出,并求得成绩最高的学生序号。具体代码如下:
```c
#include<stdio.h>
#include<string.h>
#define MAX_STUDENT_NUM 5
//定义学生结构体
struct student{
int sid; //学号
char name[20]; //姓名
char gender[5]; //性别
char birthday[20]; //出生年月
float score; //成绩
};
//函数声明
void input_student(struct student stu[]);
void output_student(struct student stu[]);
int find_max_score(struct student stu[]);
int main(){
struct student stu[MAX_STUDENT_NUM];
input_student(stu); //输入学生信息
output_student(stu); //输出学生信息
printf("成绩最高学生的序号为:%d\n", find_max_score(stu)); //输出成绩最高学生的序号
return 0;
}
//函数实现
void input_student(struct student stu[]){
int i;
for(i=0; i<MAX_STUDENT_NUM; i++){
printf("请输入第%d个学生的信息:\n", i+1);
printf("学号:");
scanf("%d", &(stu[i].sid));
printf("姓名:");
scanf("%s", stu[i].name);
printf("性别:");
scanf("%s", stu[i].gender);
printf("出生年月:");
scanf("%s", stu[i].birthday);
printf("成绩:");
scanf("%f", &(stu[i].score));
}
}
void output_student(struct student stu[]){
int i;
printf("\n学号\t姓名\t性别\t出生年月\t成绩\n");
for(i=0; i<MAX_STUDENT_NUM; i++){
printf("%d\t%s\t%s\t%s\t\t%.2f\n", stu[i].sid, stu[i].name, stu[i].gender, stu[i].birthday, stu[i].score);
}
}
int find_max_score(struct student stu[]){
int i, max_score_index;
float max_score = 0;
for(i=0; i<MAX_STUDENT_NUM; i++){
if(stu[i].score > max_score){
max_score = stu[i].score;
max_score_index = i;
}
}
return max_score_index;
}
```
希望这个程序可以帮助你理解C语言结构体数组的使用方法。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)