本文实例为大家分享了C++实现图书管理程序的具体代码,供大家参考,具体内容如下
主文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include <iostream> #include "library.h" #include "info.h"//读取帮助文件 using namespace std; int main() { char choice= 'w' ; string bookid; int readerid; //图书、读者编号 RDatabase ReaderDB; //读者库 Reader *r; BDatabase BookDB; //图书库 Book *b; int i=1; while (choice!= '0' ) { cout << "\n\n\t\t\t 图 书 管 理 系 统\n\n\n" ; cout << "\t\t\t1: 借 书\n\n\t\t\t2: 还 书 \n\n\t\t\t3: 图 书 维 护\n\n\t\t\t4: 读 者 维 护\n\n\t\t\t5: 帮 助\n\n\t\t\t0: 退 出" <<endl; cin >> choice; switch (choice) { case '1' : cout << " |借书|\n读者编号(1~" <<Maxr<< "):" ; cin >>readerid; r=ReaderDB.query(readerid); //按编号查找 if (NULL==r) //查找是否有该读者 { cout << "不存在该读者,不能借书" <<endl; break ; } cout << " 图书编号(n-n-n-x):" ; bookid=input_ISBN(); b=BookDB.query(bookid); //查找是否有该书 if (b==NULL) { cout << "不存在该图书,不能借书" << endl; break ; } if (b->borrowbook()==0) //存在该书的情况下查找该书是否已借出 { cout << " 该图书已借出,不能借书" << endl; break ; } r->borrowbook(b->getISBN()); //成功借书 cout<< "借书成功!" <<endl; break ; case '2' : cout<< "|还书|\n 读者编号(1~" <<Maxr<< "):" ; cin >>readerid; if (r==NULL) { cout << "不存在该读者,不能还书" << endl; break ; } cout << "图书编号(n-n-n-x):" ; bookid=input_ISBN(); r=ReaderDB.query(readerid); b=BookDB.query(bookid); if (b==NULL) { cout << "不存在该图书,不能还书" <<endl; break ; } b->retbook(); r->retbook(b->getISBN()); //还书成功 break ; case '3' : BookDB.bookdata(); //图书维护 break ; case '4' : ReaderDB.readerdata(); //读者维护 break ; case '5' : read_info(); case '0' : break ; default : cout<< "输入错误,请重新输入:" ; } } return 0; } |
acquire_date.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//获取系统时间 #include <iostream> #include <ctime> /* time.h in C */ using namespace std; struct Time { int y; int m; int d; }; Time acquire_time() { time_t now; struct tm *fmt; Time t; time (&now); fmt = localtime (&now); t.y=fmt->tm_year%100; //获取年月日 t.m=fmt->tm_mon+1; t.d=fmt->tm_mday; cout<<t.y<< "年" <<t.m<< "月" <<t.d<< "日" <<endl; return t; } |
delete_space.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
//去空格 #include <iostream> #include <string> using namespace std; string input_ISBN() { bool ifGo=1; char n= '0' ; string names= "0" ; cout<< "(输入ISBN)" <<endl; while (ifGo==1){ int i=0; int f=0; while (cin>>n) { if (n>= '0' &&n<= '9' &&i<3){ names+=n; f++;} else if (n== '-' &&i<3&&f!=0){ names+=n; i++; } else if (i==3&&((n>= 'a' &&n<= 'z' )||(n>= 'A' &&n<= 'Z' )||(n>= '0' &&n<= '9' ))) { names+=n; cin.clear(); cin.sync(); ifGo=0; break ; } else { cout<< "输入错误!请重新输入:" ; names= "0" ; ifGo=1; cin.clear(); cin.sync(); i=0; f=0; } } } string names1; for ( int j=1;j<names.length();j++) names1+=names[j]; return names1; } |
info.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//读取帮助文件 #include <fstream> #include <iostream> using namespace std; const int l=100; //每一行的长度 void read_info() { ifstream ios( "help.txt" ); char s[l]; //用于读取文件中的一行 for ( int j=0;j<l;j++) s[j]= '\t' ; while ( ios.getline(s,l)) { for ( int i=0;i<l;i++) { if (s[i]!= '\t' ) cout<<s[i]; } cout<<endl; for ( int j=0;j<l;j++) s[j]= '\t' ; } } |
library.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
//主功能实现 #include <iostream> #include <iomanip> #include <string> #include <fstream>//输入/输出文件流类 #include "delete_space.h" #include "acquire_date.h"//获取系统日期 using namespace std; const int Maxr=100; //最多的读者 const int Maxb=100; //最多的图书 const int Maxbor=8; //每位读者最多借八本书 const int bkNlen=20; //图书名字长度 const int rdNlen=20; //读者及作者名字长度 const int Maxday=60; //读书借阅期(以日算) enum Genre{fic,nfic,per,bio,chi,unk}; /*fic-fiction,nfic-nonfiction,per-periodical, bio-biography,chi-children,unk-unknown*/ //读者类,实现对读者的信息的描述 class Reader { private : bool tag; //删除标记 1:已删 0:未删 int num; //读者编号 char name[rdNlen]; //读者姓名 string borbook[Maxbor]; //所借图书 Time bortime[Maxbor]; //借书时间 public : Reader() {} char *getname() { return name;} //获取姓名 bool gettag() { return tag;} //获取删除标记 int getnum() { return num;} //获取读者编号 void setname( char na[]) //设置姓名 { strcpy (name,na); } void delbook(){ tag=1; } //设置删除标记 1:已删 0:未删 void addreader( int n, char *na) //增加读者 { tag=0; num=n; strcpy (name,na); for ( int i=0;i<Maxbor;i++) borbook[i]= "0" ; } void borrowbook(string bookid) //借书操作 { for ( int i=0;i<Maxbor;i++) { if (borbook[i]== "0" ) { borbook[i]=bookid; bortime[i]=acquire_time(); return ; } } } int retbook(string bookid) //还书操作 { for ( int i=0;i<Maxbor;i++) { if (borbook[i]==bookid) { borbook[i]= "0" ; bortime[i].y=0; bortime[i].m=0; bortime[i].d=0; return 1; } } return 0; } void check() //检查是否欠费 { Time t=acquire_time(); for ( int i=0;i<Maxbor;i++) { if ((bortime[i].y!=0&&bortime[i].m!=0&&bortime[i].d!=0) &&((t.y-bortime[i].y)*365+(t.m-bortime[i].m)*30+(t.d-bortime[i].d))>Maxday) cout<< "欠费" <<endl; } cout<< "不欠费" <<endl; } void disp() //读出读者信息 { check(); cout << setw(10) << num <<setw(20) << name<< "借书编号:[" ; for ( int i=0;i<Maxbor;i++) if (borbook[i]!= "0" ) cout << borbook[i] << "|" ; cout << "]" <<endl; } }; class RDatabase //读者类库,管理建立读者的个人资料 { private : int top; //读者数组最高下标 Reader read[Maxr]; //读者记录 public : RDatabase() //将reader.txt读到read[]中 { Reader s; top=-1; fstream file( "reader.txt" ,ios::in); //打开一个输入文件 while ( true ) { file.read(( char *)&s, sizeof (s)); if (!file) break ; top++; read[top]=s; } file.close(); //关闭 reader.txt } void clear() //删除所有读者信息 { top=-1; } int addreader( int n, char *na) //添加读者时先查找是否存在 { Reader *p=query(n); if (p==NULL) { top++; read[top].addreader(n,na); return 1; } return 0; } Reader *query( int readerid) //按编号查找 { for ( int i=0;i<=top;i++) if (read[i].getnum()==readerid && read[i].gettag()==0) { return &read[i]; } return NULL; } void disp() //输出所有读者信息 { for ( int i=0;i<=top;i++) read[i].disp(); } void readerdata(); //读者库维护 ~RDatabase() //将read[]写到reader.txt文件中 { fstream file( "reader.txt" ,ios::out); for ( int i=0;i<=top;i++) if (read[i].gettag()==0) file.write(( char *)&read[i], sizeof (read[i])); file.close(); } }; void RDatabase::readerdata() { char choice= '7' ; char rname[rdNlen]; int readerid; Reader *r; while (choice!= '0' ) { cout << "\n\n\t\t\t读 者 维 护\n\n\n\t\t 1: 新 增\n\n\t\t 2: 更改\n\n\t\t 3: 删 除\n\n\t\t 4: 查 找\n\n\t\t 5: 显 示\n\n\t\t 6: 全 删\n\n\t\t 0: 退出" <<endl; cin >> choice; switch (choice) { case '1' : cout << "输入读者编号(1~" <<Maxr<< "):" ; cin >> readerid; cout << "输入读者姓名:" ; cin >> rname; addreader (readerid,rname); break ; case '2' : cout << "输入读者编号(1~" <<Maxr<< "):" ; cin >> readerid; r=query(readerid); if (r==NULL) { cout << " 该读者不存在 " <<endl; break ; } cout << "输入新的姓名:" ; cin >> rname; r->setname(rname); break ; case '3' : cout << " 输入读者编号(1~" <<Maxr<< "):" ; cin >> readerid; r=query(readerid); if (r==NULL) { cout << " 该读者不存在" << endl; break ; } r->delbook(); break ; case '4' : cout << "输入读者编号(1~" <<Maxr<< "):" ; cin >> readerid; r=query(readerid); if (r==NULL) { cout << "该读者不存在" << endl; break ; } r->disp(); break ; case '5' : disp(); break ; case '6' : clear(); break ; default : cout<< "输入错误,请重新输入:" ; } } } //图书类,实现对图书的描述,图书的编号,书名,借出,还入等 class Book { private : bool tag; //删除标记 1:已删 0:未删 string ISBN; //图书编号 char name[bkNlen]; //书名 char author[rdNlen]; //作者名 Genre kind; //图书类型 bool onshelf; //是否在架 1:在架 0:已借 public : Book(){} char *getname() { return name; } //获取书名 char *getauthor(){ return author;} //获取作者 string getkind(); //获取图书类型 string getISBN(){ return ISBN; } //获取图书ISBN bool gettag(){ return tag; } //获取删除标记 void setname( char na[]) //设置书名 { strcpy (name,na); } void setauthor( char au[]) //设置作者名 { strcpy (author,au); } void setkind(Genre g) //设置图书类型 { kind=g; } void delbook(){ tag=1;} //删除图书 void addbook(string s1, char *na, char *au,Genre g) //增加图书 { tag=0; ISBN=s1; strcpy (name,na); strcpy (author,au); kind=g; onshelf=1; } bool borrowbook() //借书操作 (并查找是否在架) { if (onshelf==1) { onshelf=0; return 1; } return 0; } void retbook() //还书操作 { onshelf=1; } void disp() //输出图书 { cout <<setw(10)<<ISBN <<setw(10)<< "《" <<name<< "》" <<setw(10)<< "(" <<author<< ")" <<setw(15)<<getkind() <<setw(10)<<(onshelf==1? "在架" : "已借" ) <<endl; } }; string Book::getkind() //返回图书类型 { switch (kind) { case 0: return "fiction" ; break ; case 1: return "nonfiction" ; break ; case 2: return "periodical" ; case 3: return "biography" ; case 4: return "children" ; default : return "unknow" ; } } bool operator==(Book &b1,Book&b2) //判断两本书的ISBN号是否相等 1-相同 0-不同 { if (b1.getISBN()==b2.getISBN()) return 1; return 0; } bool operator!=(Book &b1,Book&b2) //判断两本书的ISBN号是否不等 1-不同 0-相同 { if (b1.getISBN()!=b2.getISBN()) return 1; return 0; } //图书库类,实现对图书的维护,查找,删除等 class BDatabase { private : int top; //图书数组最高下标 Book book[Maxb]; //图书记录 public : BDatabase() //将book.txt读到book[]中 { Book b; top=-1; fstream file( "book.txt" ,ios::in); while ( true ) { file.read(( char *)&b, sizeof (b)); if (!file) break ; top++; book[top]=b; } file.close(); } void clear() //全删 { top=-1; } int addbook(string s1, char *na, char *au,Genre g) //增加图书 { Book *p=query(s1); if (p==NULL) { top++; book[top].addbook(s1,na,au,g); return 1; } else { cout<< "已存在该ISBN号!请检查输入!" <<endl; return 0; } } Book *query(string bookid) //查找图书 { for ( int i=0;i<=top;i++) if (book[i].getISBN()==bookid &&book[i].gettag()==0) //找到并且未删 { return &book[i]; } return NULL; } void bookdata(); //图书库维护 void disp() { for ( int i=0;i<=top;i++) if (book[i].gettag()==0) book[i].disp(); } ~BDatabase() //将book[]写到book.txt文件中 { fstream file( "book.txt" ,ios::out); for ( int i=0;i<top;i++) if (book[i].gettag()==0) file.write(( char *)&book[i], sizeof (book[i])); file.close(); } }; void BDatabase::bookdata() { char choice= 'w' ; char bname[bkNlen]; char aname[rdNlen]; int bkind; Genre bkind1; string bookid; Book *b; while (choice!= '0' ) { cout << "\n\n\n\t\t\t图 书 维 护 \n" <<endl; cout<< "\t\t1: 新 增\n \t\t2: 更 改\n\t\t3: 删 除\n\t\t4: 查 找\n\t\t5: 显示\n\t\t6: 全 删\n\t\t0: 退 出" <<endl; cin >> choice; switch (choice) { case '1' : cout << "输入图书编号(n-n-n-x):" <<endl; bookid=input_ISBN(); cout << "输入图书书名:" <<endl; cin >>bname; cout<< "输入作者姓名:" <<endl; cin>>aname; cout<< "输入图书类型:\n1-fiction,2-nonfiction,3-periodical,4-biography,5-children" <<endl; cin>>bkind; if (bkind<6&&bkind>0) bkind1=Genre(bkind-1); else bkind1=Genre(5); addbook(bookid,bname,aname,bkind1); break ; case '2' : cout << "输入图书编号(n-n-n-x):" <<endl; cin >> bookid; b=query(bookid); if (b==NULL) { cout << " 该图书不存在 " <<endl; break ; } cout << "输入新的书名:" <<endl; cin >>bname; b->setname(bname); cout << "输入新的作者名:" <<endl; cin >>aname; b->setauthor(aname); cout << "输入新的类型:\n1-fiction,2-nonfiction,3-periodical,4-biography,5-children" <<endl; cin >>bkind; if (bkind<6&&bkind>0) bkind1=Genre(bkind-1); b->setkind(bkind1); break ; case '3' : cout << "输入图书编号(n-n-n-x):" <<endl; cin >> bookid; b=query(bookid); if (b==NULL) { cout << "该图书不存在" << endl; break ; } b->delbook(); break ; case '4' : cout << "输入图书编号(n-n-n-x):" <<endl; cin >> bookid; b=query(bookid); if (b==NULL) { cout << "该图书不存在" << endl; break ; } b->disp(); break ; case '5' : disp(); break ; case '6' : clear(); break ; case '0' : break ; default : cout<< "输入错误,请重新输入:" ; } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/XIXI_0921/article/details/42422727