图书信息管理系统的设计与实现
#include<bits/stdc++.h>
using namespace std;
#define DAD(x) ((x)>>1)
#define LE(x) ((x)<<1)
#define RT(x) (((x)<<1)|1)
struct book{
string no;
string name;
double price;
};
struct bokSys
{
book bok[100];
bokSys(){tot=0;}
int tot;
void delSame(){
for(int i=1;i<tot;i++){
if(bok[i].no.compare(bok[tot].no)==0)delet(tot);
}
}
void insert(book a){
bok[++tot]=a;
delSame();
}
void delet(int k){
for(int i=k;i<tot;i++){
bok[i]=bok[i+1];
}
tot--;
}
void set(int k){
cout<<"请输入修改后的书号、书名、价格:\n";
cin>>bok[k].no>>bok[k].name>>bok[k].price;
}
int search(string name){
for(int i=1;i<=tot;i++){
if(bok[i].name.find(name)!=-1)return i;
}
return -1;
}
void printList(){
cout.width(8);cout<<"书号";
cout.width(20);cout<<"书名";
cout.width(6);cout<<"价格\n";
for(int i=1;i<=tot;i++){
cout.width(8);cout<<bok[i].no;
cout.width(20);cout<<bok[i].name;
cout.width(6);cout<<bok[i].price<<endl;
}
cout<<endl;
}
void print(int k){
cout.width(8);cout<<bok[k].no;
cout.width(20);cout<<bok[k].name;
cout.width(6);cout<<bok[k].price<<endl ;
cout<<endl;
}
int quickSearch(string name){
int low=1,high=tot;
while(low<=high){
int mid=(low+high)/2;
int compare=name.compare(bok[mid].name);
if(compare==0)return mid;
if(compare<0)high=mid-1;
else low=mid+1;
}
return 0;
}
friend void sort1(bokSys&S,int low,int high);
friend void sort2(bokSys&S,int low,int high);
};
void sort1(bokSys&S,int low,int high){
if(low<high){
int low1=low,high1=high;
book temp=S.bok[low1];
while(low1<high1){
while(low1<high1&&S.bok[high1].name.compare(temp.name)>=0)high1--;
S.bok[low1]=S.bok[high1];
while(low1<high1&&S.bok[low1].name.compare(temp.name)<=0)low1++;
S.bok[high1]=S.bok[low1];
}
S.bok[low1]=temp;
sort1(S,low,low1-1);
sort1(S,low1+1,high);
}
}
void sort2(bokSys&S,int low,int high){
if (low < high){
int low1 = low, high1 = high;
book temp = S.bok[low1];
while (low1 < high1){
while (low1 < high1 && S.bok[high1].price <= temp.price)--high1;
S.bok[low1] = S.bok[high1];
while (low1 < high1 && S.bok[low1].price >= temp.price)++low1;
S.bok[high1] = S.bok[low1];
}
S.bok[low1] = temp;
sort2(S, low, low1 - 1);
sort2(S, low1 + 1, high);
}
}
int main()
{
cout<<setiosflags(ios::fixed);cout<<setprecision(2);
bokSys S;int k;string name;book temp;
int ch; bool flag = true;
while (flag)
{
cout << "1:插入图书信息\n2:删除图书信息\n3:修改图书信息\n4:打印所有图书信息\n5:查找最爱书籍\n6:查找最贵书籍\n7:统计表中图书个数\n8:退出程序\n请输入实现功能的序号:";
cin >> ch;
switch (ch)
{
case 1:
while (true){
cout << "请依次输入书号、书名、价格:\n";
cin >> temp.no >> temp.name >> temp.price;
if (temp.no == "0" && temp.name == "0" && abs(temp.price-0)<1e-5)break;
else S.insert(temp);
}
break;
case 2:
cout << "请输入要删除书籍的名字:";
cin>>name;
k=S.search(name);
if(k!=-1){
cout<<"删除成功!\n";
S.print(k);
S.delet(k);
}else cout<<"查无此书!\n\n";
break;
case 3:
cout << "请输入要修改书籍的名字:";
cin>>name;
k=S.search(name);
if(k!=-1){
S.set(k);
cout<<"修改成功!\n\n";
}else cout<<"查无此书\n\n";
break;
case 4:S.printList(); break;
case 5:
cout<<"请输入您最爱书籍的名字:";
cin>>name;
sort1(S,1,S.tot);
k=S.quickSearch(name);
if(k!=-1){
cout<<"查找成功!\n";
S.print(k);
}else cout<<"查无此书!\n\n";
break;
case 6:
sort2(S,1,S.tot);
S.print(1);
break;
case 7:cout<<"表中共有"<<S.tot<<"本书籍!\n\n";break;
case 8:flag = false; break;
default:cout << "输入失败,请重新输入\n";
}
}
cout << "退出程序!";
return 0;
}