#include<stdio.h> #include<stdlib.h> #define MAX 100 typedef struct Book{ double no; char name[MAX]; double price; struct Book * next; }Book,*BList; //创建列表 void CreatList(BList &B) { //头插法创建单链表 B = (BList)malloc(sizeof(Book)); B->next = NULL; BList rear = B; while(1) { BList p = (BList)malloc(sizeof(Book)); scanf("%lf",&p->no); scanf("%s",p->name); scanf("%lf",&p->price); if(p->no==0&&p->name[0]=='0'&&p->price==0) break; rear->next = p; p->next = NULL; rear = p; } } //遍历 void traverse(BList B) { BList p = B->next; while(p) { printf("%.0f ",p->no); printf("%s ",p->name); printf("%.2f",p->price); printf(" "); p = p->next; } } //获得平均值并返回其值 double getAvar(BList B) { BList p = B->next; double sum=0.0; int i = 0; while(p) { sum+=p->price; p = p->next; i++; } return sum/i; } //按题意修改价格 void alter(BList &B) { BList p = B->next; while(p) { if(p->price<getAvar(B)) p->price = p->price*(1+0.2); else p->price = p->price*(1+0.1); p = p->next; } } int main() { BList B; CreatList(B); printf("%.2f ",getAvar(B)); alter(B); traverse(B); return 0; }