服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现小型图书管理系统

C++实现小型图书管理系统

2022-10-18 12:59GroovRain C/C++

这篇文章主要为大家详细介绍了C++实现小型图书管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现小型图书管理系统的具体代码,供大家参考,具体内容如下

因为课程设计的原因,需要实现一个小型图书管理系统

包含功能:

问题描述:

设计一个系统,对图书信息进行管理,信息描述:有关该系统基本信息的描述,如:图书名称、图书编号、单价、作者、存在状态、借书人姓名、性别、学号等。

基本要求:

基本功能:

1、新进图书基本信息的输入。
2、图书基本信息的查询。
3、对撤消图书信息的删除。
4、为借书人办理注册。
5、办理借书手续(非注册会员不能借书)。
6、办理还书手续。
7、统计图书库存、已借出图书数量。

需要创建三个文本文件:record.txt  book.txt reader.txt

operating.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
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
#include <iostream>
#include <fstream>
#include <string>
#include <time.h> 
#include<sstream>
#include<vector>
#include <iomanip>
 
using namespace std;
 
 
int all_stock = 0;
int out_stock = 0;
int times=0;
void outData(vector<string> res,int n)  // n为txt中 每行数据个数
{
    for(int i=0;i<res.size();i+=n){
        for(int j=0;j<n;j++)
            cout<<setw(12)<<res[i+j]<<" ";
        
        cout<<endl;
    }
}
 
void BookEntry()
{
    double price;
    string bookname,writer;
    fstream out;
    out.open("book.txt",ios::app);
    if(!out)
    {
        cerr<<"打开文件失败!"<<endl;
    }
 
    time_t tt = time(NULL);//这句返回的只是一个时间cuo
    
    cout<<"请输入书籍名称"<<endl;
    cin>>bookname;
    cout<<"请输入书籍作者"<<endl;
    cin>>writer;
    cout<<"请输入书籍价格"<<endl;
 
 
    while(! (cin>>price)  || price <= 0 )
    {
        cin.clear();
        cin.ignore(100,'\n');
        cout<<"请输入正确的价格"<<endl;
    }
    
    out<<tt<<" "<<bookname<<" "<<writer<<" "<<price<<" "<<"0"<<"\n";
    
    out.close();
    
}
 
 
void BookMes()
{
    fstream in;
    string line;
    //用于存放分割后的字符串 
    vector<string> res;
    string temp; //暂存字符串
    
    in.open("book.txt",ios::in);
    if(!in)
    {
        cerr<<"打开文件失败!"<<endl;
    }
    all_stock = 0;
    while(getline(in,line))
    {
        all_stock++;
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        while(input>>temp)
        {
            
            res.push_back(temp);
        }
    }
    // 0 书籍编号 1 书籍名称 2作者 3价格 4书籍状态
    //输出res 
    cout<<endl<<setw(12)<<"书籍编号"<<" "<<setw(12)<<"书籍名称"<<" "<<setw(12)<<"作者"<<" "<<setw(12)<<"价格"<<" "<<setw(12)<<"在馆0,不在1"<<"\n";
    outData(res,5);
    in.close();
}
 
 
void DelBook()
{
    string del_book;
    string line;
    vector<string>res;
    string temp;
    bool flag=false;
 
 
    fstream in;
    in.open("book.txt",ios::in);
    if(!in)
    {
        cerr<<"打开错误文件"<<endl;
    }
 
 
    cout<<"请输入需要删除的图书ID"<<endl;
    cin>>del_book;
    
        while(getline(in,line))
    {
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        times=0;
        while(input>>temp)
        {
            if(del_book == temp && times==0)
            {
                
                for(int i=0;i<3;i++)  //因为一共五个 第一个temp已经是del_book 所以这里取得是四个
                {
                    input>>temp;
                }
                input>>temp;
                if(temp != "0")
                {
                    cout<<"书籍状态不对";
                    in.close();
                    return ;
                }
                flag=true;
                cout<<"\n找到了喔,应该删除成功了\n";
                continue;
            }
            res.push_back(temp);
            times++;
            
        }
    }
    
    //outData(res,5);
    in.close();
    
    if(!flag)
    {
        cout<<"\n错误的书籍ID\n";
        return ;
    }
    fstream out;
 
 
    out.open("book.txt",ios::out);
    if(!out)
    {
        cerr<<"打开文件失败!"<<endl;
    }
    
    for(int j=0;j<res.size();j+=5)
    {
        line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "\n";
        out<<line;
    }
    out.close();
    
}
 
void ReaderEntry()
{
    
    string readername,sex_str;
    int sex;
    fstream out;
 
 
    out.open("reader.txt",ios::app);
    if(!out)
    {
        cerr<<"打开文件失败!"<<endl;
    }
 
    time_t readerid = time(NULL);//这句返回的只是一个时间cuo
    
    cout<<"请输入读者姓名"<<endl;
    cin>>readername;
    
    do
    {
        cout<<"请输入读者性别:0为女,1为男"<<endl;
        while(! (cin>>sex) )
        {
            cin.clear();
            cin.ignore(100,'\n');
            cout<<"请输入正确的0或1"<<endl;
        }
    }while(sex != 0 && sex!=1);
 
 
    if(sex == 1)
    {
        sex_str = "男";
    }else if (sex == 0){
        sex_str = "女";
    }else{
        out.close();
        return ;
    }
 
    out<<readerid<<" "<<readername<<" "<<sex_str<<"\n";
    
    out.close();
    
}
/*读者信息*/
void ReaderMes()
{
    fstream in;
    string line;
    //用于存放分割后的字符串 
    vector<string> res;
    string temp; //暂存字符串
    in.open("reader.txt",ios::in);
    if(!in)
    {
        cerr<<"打开文件失败!"<<endl;
    }
    
    while(getline(in,line))
    {
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        while(input>>temp)
        res.push_back(temp);
    }
    // 0读者学号 1读者姓名 2读者性别
    //输出res 
    cout<<endl<<setw(12)<<"读者编号"<<" "<<setw(12)<<"读者"<<" "<<setw(12)<<"性别"<<"\n";
    outData(res,3);
    in.close();
}
 
 
/* 借阅书籍 */
void BorrowBook()
{
    string book[5];
    string readerid;
    string readername;
    string line;
    vector<string>res; //取书籍状况,并且更新
 
 
    string temp;
    bool flag_book = false; //用于判断书籍是否存在  读者是否存在
    bool flag_reader = false;
 
 
    /* 取book的图书情况,并判断是否在馆*/
    fstream in;
    in.open("book.txt",ios::in);
    if(!in)
    {
        cerr<<"打开错误文件"<<endl;
    }
 
    cout<<"请输入需要借的图书ID"<<endl;
    cin>>book[0];
    
    while(getline(in,line))
    {
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        times=0;
        while(input>>temp)
        {
            if(book[0] == temp && times ==0)
            {
                res.push_back(temp);
                for(int i=0;i<3;i++)  //从书籍名称开始取,一直取到价钱
                {
                    input>>temp; //读取了书籍编号,要及时写入res,以后要写进文本
                    book[1+i]=temp;
                    res.push_back(temp);
                }
                input>>temp;  //取书籍状态,如果0在馆 如果1不在馆
                if(temp == "0")
                {
                    book[4]="1";
                    temp="1";
                    res.push_back(temp);
                    flag_book=true;
                }else{
                    cout<<"\n书籍不在馆\n";
                    in.close();
                    return ;
                }
                continue;  //继续取
            }
            res.push_back(temp);
            times++;
            
        }
    }
 
    in.close();
    if(!flag_book)
    {
        cout<<"错误的书籍ID"<<endl;
        return ;
    }
    
    in.open("reader.txt",ios::in);
    if(!in)
    {
        cerr<<"打开错误文件"<<endl;
    }
    cout<<"\n请输入读者ID\n";
    cin>>readerid;
 
 
    while(getline(in,line))
    {
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        times=0;
        while(input>>temp)
        {
            if(readerid == temp && times==0)
            {
                input>>temp;
                readername=temp;
                flag_reader=true;
                break;
 
 
            }
            times++;
            
        }
    }
    if(!flag_reader)
    {
        cout<<"错误的读者ID"<<endl;
        in.close();
        return ;
    }
 
    in.close();
       
    fstream out;
    out.open("record.txt",ios::app);
    if(!out)
    {
        cerr<<"打开错误文件"<<endl;
    }
    line = book[0] + " " + book[1] + " " + readername + '\n';
    out<<line;
    cout<<"\n办理借书成功\n";
    out.close();
    out.open("book.txt",ios::out);
    if(!out)
    {
        cerr<<"打开文件失败!"<<endl;
    }
    
    for(int j=0;j<res.size();j+=5)
    {
        line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "\n";
        out<<line;
    }
    out.close();
}
 
void BorrowMes()
{
    fstream in;
    string line;
    //用于存放分割后的字符串 
    vector<string> res;
    string temp; //暂存字符串
    in.open("record.txt",ios::in);
    if(!in)
    {
        cerr<<"打开文件失败!"<<endl;
    }
    out_stock=0;
    while(getline(in,line))
    {
        out_stock++;
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        while(input>>temp)
        res.push_back(temp);
    }
    // 0书籍编号 1书籍名称 2读者姓名
    //输出res 
    cout<<endl<<setw(12)<<"书籍编号"<<" "<<setw(12)<<"书籍名称"<<" "<<setw(12)<<"读者"<<"\n";
    outData(res,3);
    
    in.close();
}
 
void RtnBook()
{
    string rtn_book;
    string line;
    vector<string>res;
    string temp;
    bool flag=false;
 
 
    fstream in;
    in.open("record.txt",ios::in);  //先打开record 查看是否有借这本书
    if(!in)
    {
        cerr<<"打开错误文件"<<endl;
    }
 
    cout<<"请输入需要归还的书籍ID"<<endl;
    cin>> rtn_book;
        
    while(getline(in,line))
    {
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        times=0;
        while(input>>temp)
        {
            if(rtn_book == temp && times==0) //如果有的话
            {
                flag=true;
                
                for(int i=0;i<2;i++)  //因为一共三个 第一个temp已经是del_book 所以这里取得是两个
                {
                    input>>temp;// 将删除的东西不输出到向量中
                }
                continue;
            }
            res.push_back(temp);
            times++;
            
        }
    }
    
    //outData(res,3);
    in.close();
    if(!flag)
    {
        cout<<"该图书不存在或者没有被外借"<<endl;
        return ;
    }
 
    fstream out;
 
    out.open("record.txt",ios::out); //record已经删除成功
    if(!out)
    {
        cerr<<"打开文件失败!"<<endl;
    }
    
    for(int j=0;j<res.size();j+=3)
    {
        line = res[j] + " " + res[j+1] + " " + res[j+2]  + "\n";
        out<<line;
    }
    out.close();
 
    vector<string>res_book;
 
    in.open("book.txt",ios::in); //开始取 被修改的书籍
    if(!in)
    {
        cerr<<"打开错误文件"<<endl;
    }    
    
    while(getline(in,line))
    {
        //cout<<line<<endl;
        //将字符串读到input中 
        stringstream input(line); //将line切割 通过input存入temp,然后存入res中
        times=0;
        while(input>>temp)
        {
            if(rtn_book == temp && times==0)
            {
                res_book.push_back(temp);
                for(int i=0;i<3;i++)  //因为一共五个 第一个temp已经是rtn_book 所以这里取得是四个
                {
                    input>>temp;
                    res_book.push_back(temp);
                }
                input>>temp;//最后一个取得是书籍状态,需要修改书籍状态
                temp = "0";
                res_book.push_back(temp);
                continue;
            }
            res_book.push_back(temp);
            times++;
        }
    }
    
    //outData(res,5);
    in.close();
    
    out.open("book.txt",ios::out); //再存入文本中;
    if(!out)
    {
        cerr<<"打开文件失败!"<<endl;
    }
    
    for(int j=0;j<res_book.size();j+=5)
    {
        line = res_book[j] + " " + res_book[j+1] + " " + res_book[j+2] + " " + res_book[j+3] + " " + res_book[j+4] + "\n";
        out<<line;
    }
    out.close();
 
 
    cout<<"\n找到了喔,应该还书成功了\n";
}
 
 
void CountBook()
{    
    cout<<"\n图书馆书籍情况";
    BookMes();
    cout<<"图书馆一共有:"<<all_stock<<" 本书\n\n\n";
    cout<<"\n图书馆书籍外借情况";
    BorrowMes();
    cout<<"图书馆目前外借:"<<out_stock<<" 本书\n\n";
    cout<<"\n\n图书馆当前在馆书籍还有:"<<all_stock - out_stock<<" 本书\n";
}

main.cpp的主函数

?
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
#include "operating.h"
 
int main()
{
    int order;
    do
    {
        order = -1;
        cout<<"\n";
        cout<<"----------------------------------------------------------\n";
        cout<<"| 1. 图书信息录入    2. 图书信息查询    3. 图书信息删除  |\n";
        cout<<"| 4. 读者办理注册    5. 读者信息查询    6. 办理借书手续  |\n";
        cout<<"| 7. 办理还书手续    8  已借出图书      9.统计图书库存  |\n";
        cout<<"|                                        按 \"0\"退出    |\n";
        cout<<"----------------------------------------------------------\n";
        cout<<"  请输入相应序号进行相应操作:";
        cin>>order;
        cin.clear();//清除缓冲区中后面的字符
        cin.ignore(100,'\n');
 
        switch(order)
        {
        case 1:
            BookEntry();
            break;
        case 2:
            BookMes();
            break;
        case 3:
            DelBook();
            break;
        case 4:
            ReaderEntry();
            break;
        case 5:
            ReaderMes();
            break;
        case 6:
            BorrowBook();
            break;
        case 7:
            RtnBook();
            break;
        case 8:
            BorrowMes();
            break;
        case 9:
            CountBook();
            break;
        case 0:
            break;
        default:
            cout<<"错误的命令行"<<endl;
            break;
        }
        
    }while(order != 0);
 
    system("pause");
    return 0;
 
    
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/hxfghgh/article/details/80258230

延伸 · 阅读

精彩推荐