BJFU-206-基于顺序存储结构的图书信息表的修改
#include<stdio.h> #include<stdlib.h> #define MAX 1000 typedef struct{ double no; char name[MAX]; double price; }Book; int createList(Book b[]); void traverse(Book b[],int len); double average(Book b[],int len); void add(Book b[],int len,double ave); int main() { Book book[MAX]; int len; double ave; len = createList(book); ave = average(book,len); printf("%.2f\n",ave); add(book,len,ave); traverse(book,len); return 0; } int createList(Book b[]) { int i=0; while(1) { scanf("%lf",&b[i].no); scanf("%s",&b[i].name); scanf("%lf",&b[i].price); if(b[i].no==0&&b[i].name[0]=='0'&&b[i].price==0) break; i++; } return i; } double average(Book b[],int len) { double sum = 0; int i; for(i=0;i<len;i++) { sum = sum+b[i].price; } return sum/i; } void traverse(Book b[],int len) { for(int i=0;i<len;i++) { printf("%.0lf ",b[i].no); printf("%s ",b[i].name); printf("%.2f",b[i].price); printf("\n"); } } void add(Book b[],int len,double ave) { for(int i = 0;i<len;i++) { if(b[i].price<ave) { b[i].price = b[i].price*(1+0.2); } else { b[i].price = b[i].price*(1+0.1); } } }