#include<stdio.h>
struct book{ //定义结构体
int id;
char name[10];
char editor[10];
int price;
};
int main(){
struct book books[10]; //定义结构体数组
int i,sum=0;
for(i=0;i<10;i++){
scanf("%d%s%s%d",&books[i].id,&books[i].name,&books[i].editor,&books[i].price);
sum+=books[i].price;
}
//printf("%d",sum);
float ave;
ave=sum/10.0;
struct book *p; //定义一个指向book结构体类型的指针变量
printf("ID\tname\teditor\tprice");
for(p=books;p<books+10;p++){
/*p=book 使p指向结构体数组books中的第一个元素,p+1指向下一个元素
指向下一个元素指的是数组中的下一个元素,一个数组元素中存储的是包含
了定义的结构体中的所有元素。*/
printf("\n%d \t%s \t %s\t %d\n",p->id,p->name,p->editor,p->price);
}
printf("\n%.2f",ave);
return 0;
}
运行结果如图所示:
若有错误,敬请指出,万分感激!