题目:
// 利用结构体数组打印平均成绩,不及格人数,并打印80-100的姓名及成绩
#include <stdio.h>
int main(int argc, const char * argv[]) {
// 定义结构体及变量
struct stu {
int num;
char *name;
float score;
};
// 赋值,即输入成绩
struct stu student[5]={
{01,"chenkang1",45.5},
{02,"chenkang2",55.5},
{03,"chenkang3",77},
{04,"chenkang4",88},
{05,"chenkang5",99}
};
// 计算平均成绩
float avrage = 0;
avrage = (student[0].score + student[1].score + student[2].score + student[3].score + student[4].score)/5;
printf("%.2f\n",avrage);
// 计算不及格人数
int j = 0;//定义相对于for循环的全局变量计算不及格人数
for(int i = 0;i < 5;i++){
if (student[i].score < 60) {
j++;
}
}
printf("%d\n",j);
// 找出80-100成绩
for(int i = 0;i < 5;i++){
if (student[i].score >= 80) {
printf("%s:%.2f\n",student[i].name,student[i].score);
}
}
return 0;
}
|
|