利用结构数组处理多个学生信息假设学生信息包括学号、姓名、3门课的成绩。1录入每个学生的学号、姓名和3门课成绩学生数自己确定;2输出每个学生的信息每个学生的信息占据一行。
#include <stdio.h>
struct student { int id; // 学号 char name[20]; // 姓名 float score[3]; // 成绩 };
int main() { int n; printf("请输入学生数:"); scanf("%d", &n); struct student stu[n]; // 定义结构数组
// 录入学生信息
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的学号:", i+1);
scanf("%d", &stu[i].id);
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", stu[i].name);
printf("请输入第%d个学生的三门课成绩:", i+1);
for (int j = 0; j < 3; j++) {
scanf("%f", &stu[i].score[j]);
}
}
// 输出学生信息
for (int i = 0; i < n; i++) {
printf("学号:%d\t姓名:%s\t成绩:%.2f %.2f %.2f\n", stu[i].id, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
}
return 0;
原文地址: http://www.cveoy.top/t/topic/hoBr 著作权归作者所有。请勿转载和采集!