STUDENT学号、姓名、数学、英语和计算机学号和姓名定义为字符数组类型三门成绩定义为整型。定义一个结构体类型数组存放3位学生的信息依次输入他们的学号、姓名、数学、英语和计算机成绩以列表形式输出3位学生的姓名学号平均分
#include <stdio.h> #include <string.h>
struct student { char id[10]; char name[20]; int math; int english; int computer; };
int main() { struct student stu[3]; int i, sum_math = 0, sum_english = 0, sum_computer = 0;
for (i = 0; i < 3; i++) {
printf("请输入第%d位学生的学号:", i+1);
scanf("%s", stu[i].id);
printf("请输入第%d位学生的姓名:", i+1);
scanf("%s", stu[i].name);
printf("请输入第%d位学生的数学成绩:", i+1);
scanf("%d", &stu[i].math);
printf("请输入第%d位学生的英语成绩:", i+1);
scanf("%d", &stu[i].english);
printf("请输入第%d位学生的计算机成绩:", i+1);
scanf("%d", &stu[i].computer);
printf("\n");
}
printf("学号\t姓名\t平均分\n");
for (i = 0; i < 3; i++) {
sum_math += stu[i].math;
sum_english += stu[i].english;
sum_computer += stu[i].computer;
printf("%s\t%s\t%.2f\n", stu[i].id, stu[i].name, (float)(stu[i].math + stu[i].english + stu[i].computer) / 3.0);
}
printf("平均分\t\t%.2f\t%.2f\t%.2f\n", (float)sum_math / 3.0, (float)sum_english / 3.0, (float)sum_computer / 3.0);
return 0;
原文地址: https://www.cveoy.top/t/topic/dTQ5 著作权归作者所有。请勿转载和采集!