编写程序从键盘输入5个学生的信息,在屏幕上输出这5个学生的信息。要求用结构体数组保存学生的信息,用函数实现结构体数组的输入输出
时间: 2023-05-19 10:03:04 浏览: 62
好的,以下是我为您准备的代码:
#include <stdio.h>
// 定义学生结构体
struct Student {
char name[20];
int age;
char gender[10];
float score;
};
// 定义函数,用于输入学生信息
void input(struct Student *stu) {
printf("请输入学生姓名:");
scanf("%s", stu->name);
printf("请输入学生年龄:");
scanf("%d", &stu->age);
printf("请输入学生性别:");
scanf("%s", stu->gender);
printf("请输入学生成绩:");
scanf("%f", &stu->score);
}
// 定义函数,用于输出学生信息
void output(struct Student *stu) {
printf("姓名:%s,年龄:%d,性别:%s,成绩:%.2f\n", stu->name, stu->age, stu->gender, stu->score);
}
int main() {
// 定义学生结构体数组
struct Student stu[5];
// 循环输入学生信息
for (int i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息:\n", i+1);
input(&stu[i]);
}
// 循环输出学生信息
for (int i = 0; i < 5; i++) {
printf("第%d个学生的信息如下:\n", i+1);
output(&stu[i]);
}
return 0;
}
希望这个程序能够满足您的需求!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)