1 #include <iostream>
2 #include <cstdio>
3 #include <string>
4 #include <cstring>
5 #include <fstream>
6 #include <iomanip>
7 #include <ctype.h>
8 using namespace std;
9 //定义结构体
10 struct perbook{
11 double price;
12 char name[50];
13 char isbn[15];
14 }book[155];
15 int cnt=0;
16 int ok=0;
17 char title1[10],title2[10];
18 char Name[10],Isbn[10],Price[10];
19
20 //1读入文件 T(n)=O(n) S(1)
21 void Input(){
22 int i=0;
23 fstream infile;
24 infile.open("book.txt",ios::in);
25 if(!infile){
26 cout<<"打开失败!\n"<<endl;
27 exit(1);
28 }
29 else{
30 infile>>title1;
31 cout<<"正在载入 "<<title1<<" 中——————"<<endl;
32 infile>>Name>>Isbn>>Price;
33 while(!infile.eof()){
34 infile>>book[i].isbn>>book[i].name>>book[i].price;
35 // cout<<i<<" : "<<left<<setw(15)<<book[i].isbn<<setw(50)<<book[i].name<<"\t"<<left<<setw(5)<<book[i].price<<endl;
36 i++;
37 }
38 }
39 cnt=i;// cout<<"i="<<i<<endl;
40 infile.close();
41 cout<<"=========== 读入成功!=========\n"<<endl;
42 ok=1;
43 }
44
45 //2输出信息 T(n)=O(n) S(1)
46 void Output(){
47 if(!ok){
48 cout<<"\t\t当前未录入图书信息,请先录入。\n"<<endl;
49 exit(0);
50 }
51 else{
52 cout<<title1<<endl;
53 cout<<"位置\t"<<left<<setw(15)<<Name<<"\t"<<left<<setw(50)<<Isbn<<"\t"<<left<<setw(5)<<Price<<endl;
54 for(int i=0;i<cnt;i++)
55 {
56 cout<<i+1<<"\t"<<left<<setw(15)<<book[i].isbn<<setw(50)<<book[i].name<<"\t"<<left<<setw(5)<<book[i].price<<endl;
57 }
58 cout<<"\t\t==========输出完毕=========\n"<<endl;
59 }
60 }
61
62 //3计算图书总数 T(n)=O(1) S(1)
63 void Count(){
64 cout<<"当前共有图书"<<cnt<<"本~\n";
65 }
66
67 //4书名查找 T(n)=O(n) S(1)
68 void Locate(){
69 char find[20];
70 bool okk=false;
71 cout<<"请输入书名: ";
72 cin>>find;
73 cout<<"位置\t"<<left<<setw(15)<<Name<<"\t"<<left<<setw(50)<<Isbn<<"\t"<<left<<setw(5)<<Price<<endl;
74 for(int i=0;i<cnt;++i)
75 {
76 if(strcmp(find,book[i].name)==0){
77 cout<<i+1<<"\t"<<left<<setw(15)<<book[i].isbn<<setw(50)<<book[i].name<<"\t"<<left<<setw(5)<<book[i].price<<endl;
78 okk=true;
79 }
80 }
81
82 if(!okk)
83 {
84 cout<<"=====查无此书~对不起======\n";
85 }
86 else{
87 cout<<"=========以上是为您搜索到的同名图书。=========\n";
88 }
89
90 }
91 //5位置查找 T(n)=O(1) S(1)
92 void Get(){
93 int finds;
94 cout<<"请输入书的位置: ";
95 cin>>finds;
96 if(finds>=cnt)
97 {
98 cout<<"该位置不存在图书!请检查后重试\n";
99 exit(0);
100 }
101 cout<<left<<setw(15)<<Name<<"\t"<<left<<setw(50)<<Isbn<<"\t"<<left<<setw(5)<<Price<<endl;
102 cout<<left<<setw(15)<<book[finds-1].isbn<<setw(50)<<book[finds-1].name<<"\t"<<left<<setw(5)<<book[finds-1].price<<endl;
103 cout<<"=========以上是为您搜索到的同位图书。=========\n";
104 }
105 //6指定位置插入图书信息 T(n)=O(n) S(1)
106 void Insert(){
107 int locality;
108 struct perbook newbook;
109 cout<<"请输入您要插入的图书的位置:";
110 cin>>locality; cout<<endl;
111 cout<<"请输入该图书的ISBN号:";
112 cin>>newbook.isbn; cout<<endl;
113 cout<<"请输入该图书的书名:";
114 cin>>newbook.name; cout<<endl;
115 cout<<"请输入该图书的单价:";
116 cin>>newbook.price; cout<<endl;
117 cnt++;
118 //先把后面的图书向后挪一位
119 for(int i=cnt-1;i>=locality-1;i--)
120 {
121 struct perbook temp;
122 temp=book[i];
123 book[i+1]=temp;
124 }
125 //插入新图书
126 book[locality-1]=newbook;
127 cout<<"==========新图书信息插入成功======\n";
128
129 }
130 //7指定位置删除图书信息 T(n)=O(n) S(1)
131 void Delete(){
132 int locality;
133 cout<<"请输入您要删除的图书的位置:";
134 cin>>locality;
135 cout<<endl;
136 for(int i=locality;i<cnt;i++)
137 {
138 book[i-1]=book[i];
139 }
140 cnt--;
141 cout<<"==========图书信息删除成功======\n";
142 }
143 //8根据定价排序 T(n)=O(nlogn) S(1)
144 void Qsort(int low, int high){
145 if (high <= low) return;
146 int i = low;
147 int j = high + 1;
148 int key = book[low].price;
149 while (true)
150 {
151 while (book[++i].price < key)
152 {
153 if (i == high){
154 break;
155 }
156 }
157 while (book[--j].price > key)
158 {
159 if (j == low){
160 break;
161 }
162 }
163 if (i >= j) break;
164 perbook temp = book[i];
165 book[i] = book[j];
166 book[j] = temp;
167 }
168 perbook temp = book[low];
169 book[low] = book[j];
170 book[j] = temp;
171 Qsort(low, j - 1);
172 Qsort( j + 1, high);
173
174 }
175 //9 求价格最高的图书信息 T(n)=O(n) S(1)
176 void Max()
177 {
178 double expensive=0;
179 int exp[155];
180 int expcnt=0;
181 memset(exp,0,sizeof(exp));
182 for(int i=0;i<cnt;i++)
183 {
184 if(book[i].price>expensive)
185 {
186 expensive=book[i].price;
187 memset(exp,0,sizeof(exp));
188 expcnt=0;
189 exp[expcnt++]=i;
190 }
191 else if(book[i].price==expensive)
192 {
193 exp[expcnt++]=i;
194 }
195 }
196 cout<<"位置\t"<<left<<setw(15)<<Name<<"\t"<<left<<setw(50)<<Isbn<<"\t"<<left<<setw(5)<<Price<<endl;
197 for(int i=0;i<expcnt;i++)
198 cout<<exp[i]<<"\t"<<left<<setw(15)<<book[exp[i]].isbn<<setw(50)<<book[exp[i]].name<<"\t"<<left<<setw(5)<<book[exp[i]].price<<endl;
199 cout<<"\t\t==========输出完毕=========\n"<<endl;
200 }
201
202 //10 逆序存储图书信息 T(n)=O(n) S(1)
203 void Inverse()
204 {
205 if(!ok)
206 {
207 cout<<"\t\t当前未录入图书信息,请先录入。\n"<<endl;
208 exit(0);
209 }
210 for(int i=0;i<cnt/2;i++)
211 {
212 perbook temp;
213 temp=book[i];
214 book[i]=book[cnt-i-1];
215 book[cnt-i-1]=temp;
216 }
217 cout<<"===========逆序存储成功===========\n";
218 //是否需要查看逆序存储结果
219 cout<<"是否需要查看逆序存储结果?请输入(Y/N):";
220 char op;
221 cin>>op;
222 op=toupper(op);
223 if(op=='Y')
224 {
225 Output();
226 }
227 else{
228 cout<<"您已安全结束本次操作!\n";
229 }
230
231 }
232 int main(){
233 int op;
234 while(1){
235 cout<<"\n=========================欢迎使用北林图书信息管理系统(beta 1.0)=======================\n";
236 cout<<"=======================请根据对应编号输入数字并按回车即可执行功能=======================\n";
237 cout<<"\t\t1.导入图书信息文件\t\t2.输出所有图书信息\n";
238 cout<<"\t\t3.获取图书总数\t\t\t4.输入书名获取位置及其他图书信息\n";
239 cout<<"\t\t5.输入图书位置获取图书信息\t6.插入新的图书信息\n";
240 cout<<"\t\t7.输入图书位置删除图书信息\t8.根据价格升序重排图书信息\n";
241 cout<<"\t\t9.求价格最高的图书信息\t\t10.逆序存储图书信息\n";
242 cout<<"\t\t11.退出\n"<<endl;
243 cout<<"输入命令数字:";
244 cin>>op;
245 cout<<endl;
246 switch(op){
247 case 1:Input();break;//输入
248 case 2:Output();break;//输出
249 case 3:Count();break;//返回计数
250 case 4:Locate();break;//书名查找
251 case 5:Get();break;//位置查找
252 case 6:Insert();break;//插入 【beta1.0采用结构体数组】
253 case 7:Delete();break;//删除
254 case 8:{
255 Qsort(0,cnt-1);
256 cout<<"======================排序成功===============\n"; }break; //价格排序 【beta1.0采用快速排序】
257 case 9:Max();break;//最高价格
258 case 10:Inverse();break; //倒序存储
259 case 11:cout<<"\t退出成功,再见~";exit(0);
260 }
261 }
262 return 0;
263 }