本文实例为大家分享了图书管理信息系统的设计与实现,供大家参考,具体内容如下
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
#include<stdio.h>
#include<string.h>
#define BookSize 100
#define BLHum 50
#define BLHnum 50
#define RRnum 50
/*主数据库文件*/
typedef struct {
int bno;
char bname[21];
int namenext;
char author[9];
int authnext;
char press[11];
int prenext;
char sortno[4];
int storenum;
int borrownum;
}BookRecType;
typedef struct {
BookRecType BookDbase[BookSize];
int len;
}BookDbaseFile;
/*书号索引文件*/
typedef struct {
int bno;
int RecNo;
}BidxRecType;
typedef struct {
BidxRecType BnoIdx[BookSize];
int len;
}BnoIdxFile;
/*书名链头索引文件*/
typedef struct {
char bname[21];
int lhead;
int RecNum;
}BNRecType;
typedef struct {
BNRecType LHFrecl[BLHum];
int len1;
}LHFile1;
/*作者链头索引文件*/
typedef struct {
char author[9];
int lhead;
int RecNum;
}BARecType;
typedef struct {
BARecType LHFrec2[BLHnum];
int len2;
}LHFile2;
/*出版社链头索引问件*/
typedef struct {
char press[11];
int lhead;
int RecNum;
}BPRecType;
typedef struct {
BPRecType LHFrec3[BLHnum];
int len3;
}LHFile3;
/*读者文件*/
typedef struct {
int rno;
char name[8];
int bn1;
int bn2;
}RRecType;
typedef struct {
RRecType ReadRec[RRnum];
int len;
}ReadFile;
/*借还书文件*/
typedef struct {
int rno;
int bno;
char date1[9];
char date2[9];
}BbookRecType;
typedef struct {
BbookRecType Bbook[BookSize];
int len;
}BbookFile;
/*追加一条图书记录建立相关文件*/
BookDbaseFile AppeDBaseRec(BookDbaseFile df)
{
int i;
printf ( "书号 书名 作者名 出版社 分类 藏书量\n" );
scanf ( "%d%s" , df.BookDbase[i].bno, df.BookDbase[i].bname);
scanf ( "%s%s" , df.BookDbase[i].author, df.BookDbase[i].press);
scanf ( "%s %d" , df.BookDbase[i].sortno, &df.BookDbase[i].storenum);
df.BookDbase[i].borrownum = 0;
return df;
}
/*修改书号索引表的设计*/
BnoIdxFile ChangeBnoIdxF(BookDbaseFile df, BnoIdxFile bif)
{
int i, j, k = 1;
i = df.len;
j = bif.len;
while (j >= 1)
{
if (df.BookDbase[i].bno>bif.BnoIdx[j].bno)
{
k = j + 1;
break ;
}
j--;
}
if (bif.len>0)
for (j = bif.len; j >= k; j--)
bif.BnoIdx[j + 1] = bif.BnoIdx[j];
bif.BnoIdx[k].bno = df.BookDbase[i].bno;
bif.BnoIdx[k].RecNo = i;
bif.len++;
return bif;
}
/*修改书名索引以及书名链头索引的设计*/
LHFile1 ChangeLinkHeadF1(BookDbaseFile *df, LHFile1 lhf1)
{
int i, j, k, m;
char sm[21];
i = df->len;
strcpy (sm , df->BookDbase[i].bname);
j = 1;
k = 0;
while (j <= lhf1.len1)
{
if ( strcmp (sm, lhf1.LHFrecl[j].bname) == 0)
{
k = j;
break ;
}
j++;
}
if (k != 0)
{
df->BookDbase[i].namenext = lhf1.LHFrecl[k].lhead;
lhf1.LHFrecl[k].lhead = i;
lhf1.LHFrecl[k].RecNum++;
}
else
{
m = ++lhf1.len1;
df->BookDbase[i].namenext = 0;
lhf1.LHFrecl[m].lhead = i;
lhf1.LHFrecl[m].RecNum = 1;
strcpy (lhf1.LHFrecl[m].bname, sm);
}
return lhf1;
}
/*修改作者索引以及作者链头索引表的设计*/
LHFile2 ChangeLinkHeadF2(BookDbaseFile *df, LHFile2 lhf2)
{
int i, j, k, m;
char zz[9];
i = df->len;
strcpy (zz, df->BookDbase[i].author);
j = 1;
k = 0;
while (j <= lhf2.len2)
{
if ( strcmp (zz, lhf2.LHFrec2[j].author) == 0)
{
k = j;
break ;
}
j++;
}
if (k != 0)
{
df->BookDbase[i].authnext = lhf2.LHFrec2[k].lhead;
lhf2.LHFrec2[k].lhead = i;
lhf2.LHFrec2[k].RecNum = 1;
strcpy (lhf2.LHFrec2[m].author, zz);
}
return lhf2;
}
/*修改出版社的索引以及出版社链头索引表的设计*/
LHFile3 ChangeLinkHeadF3(BookDbaseFile *df, LHFile3 lhf3)
{
int i, j, k, m;
char cbs[11];
i = df->len;
strcpy (cbs, df->BookDbase[i].press);
j = 1;
k = 0;
while (j <= lhf3.len3)
{
if ( strcmp (cbs, lhf3.LHFrec3[j].press) == 0)
{
k = j;
break ;
}
j++;
}
if (k != 0)
{
df->BookDbase[i].prenext = lhf3.LHFrec3[k].lhead;
lhf3.LHFrec3[k].lhead = i;
lhf3.LHFrec3[k].RecNum++;
}
else
{
m = ++lhf3.len3;
df->BookDbase[i].prenext = 0;
lhf3.LHFrec3[m].lhead = i;
lhf3.LHFrec3[m].RecNum = i;
strcpy (lhf3.LHFrec3[m].press, cbs);
}
return lhf3;
}
/*用二分查找实现书号查询算法*/
int BinSearch(BnoIdxFile bif, int key)
{
int low, high, mid;
low = 1;
high = bif.len;
while (low <= high)
{
mid = (low + high) / 2;
if (key == bif.BnoIdx[mid].bno)
return bif.BnoIdx[mid].RecNo;
else
if (key<bif.BnoIdx[mid].bno)
high = mid - 1;
else
low = mid + 1;
}
return 0;
}
/*按书名查询算法的设计*/
int BnameFind(LHFile1 lhf1, char key[])
{
int i, k = 0;
for (i = 1; i<=lhf1.len1;i++)
{
if ( strcmp (key, lhf1.LHFrecl[1].bname) == 0)
{
k = lhf1.LHFrecl[i].lhead;
break ;
}
}
return k;
}
/*按作者查询算法的设计*/
int BauthFind(LHFile2 lhf2, char key[])
{
int i, k = 0;
for (i = 1; i <= lhf2.len2; i++)
{
if ( strcmp (key, lhf2.LHFrec2[i].author) == 0)
{
k = lhf2.LHFrec2[i].lhead;
break ;
}
}
return k;
}
/*按出版社查询算法的设计*/
int BnameFind(LHFile3 lhf3, char key[])
{
int i, k = 0;
for (i = 1; i <= lhf3.len3; i++)
{
if ( strcmp (key, lhf3.LHFrec3[i].press) == 0)
{
k = lhf3.LHFrec3[i].lhead;
break ;
}
}
return k;
}
/*输出一条图书主数据库记录的设计*/
void ShowRec(BookDbaseFile df, int i)
{
printf ( "书号 书名 作者名 出版社 分类号 可借数\n" );
printf ( "==============================================\n" );
printf ( "%d%12s" , df.BookDbase[i].bno, df.BookDbase[i].bname);
printf ( "%8s%12s" , df.BookDbase[i].author, df.BookDbase[i].press);
printf ( "%6s" , df.BookDbase[i].sortno);
printf ( "%4d\n" , df.BookDbase[i].storenum - df.BookDbase[i].borrownum);
printf ( "==============================================\n" );
}
/*图书查询控制程序设计*/
void SearchBook(BookDbaseFile df, BnoIdxFile bif, LHFile1 f1, LHFile2 f2, LHFile3 f3)
{
char sm[21], zz[9],cbs[11];
int i, k, choose = 1;
int sh;
while (choose >= 1 && choose <= 5)
{
printf ( "图书查询子系统\n" );
printf ( "------------------\n" );
printf ( "1.书 号 2.书 名\n" );
printf ( "3.作 者 4.出版社\n" );
printf ( "5.退 出 查 询" );
printf ( "------------------\n" );
printf ( " 请 用 户 选 择:" );
scanf ( "%d" , &choose);
switch (choose)
{
case 1:
printf ( "输入学号:" );
scanf ( "%d" , &sh);
k = BinSearch(bif, sh);
if (k == 0) {
printf ( "没有要检查的图书,请检查是否输入有错\n" );
break ;
}
ShowRec(df, k);
break ;
case 2:
printf ( "输入书名:" );
scanf ( "%" , &sm);
k = BnameFind(f1, sm);
if (k == 0) {
printf ( "没有要检查的图书,请检查是否输入有错\n" );
break ;
}
for (i = k; i; i = df.BookDbase[i].namenext)
ShowRec(df, i);
break ;
case 3:
printf ( "输入作者名:" );
scanf ( "%s" , &zz);
k = BauthFind(f2, zz);
if (k == 0) {
printf ( "没有要检查的图书,请检查是否输入有错\n" );
break ;
}
for (i = k; i; i = df.BookDbase[i].authnext)
ShowRec(df, i);
break ;
case 4:
printf ( "输入出版社:" );
scanf ( "%s" , &cbs);
k = BnameFind(f3,cbs);
if (k == 0) {
printf ( "没有要检查的图书,请检查是否输入有错\n" );
break ;
}
for (i = k; i; i = df.BookDbase[i].prenext)
ShowRec(df, k);
break ;
case 5:
return ;
}
}
}
/*借还书处理算法*/
void BorrowBook(BookDbaseFile *bf, BnoIdxFile bif, BbookFile *bbf, ReadFile *rf)
{
char jyrq[9];
int sh, dzh;
int i, j, k = 0;
printf ( "输入读者号 书号 借阅日期\n" );
scanf ( "%d%d%s" , &dzh, &sh, jyrq);
for (i = 1; i <= rf->len; i++)
{
if (dzh == rf->ReadRec[i].rno)
{
k = i;
break ;
}
}
if (k == 0)
{
printf ( "非法读者!\n" );
return ;
}
if (rf->ReadRec[k].bn2 >= rf->ReadRec[k].bn1)
{
printf ( "书已借满!\n" );
return ;
}
j = BinSearch(bif, sh);
if (j == 0)
{
printf ( "非法书号!" );
return ;
}
if (bf->BookDbase[j].borrownum >= bf->BookDbase[j].storenum)
{
printf ( "图书已借出\n" );
return ;
}
i = ++bbf->len;
bbf->Bbook[i].rno = dzh;
bbf->Bbook[i].bno = sh;
strcpy (bbf->Bbook[i].date1, jyrq);
rf->ReadRec[k].bn2++;
bf->BookDbase[j].borrownum++;
printf ( "借书成功!\n" );
}
/*还书处理算法的设计*/
void BackBook(BookDbaseFile *bf,BnoIdxFile bif ,BbookFile *bbf, ReadFile *rf)
{
char hsrq[8];
int sh, dzh;
int i, j, k = 0, m = 0;
printf ( "读者号 书号 还书日期\n" );
scanf ( "%d%d%s" , &dzh, &sh, hsrq);
for (i = 1; i <= rf->len; i++)
{
if (dzh == rf->ReadRec[i].rno)
{
k = i;
break ;
}
}
if (k == 0)
{
printf ( "非法读者!\n" );
return ;
}
for (i = 1; i <= bbf->len; i++)
if (sh == bbf->Bbook[i].bno)
{
m = i;
break ;
}
if (m == 0)
{
printf ( "非法书号!\n" );
return ;
}
j = BinSearch(bif, sh);
if (j == 0)
{
printf ( "非法书号!\n" );
return ;
}
rf->ReadRec[k].bn2--;
bf->BookDbase[j].borrownum--;
strcpy (bbf->Bbook[m].date2, hsrq);
printf ( "还书成功!\n" );
}
/*读者管理子系统*/
ReadFile ReaderManage(ReadFile rf)
{
int i;
char yn = 'y' ;
i = ++rf.len;
while (yn == 'y' || yn == 'Y' )
{
printf ( "输入读者号 读者名 可借图书数\n" );
scanf ( "%d %s" , &rf.ReadRec[i].rno, rf.ReadRec[i].name);
scanf ( "%d" , &rf.ReadRec[i].bn1);
rf.ReadRec[i].bn2 = 0;
printf ( "继续输入吗?y/n:" );
getchar ();
scanf ( "%c" ,&yn);
}
rf.len=i-1;
return rf;
}
/*写各类文件*/
void writefile(BookDbaseFile bf, BnoIdxFile bif, LHFile1 f1, LHFile2 f2, LHFile3 f3, ReadFile rf, BbookFile bbf)
{
FILE *fp;
int i;
//图书主文件
fp = fopen ( "book" , "wb" );
for (i = 1; i <= bf.len; i++)
fwrite (&bf.BookDbase[i], sizeof (BookRecType), 1, fp);
fclose (fp);
//图书索引文件
fp = fopen ( "bidx" , "wb" );
for (i = 1; i <= bif.len; i++)
fwrite (&bif.BnoIdx[i], sizeof (BidxRecType), 1, fp);
fclose (fp);
//书名索引链头文件
fp = fopen ( "nidx" , "wb" );
for (i = 1; i <= f1.len1; i++)
fwrite (&f1.LHFrecl[i], sizeof (BNRecType), 1, fp);
fclose (fp);
//作者索引链头文件
fp = fopen ( "aidx" , "wb" );
for (i = 1; i <= f2.len2; i++)
fwrite (&f2.LHFrec2[i], sizeof (BARecType), 1, fp);
fclose (fp);
//出版社索引链头文件
fp = fopen ( "pidx" , "wb" );
for (i = 1; i <= f3.len3; i++)
fwrite (&f3.LHFrec3[i], sizeof (BPRecType), 1, fp);
fclose (fp);
//读者文件
fp = fopen ( "read" , "wb" );
for (i = 1; i <= rf.len; i++)
fwrite (&rf.ReadRec[i], sizeof (RRecType), 1, fp);
fclose (fp);
//借还书文件
fp = fopen ( "bbff" , "wb" );
for (i = 1; i <= bbf.len; i++)
fwrite (&bbf.Bbook[i], sizeof (BbookRecType), 1, fp);
fclose (fp);
}
/*读各类文件*/
void readfile(BookDbaseFile *bf, BnoIdxFile *bif, LHFile1 *f1, LHFile2 *f2, LHFile3 *f3, ReadFile *rf, BbookFile *bbf)
{
FILE *fp;
int i;
//读图书主文件
fp = fopen ( "book" , "rb" );
i = 1;
while (! feof (fp))
{
fread (&bf->BookDbase[i], sizeof (BookRecType), 1, fp);
i++;
if ( feof (fp)) break ;
}
bf->len = i - 2;
fclose (fp);
//读书好索引文件
fp = fopen ( "bidx" , "rb" );
i = 1;
while (! feof (fp))
{
fread (&bif->BnoIdx[i], sizeof (BidxRecType), 1, fp);
i++;
}
bif->len = i - 2;
fclose (fp);
//读书名索引文件
fp = fopen ( "nidx" , "rb" );
i = 1;
while (! feof (fp))
{
fread (&f1->LHFrecl[i], sizeof (BNRecType), 1, fp);
i++;
}
f1->len1 = i - 2;
fclose (fp);
//读作者索引文件
fp = fopen ( "aidx" , "rb" );
i = 1;
while (! feof (fp))
{
fread (&f2->LHFrec2[i], sizeof (BARecType), 1, fp);
i++;
}
f2->len2 = i - 2;
fclose (fp);
//读出版社索引链头文件
fp = fopen ( "pidx" , "rb" );
i = 1;
while (! feof (fp))
{
fread (&f3->LHFrec3[i], sizeof (BPRecType), 1, fp);
i++;
}
f3->len3 = i - 2;
fclose (fp);
//读读者文件
fp = fopen ( "read" , "rb" );
i = 1;
while (! feof (fp))
{
fread (&rf->ReadRec[i], sizeof (RRecType), 1, fp);
i++;
}
rf->len = i - 2;
fclose (fp);
//读借还书文件
fp = fopen ( "bbff" , "rb" );
i = 1;
while (! feof (fp))
{
fread (&bbf->Bbook[i], sizeof (BbookRecType), 1, fp);
i++;
}
bbf->len = i - 2;
fclose (fp);
}
/*主控菜单*/
int main( void )
{
int j, m, k = 1;
char xz = 'n' ;
BookDbaseFile bf;
BnoIdxFile bif;
LHFile1 f1;
LHFile2 f2;
LHFile3 f3;
ReadFile rf;
BbookFile bbf;
while (k <= 5)
{
printf ( "图书管理系统\n" );
printf ( "============\n" );
printf ( " 1.系统维护 \n" );
printf ( " 2.读者管理 \n" );
printf ( " 3.图书管理 \n" );
printf ( " 4.图书流通 \n" );
printf ( " 5.退出系统 \n" );
printf ( "============\n" );
printf ( " 请选择1-5:" );
scanf ( "%d" , &k);
switch (k)
{
case 1:
printf ( "系统维护\n" );
printf ( "--------\n" );
printf ( "1.初始化\n" );
printf ( "2. 读 盘\n" );
printf ( "--------\n" );
printf ( "请选择:\n" );
scanf ( "%d" , &m);
switch (m)
{
case 1:
printf ( "初始化只能做一次,需慎重!初始化吗?y/n:" );
getchar ();
scanf ( "%c" , &xz);
if ( xz == 'y' || xz == 'Y' )
{
bf.len = bif.len = f1.len1 = f2.len2 = 0;
f3.len3 = rf.len = bbf.len = 0;
}
break ;
case 2:
readfile(&bf, &bif, &f1, &f2, &f3, &rf, &bbf);
break ;
}
break ;
case 2:
ReaderManage(rf);
break ;
case 3:
printf ( "图书管理子系统\n" );
printf ( "--------------\n" );
printf ( "1.图书信息输入\n" );
printf ( "2.图书信息查询\n" );
printf ( "--------------\n" );
printf ( " 请 选 择 :" );
scanf ( "%d" , &j);
if (j == 1)
{
char yn = 'y' ;
int k = 1;
while (yn = 'y' || yn == 'Y' )
{
bf = AppeDBaseRec(bf);
bif = ChangeBnoIdxF(bf, bif);
f1 = ChangeLinkHeadF1(&bf, f1);
f2 = ChangeLinkHeadF2(&bf, f2);
f3 = ChangeLinkHeadF3(&bf, f3);
ShowRec(bf, k);
k++;
printf ( "继续输入吗?y/n:" );
getchar ();
scanf ( "%c" , &yn);
}
}
else
SearchBook(bf, bif, f1, f2, f3);
break ;
case 4:
printf ( "图书流通子系统\n" );
printf ( "--------------\n" );
printf ( " 1. 借书处理 \n" );
printf ( " 2. 还书处理 \n" );
printf ( "--------------\n" );
printf ( " 请 选 择 : \n" );
scanf ( "%d" , &j);
if (j == 1)
BorrowBook(&bf, bif, &bbf, &rf);
else if (j == 2)
BackBook(&bf, bif, &bbf, &rf);
break ;
case 5:
printf ( "系统正在写盘,请稍后......\n" );
writefile(bf, bif, f1, f2, f3, rf, bbf);
printf ( "再见!\n" );
return 0;
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/qq_33735635/article/details/73743468