描述
定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的待入库的新图书的位置和信息,将新图书插入到图书表中指定的位置上,最后输出新图书入库后所有图书的信息。
输入
总计n+3行。首先输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。之后输入第n+2行,内容仅为一个整数,代表待入库的新图书的位置序号。最后输入第n+3行,内容为新图书的信息,书号、书名、价格用空格分隔。
输出
若插入成功: 输出新图书入库后所有图书的信息(书号、书名、价格),总计n+1行,每行是一本图书的信息,书号、书名、价格用空格分隔。其中价格输出保留两位小数。 若插入失败: 只输出以下提示:抱歉,入库位置非法!
输入样例 1
7
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
2
9787822234110 The-C-Programming-Language 38.00
输出样例 1
9787302257646 Data-Structure 35.00
9787822234110 The-C-Programming-Language 38.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
输入样例 2
7
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
9
9787822234110 The-C-Programming-Language 38.00
输出样例 2
Sorry,the position to be inserted is invalid!
#include <iostream>
#include<iomanip>
using namespace std;
#define MAXSIZE 10000
typedef struct
{
string no;
string name;
double price;
}Book;
typedef struct
{
Book *elem;
int num;
}SqList;
void Aver(SqList &L)
{
double aver=0,sum=0;int i=0;
for(i=0;i<L.num;i++)
sum=sum+L.elem[i].price;
aver=sum/L.num;
for(i=0;i<L.num;i++)
{
if(L.elem[i].price<aver)
L.elem[i].price=1.2*L.elem[i].price;
else if(L.elem[i].price>=aver)
L.elem[i].price=1.1*L.elem[i].price;
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<aver<<endl;
}
void Sort(SqList &L)
{
int i=0,j=0;
for(i=0;i<L.num-1;i++)
for(j=0;j<L.num-1-i;j++)
if(L.elem[j].price<L.elem[j+1].price)
{
Book temp;
temp=L.elem[j];
L.elem[j]=L.elem[j+1];
L.elem[j+1]=temp;
}
}
void Input(SqList &L)
{
int n;int i=0;
cin>>n;
string a,b;
double c;
while(i<n)
{
cin>>a>>b>>c;
if(i==n) break;
L.elem[L.num].no=a;
L.elem[L.num].name=b;
L.elem[L.num].price=c;
L.num++;
i++;
}
return ;
}
void Output(SqList &L)
{
int i;
for(i=0;i<=L.num-1;i++)
cout<<L.elem[i].no<<" "<<L.elem[i].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<L.elem[i].price<<endl;
return ;
}
void InitList(SqList &L)
{
L.elem=new Book[MAXSIZE];
if(!L.elem) return;
L.num=0;
return;
}
void Upsidedown(SqList &L)
{
int i;
for(i=0;i<L.num/2;i++)
{
Book temp;
temp=L.elem[i];
L.elem[i]=L.elem[L.num-1-i];
L.elem[L.num-1-i]=temp;
}
}
void mostexpen(SqList &L)
{
Book max=L.elem[0];
int flag[MAXSIZE];
int i=0,k=0;
for(i=0;i<L.num-1;i++)
{
if(L.elem[i].price>max.price)
{
max=L.elem[i];
}
}
for(i=0;i<L.num-1;i++)
{
if(L.elem[i].price==max.price)
{
flag[k]=i;
k++;
}
}
cout<<k<<endl;
for(i=k-1;i>=0;i--)
cout<<L.elem[flag[i]].no<<" "<<L.elem[flag[i]].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<L.elem[flag[i]].price<<endl;
return ;
}
void favourite(SqList &L)
{
int n;int i;int j;string fav[10]; int flag[MAXSIZE]; int k=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>fav[i];
}
for(j=0;j<n;j++)
{
for(i=0;i<L.num-1;i++)
{
if(fav[j]==L.elem[i].name)
{
flag[k]=i;
k++;
}
}
if(k==j)
cout<<"Sorry,there is no your favourite!"<<endl;
else
{
cout<<k<<endl;
for(i=0;i<k;i++)
cout<<L.elem[flag[i]].no<<" "<<L.elem[flag[i]].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<L.elem[flag[i]].price<<endl;
}
}
}
void BestLocate(SqList &L)
{
int n;int i;int j;int no[10]; int k=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>no[i];
}
for(j=0;j<n;j++)
{
for(i=0;i<L.num-1;i++)
{
if(no[j]==i+1)
{
cout<<L.elem[i].no<<" "<<L.elem[i].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<L.elem[i].price<<endl;
k++;
}
}
if(k==j)
cout<<"Sorry,the book on the best position doesn't exist!"<<endl;
}
}
void Add(SqList &L)
{
int no; Book add;int j;
cin>>no;
cin>>add.no>>add.name>>add.price;
if(no>L.num+1||no<1)
{
cout<<"Sorry,the position to be inserted is invalid!"<<endl;
return;
}
for(j=L.num-1;j>=no-1;j--)
{
L.elem[j+1]=L.elem[j];
}
L.elem[no-1]=add;
L.num++;
Output(L);
return;
}
int main()
{
SqList L;
InitList(L);
Input(L);
Add(L);
//BestLocate(L);
//favourite(L);
//Upsidedown(L);
//mostexpen(L);
//Output(L);
return 0;
}
附:
原博地址
https://blog.csdn.net/weixin_43673589