
#include <bits/stdc++.h> using namespace std; fstream in,out; int n=0; string temp[4]; struct book { string isbn; string name; double price; }b[205]; inline bool Check()//时间O(n),空间O(1)。检查是否读入图书信息 { if(n==0) { cout<<"请先选择1读取图书信息 "; return false; } return true; } inline void Input()//时间O(n),空间O(1)。读入图书信息 { in.open("book.txt",ios::in); if(!in) { cout<<"未找到book.txt "; return; } n=1; for(int i=1;i<=4;++i)in>>temp[i]; while(!in.eof()) { in>>b[n].isbn>>b[n].name>>b[n].price; n++; } in.close(); } inline void Output()//时间O(n),空间O(1)。输出图书信息 { if(Check()) { cout<<temp[1]<<" "<<left<<setw(15)<<temp[2]<<" "<<left<<setw(50)<<temp[3]<<" "<<left<<setw(5)<<temp[4]<<" "; for(int i=1;i<n;++i) { cout<<left<<setw(15)<<b[i].isbn<<" "<<left<<setw(50)<<b[i].name<<" "<<left<<setw(5)<<b[i].price<<" "; } } } inline void Length()//时间O(1),空间O(1)。输出图书总册数 { if(Check())cout<<"共"<<n-1<<"本书"<<endl; } inline void Find()//时间O(n),空间O(1)。遍历查找图书 { if(Check()) { cout<<"请输入要查找的图书名字 "; bool flag=false; string str; cin>>str; for(int i=1;i<n;++i) { if(str==b[i].name) { flag=true; cout<<left<<setw(15)<<b[i].isbn<<" "<<left<<setw(50)<<b[i].name<<" "<<left<<setw(5)<<b[i].price<<" "; } } if(flag==false)cout<<"没有收录该图书 "; } } inline void Get()//时间O(1),空间O(1)。直接访问图书 { if(Check()) { cout<<"请输入你要查看的序号 "; int num; cin>>num; if(num<1||num>n-1) { cout<<"输入非法 "; return; } cout<<left<<setw(15)<<b[num].isbn<<" "<<left<<setw(50)<<b[num].name<<" "<<left<<setw(5)<<b[num].price<<" "; } } inline void Update()//时间O(n),空间O(1)。将图书信息存入book_out.txt文件里 { out.open("book_out.txt",ios::out); out<<temp[1]<<" "<<left<<setw(15)<<temp[2]<<" "<<left<<setw(50)<<temp[3]<<" "<<left<<setw(5)<<temp[4]<<" "; for(int i=1;i<n;++i) { out<<left<<setw(15)<<b[i].isbn<<" "<<left<<setw(50)<<b[i].name<<" "<<left<<setw(5)<<b[i].price<<" "; } out.close(); } inline void Insert()//时间O(n),空间O(1)。在数组中插入新元素 { if(Check()) { int num; cout<<"请输入要插入图书的位置 "; cin>>num; if(num<1||num>n) { cout<<"输入非法 "; return; } for(int i=n;i>num;--i) { b[i].isbn=b[i-1].isbn; b[i].name=b[i-1].name; b[i].price=b[i-1].price; } cout<<"请依次输入要插入的图书ISBN号,书名,价格 "; cin>>b[num].isbn>>b[num].name>>b[num].price; n++; Update(); } } inline void Delete()//时间O(n),空间O(1)。在数组中删除元素 { if(Check()) { int num; cout<<"请输入要删除图书的位置 "; cin>>num; if(num<1||num>n-1) { cout<<"输入非法 "; return; } for(int i=num;i<n-1;++i) { b[i].isbn=b[i+1].isbn; b[i].name=b[i+1].name; b[i].price=b[i+1].price; } n--; Update(); } } inline void BubbleSort()//时间O(n^2),空间O(1)。冒泡排序 { if(Check()) { for(int i=1;i<n-1;++i) { for(int j=1;j<n-i;++j) { if(b[j].price>b[j+1].price) { swap(b[j],b[j+1]); } } } Update(); } } inline void QuickSort(int left,int right)//时间O(nlog2n),空间O(nlogn)。快速排序 { if(left>=right)return; int i=left,j=right; double temp=b[left].price; while(i<j) { while(b[j].price>=temp&&i<j)--j; swap(b[i],b[j]); while(b[i].price<=temp&&i<j)++i; swap(b[i],b[j]); } QuickSort(left,i-1); QuickSort(i+1,right); return; } inline void QueryMax()//时间O(n),空间O(1)。查询最大值 { if(Check()) { double mmax=b[1].price; int flag=1; for(int i=2;i<n;++i) { if(b[i].price>mmax) { flag++; mmax=b[i].price; } } for(int i=1;i<n;++i) { if(b[i].price==mmax) { cout<<left<<setw(15)<<b[i].isbn<<" "<<left<<setw(50)<<b[i].name<<" "<<left<<setw(5)<<b[i].price<<" "; } } } } inline void Inverse()//时间O(n),空间O(1)。逆序存储图书信息 { if(Check()) { for(int i=1;i<=n/2;++i) { swap(b[i],b[n-i]); } } } int main() { while(1) { int opt; printf("欢迎使用图书管理系统,请输入整数来实现你需要的功能 " "1.图书数据加载,使用数组进行存储 " "2.输出所有图书信息 " "3.总图书册数 " "4.输入要查找图书的名字 " "5.输入要查找图书的序号 " "6.插入一本图书 " "7.删除一本图书 " "8.按价格冒泡排序 " "9.按价格快速排序 " "10.查询价格最大的图书 " "11.逆序存储数据 " "0.退出 "); scanf("%d",&opt); switch(opt) { case 1:Input();break; case 2:Output();break; case 3:Length();break; case 4:Find();break; case 5:Get();break; case 6:Insert();break; case 7:Delete();break; case 8:BubbleSort();break; case 9:if(Check()){QuickSort(1,n-1),Update();}break; case 10:QueryMax();break; case 11:Inverse();break; case 0:return 0;break; } } return 0; }

#include <bits/stdc++.h> using namespace std; fstream in,out; int n=0; string temp[4]; struct Book { string isbn; string name; double price; }; inline void quicksort(Book b[],int left,int right)//时间O(nlog2n),空间O(log2n)。利用数组进行快速排序 { if(left>=right)return; int i=left,j=right; double temp=b[left].price; while(i<j) { while(b[j].price>=temp&&i<j)--j; swap(b[i],b[j]); while(b[i].price<=temp&&i<j)++i; swap(b[i],b[j]); } quicksort(b,left,i-1); quicksort(b,i+1,right); return; } class book { private: string isbn; string name; double price; book *next; static book *head; static book *tail; public: book():next(NULL) { head=tail=this; } book(string a,string b,double c):isbn(a),name(b),price(c),next(NULL){} bool Check()//时间O(n),空间O(1)。检查是否读入图书信息 { if(n==0) { cout<<"请先选择1读取图书信息 "; return false; } return true; } void Input()//时间O(n),空间O(n)。读入图书信息 { in.open("book.txt",ios::in); if(!in) { cout<<"未找到book.txt "; return; } n=1; tail=head; for(int i=1;i<=4;++i)in>>temp[i]; while(!in.eof()) { string a,b; double c; in>>a>>b>>c; tail->next=new book(a,b,c); tail=tail->next; n++; } in.close(); } void Output()//时间O(n),空间O(1)。输出图书信息 { if(Check()) { cout<<temp[1]<<" "<<left<<setw(15)<<temp[2]<<" "<<left<<setw(50)<<temp[3]<<" "<<left<<setw(5)<<temp[4]<<" "; book *now=head; while(now->next) { now=now->next; cout<<left<<setw(15)<<now->isbn<<" "<<left<<setw(50)<<now->name<<" "<<left<<setw(5)<<now->price<<" "; if(now->next==NULL)break; } } } void Length()//时间O(1),空间O(1)。输出图书总册数 { if(Check())cout<<"共"<<n-1<<"本书"<<endl; } void Find()//时间O(n),空间O(1)。遍历查找图书 { if(Check()) { cout<<"请输入要查找的图书名字 "; book *now=head; bool flag=false; string str; cin>>str; while(now->next) { now=now->next; if(now->name==str) { flag=true; cout<<left<<setw(15)<<now->isbn<<" "<<left<<setw(50)<<now->name<<" "<<left<<setw(5)<<now->price<<" "; if(now->next==NULL)break; } } if(flag==false)cout<<"没有收录该图书 "; } } void Get()//时间O(n),空间O(1)。遍历访问图书 { if(Check()) { cout<<"请输入你要查看的序号 "; int num; cin>>num; if(num<1||num>n-1) { cout<<"输入非法 "; return; } book *now=head; while(num--)now=now->next; cout<<left<<setw(15)<<now->isbn<<" "<<left<<setw(50)<<now->name<<" "<<left<<setw(5)<<now->price<<" "; } } void Update()//时间O(n),空间O(1)。将图书信息存入book_out.txt文件里 { out.open("book_out.txt",ios::out); out<<temp[1]<<" "<<left<<setw(15)<<temp[2]<<" "<<left<<setw(50)<<temp[3]<<" "<<left<<setw(5)<<temp[4]<<" "; book *now=head; while(now->next) { now=now->next; out<<left<<setw(15)<<now->isbn<<" "<<left<<setw(50)<<now->name<<" "<<left<<setw(5)<<now->price<<" "; if(now->next==NULL)break; } out.close(); } void Insert()//时间O(n),空间O(1)。在链表指定位置中插入新元素 { if(Check()) { int num; cout<<"请输入要插入图书的位置 "; cin>>num; if(num<1||num>n) { cout<<"输入非法 "; return; } string a,b; double c; cout<<"请依次输入要插入的图书的ISBN号,书名,价格 "; cin>>a>>b>>c; book *now=head; while(now->next) { num--; if(num==0) { break; } now=now->next; } book *temp=new book(a,b,c); temp->next=now->next; now->next=temp; n++; Update(); } } void Delete()//时间O(n),空间O(1)。在链表指定位置中删除元素 { if(Check()) { cout<<"请输入要删除图书的位置 "; int num; cin>>num; if(num<1||num>n-1) { cout<<"输入非法 "; return; } book *now=head; while(--num)now=now->next; book *temp=now->next->next; delete now->next; now->next=temp; n--; Update(); } } void BubbleSort()//时间O(n^2),空间O(1)。冒泡排序,采用交换相邻两点的数据域的方法 { if(Check()) { book *now=head; book *tmp1; book *tmp2; double temp1; string temp2,temp3; for(int i=1;i<n-1;++i) { now=head->next; for(int j=1;j<n-i;++j) { tmp1=now; tmp2=now->next; if(tmp1->price>tmp2->price) { temp1=tmp1->price; tmp1->price=tmp2->price; tmp2->price=temp1; temp2=tmp1->isbn; tmp1->isbn=tmp2->isbn; tmp2->isbn=temp2; temp3=tmp1->name; tmp1->name=tmp2->name; tmp2->name=temp3; } now=now->next; } } Update(); } } void QuickSort(int left,int right)//时间O(nlog2n),空间O(n)。将数据存储到数组中进行快速排序,再重新存储到链表中 { if(Check()) { book *now=head; int t=1; Book *b=new Book[n]; while(now->next) { now=now->next; b[t].isbn=now->isbn; b[t].name=now->name; b[t].price=now->price; t++; if(now->next==NULL)break; } quicksort(b,left,right); tail=head; for(int i=1;i<t;++i) { tail->next=new book(b[i].isbn,b[i].name,b[i].price); tail=tail->next; } Update(); } } void QueryMax()//时间O(n),空间O(1)。查询最大值 { if(Check()) { book *now=head; double mmax=0; while(now->next) { now=now->next; if((now->price)>mmax) { mmax=now->price; } if(now->next==NULL)break; } now=head; while(now->next) { now=now->next; if(now->price==mmax) { cout<<left<<setw(15)<<now->isbn<<" "<<left<<setw(50)<<now->name<<" "<<left<<setw(5)<<now->price<<" "; } } } } void Inverse()//时间O(n),空间O(n)。逆序存储图书信息 { if(Check()) { book *pre=NULL; book *phead=head; book *now=NULL; while(phead!=NULL) { now=phead->next;//保存剩余链表 phead->next=pre;//断开剩余链表头结点pHead,指向pre pre=phead;//pre更新 phead=now;//phead更新 } tail=head; for(int i=1;i<n;++i) { tail->next=new book(pre->isbn,pre->name,pre->price); pre=pre->next; tail=tail->next; } } } }List; book *book::head; book *book::tail; int main() { while(1) { int o; printf("欢迎使用图书管理系统,请输入整数来实现你需要的功能 " "1.图书数据加载,使用链表进行存储 " "2.输出所有图书信息 " "3.总图书册数 " "4.输入要查找图书的名字 " "5.输入要查找图书的序号 " "6.插入一本图书 " "7.删除一本图书 " "8.按价格冒泡排序 " "9.按价格快速排序 " "10.查询价格最大的图书 " "11.逆序存储数据 " "0.退出 "); scanf("%d",&o); switch(o) { case 1:List.Input();break; case 2:List.Output();break; case 3:List.Length();break; case 4:List.Find();break; case 5:List.Get();break; case 6:List.Insert();break; case 7:List.Delete();break; case 8:List.BubbleSort();break; case 9:List.QuickSort(1,n-1);break; case 10:List.QueryMax();break; case 11:List.Inverse();break; case 0:return 0;break; } } return 0; }
存在的一些BUG已经修改,可参照来完成实验一前20题。