描述
定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据完成图书信息表的创建,然后计算所有图书的平均价格,将所有低于平均价格的图书价格提高20%,所有高于或等于平均价格的图书价格提高10%,最后逐行输出价格修改后的图书信息。
输入
输入n+1行,前n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。最后第n+1行是输入结束标志:0 0 0(空格分隔的三个0)。其中书号和书名为字符串类型,价格为浮点数类型。
输出
总计n+1行,第1行是修改前所有图书的平均价格,后n行是价格修改后n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔。其中价格输出保留两位小数。
输入样例 1
9787302257646 Data-Structure 35.00 9787302164340 Operating-System 50.00 9787302219972 Software-Engineer 32.00 9787302203513 Database-Principles 36.00 9787810827430 Discrete-Mathematics 36.00 9787302257800 Data-Structure 62.00 9787811234923 Compiler-Principles 62.00 9787822234110 The-C-Programming-Language 38.00 0 0 0
输出样例 1
43.88 9787302257646 Data-Structure 42.00 9787302164340 Operating-System 55.00 9787302219972 Software-Engineer 38.40 9787302203513 Database-Principles 43.20 9787810827430 Discrete-Mathematics 43.20 9787302257800 Data-Structure 68.20 9787811234923 Compiler-Principles 68.20 9787822234110 The-C-Programming-Language 45.60
具体解法如下
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<iostream>
#define OK 1
#define OVERFLOW -2
#define MAXSIZE 1000
using namespace std;
typedef struct
{
string isbn;
string name;
float price;
}Book;
typedef struct
{
Book *elem;
int length;
}Sqlist;
int InitList(Sqlist &L){
L.elem = new Book[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length = 0;
return OK;
}
int Input(Sqlist &L){
int i;
for( i = 0; i<MAXSIZE; i++)
{
cin>>L.elem[i].isbn>>L.elem[i].name>>L.elem[i].price;
if(L.elem[i].isbn == "0"&&L.elem[i].name == "0"&&L.elem[i].price == 0)
break;
else
L.length++;
}
return OK;
}
int Sort(Sqlist &L){
float AveragePrice = 0;
float SumPrice = 0;
for(int i = 0; i<L.length; i++)
{
SumPrice += L.elem[i].price;
}
AveragePrice = SumPrice / L.length;
for(int i = 0; i<L.length; i++)
{
if(L.elem[i].price<AveragePrice)
L.elem[i].price *=1.20;
else if(L.elem[i].price >=AveragePrice)
L.elem[i].price *=1.10;
}
cout<<fixed<<setprecision(2)<<AveragePrice<<endl;
return OK;
}
int Output(Sqlist &L){
for(int i=0;i<L.length;i++)
{
cout<<L.elem[i].isbn <<" "<< L.elem[i].name <<" "<<fixed<<setprecision(2)<<L.elem[i].price<<endl;
}
return OK;
}
int main(){
Sqlist L;
InitList(L);
Input(L);
Sort(L);
Output(L);
return 0;
}