#include<iostream>
#include<iomanip>
#include<cstring>
#define MAX 100
using namespace std;
struct Book{
char no[15];
char name[30];
float price;
};
struct Seqlist{
Book elem[MAX];
int length;
};
Seqlist list;
void input(){ //输入
int nums; //书的数目
Book s; //建立每本书的对象
cout<<"请输入您要输入的图书的数目:"<<'\t';
cin>>nums;
int i=list.length;
for(list.length;list.length<i+nums;list.length++){
cout<<"请输入图书的书号、书名、价格:"<<'\t';
cin>>s.no>>s.name>>s.price;
list.elem[list.length]=s;
}
}
void output(Seqlist m){ //输出
cout<<"-----------------------------------------------------------------"<<endl;
cout<<"|序号| 书 号 | 名 称 | 价 格 |"<<endl;
for(int i=0;i<m.length;i++){
cout<<"----------------------------------------------------------------"<<endl;
cout<<"|"<<setw(4)<<i+1<<"|"<<setw(17)<<m.elem[i].no<<"|"<<setw(32)<<m.elem[i].name<<"|"<<setiosflags(ios::fixed)<<setprecision(2)<<setw(7)<<m.elem[i].price<<"|"<<endl;
}
cout<<"----------------------------------------------------------------"<<endl;
}
void insert(){ //插入
int loc;//新图书的位置
Book s;
cout<<"请输入新图书插入的位置:"<<'\t';
cin>>loc;
cout<<"请输入新图书的相关信息:"<<'\t';
cin>>s.no>>s.name>>s.price;
if(loc>0&&loc<=list.length){
for(int i=list.length;i>=loc;i--) list.elem[i]=list.elem[i-1];
list.elem[loc-1]=s;
list.length++;
output(list);
}
else cout<<"抱歉,入库位置非法"<<endl;
}
void Delete(){ //删除
char booknums[15];int flag=0;
cout<<"请输入要出库的旧图书的书号:"<<'\t';
cin>>booknums;
for(int i=0;i<list.length;i++){
if(strcmp(list.elem[i].no,booknums)==0){
for(int j=i;j<list.length;j++)
list.elem[j]=list.elem[j+1];
list.length--;
flag=1;
}
}
if(flag) output(list);
else cout<<"出库失败,未找到该图书!"<<endl;
}
void statistic(){
cout<<"图书数目为:"<<list.length<<endl;
}
void duplicate(){ //去重
for(int i=0;i<list.length;i++)
for(int j=i+1;j<list.length;j++)
if(strcmp(list.elem[i].no,list.elem[j].no)==0){
for(int k=j;k<list.length;k++) list.elem[k]=list.elem[k+1];
list.length--;j--;
}
statistic();
output(list);
}
void lovebook(){ //最爱图书的查询
Seqlist love;
char booknames[30];//图书的书名
cout<<"请输入您喜欢的图书的书名:"<<'\t';
cin>>booknames;
love.length=0;
for(int i=0;i<list.length;i++)
if(strcmp(list.elem[i].name,booknames)==0)
love.elem[love.length++]=list.elem[i];
if(love.length==0) cout<<"抱歉,没有你的最爱!"<<endl;
else{
cout<<"您最爱的图书数目为"<<love.length<<endl;
output(love);
}
}
void modify(){ //批量修改
float average;
for(int i=0;i<list.length;i++){
average+=list.elem[i].price;
}
average=average/list.length;
for(int i=0;i<list.length;i++){
if(list.elem[i].price<average) list.elem[i].price=list.elem[i].price*1.2;
else list.elem[i].price=list.elem[i].price*1.1;
}
output(list);
}
void expensive(){ //最贵图书的查询
Seqlist expen;
float max=list.elem[0].price;
int a=0;expen.length=0;
for(int i=1;i<list.length;i++)
if(max<list.elem[i].price){
max=list.elem[i].price;
a=i;
}
for(int i=1;i<list.length;i++)
if(list.elem[i].price==max)
expen.elem[expen.length++]=list.elem[i];
cout<<"最贵图书的数目为"<<expen.length<<endl;
output(expen);
}
void downsort(Book s[],int l,int r){ //图书价格降序排序
if(l<r){
int i=l,j=r;
Book key=list.elem[l];
while(i<j){
while(i<j&&list.elem[j].price>=key.price) j--;
if(i<j) list.elem[i++]=list.elem[j];
while(i<j&&list.elem[i].price<=key.price) i++;
if(i<j) list.elem[j--]=list.elem[i];
}
list.elem[i]=key;
downsort(s,l,i-1);
downsort(s,i+1,r);
}
}
void selectfun(){
int nums;//选择的功能
cout<<"-----------------------------------------------"<<endl;
cout<<"|根据指定图书个数,逐个输入图书信息----------1|"<<endl;
cout<<"|逐个显示图书表中所有图书的相关信息----------2|"<<endl;
cout<<"|新图书插入到图书表中指定的位置--------------3|"<<endl;
cout<<"|根据旧图书的位置,将图书从图书表中删除------4|"<<endl;
cout<<"|统计表中图书个数----------------------------5|"<<endl;
cout<<"|图书信息表的图书去重------------------------6|"<<endl;
cout<<"|查询最爱书籍的书号和价格--------------------7|"<<endl;
cout<<"|图书信息表按指定条件进行批量修改------------8|"<<endl;
cout<<"|按照图书价格降序排序------------------------9|"<<endl;
cout<<"|实现最贵图书的查找-------------------------10|"<<endl;
cout<<"-----------------------------------------------"<<endl;
cin>>nums;
switch(nums){
case 1:input(); break;
case 2:output(list); break;
case 3:insert(); break;
case 4:Delete(); break;
case 5:statistic(); break;
case 6:duplicate(); break;
case 7:lovebook(); break;
case 8:modify(); break;
case 9:downsort(list.elem,0,list.length-1);output(list);break;
case 10:expensive(); break;
}
}
int main(){
selectfun();
while(1){
char choice;
cout<<"是否选择继续操作(Y or N)"<<endl;
cin>>choice;
if(choice=='Y'||choice=='y') selectfun();
else if(choice=='N'||choice=='n') break;
else cout<<"输入有误,请重新输入";
}
return 0;
}