#include<stdio.h>
struct stu
{
const char *name; //姓名
int num; //学号
int age; //年龄
char group; //所在小组
float score; //成绩
}stus[] = {
{"Li ping", 5, 18, 'C', 145.0},
{"Zhang ping", 4, 19, 'A', 130.5},
{"He fang", 1, 18, 'A', 148.5},
{"Cheng lin", 2, 17, 'F', 139.0},
{"Wang ming", 3, 17, 'B', 144.5}
};
void average(struct stu *ps, int len);
void display(struct stu *ps, int len);
int main()
{
int len = sizeof(stus);
display(stus,len);
average(stus, len);
return 0;
}
void average(struct stu *ps, int len){
int sum=0,average,num_140=0,i;
for(i=0;i<len;i++){
sum=sum+ps[i].score;
}
average=sum/len;
for(i=0;i<len;i++){
if(ps[i].score<140){
num_140++;
}
}
printf("%.2f\n",sum);
printf("%.2f\n",average);
printf("%d\n",num_140);
}
void display(struct stu *ps, int len){
int i;
for(i=0;i<len;i++){
printf("%s\t%d\t%d\t%s\t%.1f\n",ps[i].name,ps[i].num,ps[i].age,ps[i].group,ps[i].score);
}
}
//问题如注释说明
#include<stdio.h>
struct stu
{
const char *name; //姓名
int num; //学号
int age; //年龄
char group; //所在小组
float score; //成绩
}stus[] = {
{"Li ping", 5, 18, 'C', 145.0},
{"Zhang ping", 4, 19, 'A', 130.5},
{"He fang", 1, 18, 'A', 148.5},
{"Cheng lin", 2, 17, 'F', 139.0},
{"Wang ming", 3, 17, 'B', 144.5}
};
void average(struct stu *ps, int len);
void display(struct stu *ps, int len);
int main()
{
int len = sizeof(stus)/sizeof(stus[0]);
display(stus,len);
average(stus, len);
return 0;
}
void average(struct stu *ps, int len)
{
// 数据类型错误,socore为float,所以这里最好也是float
float sum=0, average;
int i, num_140=0;
for(i=0;i<len;i++){
sum=sum+ps[i].score;
}
average=sum/len;
for(i=0;i<len;i++){
if(ps[i].score<140){
num_140++;
}
}
printf("%.2f\n",sum);
printf("%.2f\n",average);
printf("%d\n",num_140);
}
void display(struct stu *ps, int len){
int i;
for(i=0;i<len;i++){
// group 的输出格式错误,应为%c。
printf("%s\t%d\t%d\t%c\t%.1f\n", ps[i].name, ps[i].num, ps[i].age, ps[i].group, ps[i].score);
}
}