服务器之家

服务器之家 > 正文

C++实现图书管理系统最新版

时间:2021-11-16 15:20     来源/作者:名名名名

图书管理系统设计,供大家参考,具体内容如下

一、问题描述及功能要求

(1)图书信息录入功能(图书信息用文件保存)

(2)图书信息浏览功能

(3)查询和排序功能:(至少一种查询方式)

.按书名查询
.按作者名查询

(4)图书信息的删除与修改

二、代码实现 带有注释

废话不说,直接代码,欢迎指正。
大家CV可能有不兼容的情况,可以滴滴,尽可能解决问题地回复。

?
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
#include<iostream>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include <cstring>
#include<windows.h>
#include<vector>
#define BOOKMAXREPERTORY 99
using namespace std;
class Common{
public:
    int id;
    char title[66];
    char author[66];
    Common(){
    }
    Common(int _id,char _title[],char _author[]){
        id = _id;
        strcpy(title , _title);
        strcpy(author , _author);
    }
};
 
class Book:public Common{
public:
    char publisher[66];
    int pageNumber;
    Book(){}
    Book(int _id,char _title[],char _author[],char _publisher[],char _iSBNNumber[],int _pageNumber){
        Common(_id,_title,_author);
        strcpy(publisher,_publisher);
        pageNumber = _pageNumber;
    }
};
 
class MediaLibraryManageSystem{
public:
 
    MediaLibraryManageSystem(){
        nowBookTotal = 0;
    }
 
    void _run(){
        readDataByFile();
        printf("程序加载中");
        for (int i = 0; i < 10; i++) {
            Sleep(100);    /* windows 使用Sleep,参数为毫秒 */
            printf(".");
            fflush(stdout);//强制刷新缓存,输出显示
        }
        printf("\n");
        system("cls");
        int cmd;
        while(true){
            cout<<home_menu<<endl;
            cout<<"请输入你需要执行的命令序号:";cin>>cmd;
            if(cmd == 0){
                _exit();
            }
            system("cls");
            switch(cmd){
                case 1:
                    addBook();
                    break;
                case 2:
                    queryGoods();
                    break;
                case 3:
                    showGoods();
                    break;
                case 4:
                    updateGoods();
                    break;
                case 5:
                    deleteGoods();
                    break;
                default:
                    cout<<"输入的指令有误!请重新输入!"<<endl;
            }
            system("pause");
            system("cls");
 
        }
    }
 
 
 
 
 
private:
 
    const string home_menu =
    "\n********************************** 图书管理系统 **********************************\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "*                                     1.添加                                       *\n"
    "*                                                                                  *\n"
    "*                                     2.查询                                       *\n"
    "*                                                                                  *\n"
    "*                                     3.显示图书库                                 *\n"
    "*                                                                                  *\n"
    "*                                     4.修改图书库                                 *\n"
    "*                                                                                  *\n"
    "*                                     5.删除                                       *\n"
    "*                                                                                  *\n"
    "*                                     0.退出                                       *\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "************************************************************************************\n";
 
    const string query_goods_menu =
    "\n************************************ 图书查询 ************************************\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "*                                    1.按标题查询                                  *\n"
    "*                                                                                  *\n"
    "*                                    2.按编号查询                                  *\n"
    "*                                                                                  *\n"
    "*                                    0.退出                                        *\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "************************************************************************************\n";
 
    Book bookList[BOOKMAXREPERTORY];
 
    int nowBookTotal;
 
    ///添加图书
    void addBook(){
        Book book;
        cout<<"请输入编号:";cin>>book.id;
        cout<<"请输入标题:";cin>>book.title;
        cout<<"请输入作者:";cin>>book.author;
        cout<<"请输入出版社:";cin>>book.publisher;
        cout<<"请输入页数:";cin>>book.pageNumber;
        if(getBookById(book.id) != -1){
            cout<<"添加失败! 添加编号重复! 请重新添加!"<<endl;
        }else if(nowBookTotal == BOOKMAXREPERTORY){
            cout<<"添加失败! 图书库已满!"<<endl;
        }else{
            bookList[nowBookTotal ++] = book;
        }
    }
 
    /*  查询图书  */
    void queryGoods(){
        int cmd;
        while(true){
            cout<<query_goods_menu<<endl;
            cout<<"请输入你需要执行的命令序号:";cin>>cmd;
            if(cmd == 0){
                break;
            }
            system("cls");
            switch(cmd){
                case 1:
                    queryByTitle();
                    break;
                case 2:
                    queryById();
                    break;
                default:
                    cout<<"输入的指令有误!请重新输入!"<<endl;
            }
            system("pause");
            system("cls");
 
        }
    }
 
 
    ///按标题查询
    void queryByTitle(){
        int cmd;
        char title[66];
        cout<<"请输入标题:";cin>>title;
        getBookByTitle(title);
    }
 
    ///book
    void getBookByTitle(char title[]){
        bool flag = true;
        printf("%-6s%-10s%-10s%-10s%-10s%\n","编号","标题","作者","出版社","页数");
        for(int i = 0;i < nowBookTotal;i ++){
            if(strcmp(bookList[i].title,title) == 0){
                flag = false;
                printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
            }
        }
        if(flag){
            printf("\n\n空的!\n\n");
        }
    }
 
 
    ///按编号查询
    void queryById(){
        int cmd,i,id;
        cout<<"请输入Id:";cin>>id;
        i = getBookById(id);
        if(i == -1){
            printf("查找不到!\n");
        }else{
            printf("%-6s%-10s%-10s%-10s%-10s\n","编号","标题","作者","评级","出版社","页数");
            printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
        }
    }
 
    ///Book
    int getBookById(int id){
        int index = -1;
        for(int i = 0;i < nowBookTotal;i ++){
            if(bookList[i].id == id){
                index = i;
                break;
            }
        }
        return index;
    }
 
    /*  显示图书库  */
    void showGoods(){
        if(nowBookTotal == 0){
            printf("空的!\n");
        }else{
            printf("%-6s%-10s%-10s%-10s%-10s\n","编号","标题","作者","出版社","页数");
            for(int i = 0;i < nowBookTotal;i ++){
                printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
            }
        }
    }
 
    /*  修改图书  */
    void updateGoods(){
        int cmd,id,i;
        cout<<"请输入你需要修改的图书编号:";cin>>id;
        i = getBookById(id);
        if(i == -1){
                cout<<"图书不存在!"<<endl;
        }else{
            cout<<"原图书信息为:"<<endl;
            printf("%-6s%-10s%-10s%-10s%-10s\n","编号","标题","作者","出版社","页数");
            printf("%-6d%-10s%-10s%-10s%-10d\n",bookList[i].id,bookList[i].title,bookList[i].author,bookList[i].publisher,bookList[i].pageNumber);
            Book book;
            book.id = id;
            cout<<"请输入修改后的标题:";cin>>book.title;
            cout<<"请输入修改后的作者:";cin>>book.author;
            cout<<"请输入修改后的出版社:";cin>>book.publisher;
            cout<<"请输入修改后的页数:";cin>>book.pageNumber;
            bookList[i] = book;
        }
    }
 
    /*  删除图书  */
    void deleteGoods(){
        int cmd,id,i;
        if(nowBookTotal == 0){
            cout<<"空的!"<<endl;
   return;
        }
        cout<<"请输入你需要删除的图书编号:";cin>>id;
        deleteBook(id);
    }
 
    ///book
    void deleteBook(int id){
        int index = getBookById(id);
        if(index == -1){
            cout<<"没有该图书!"<<endl;
        }else{
            for(int i = index;i < nowBookTotal - 1;i ++){
                bookList[i] = bookList[i + 1];
            }
            nowBookTotal --;
            cout<<"删除成功!"<<endl;
        }
    }
 
    /*  保存图书  */
    void saveDataToFile(){
        ///book
        FILE *bookDB = fopen("bookList.txt", "wb");
        for (int i = 0; i < nowBookTotal; i++) {
            fwrite(&bookList[i], sizeof(Book), 1, bookDB);
        }
        fclose(bookDB);
    }
 
    /*  读取图书  */
    void readDataByFile(){
        ///Book
        FILE *bookDB = fopen("bookList.txt", "rb");
        nowBookTotal = 0;
        Book book;
        while (fread(&book, sizeof(Book), 1, bookDB) == 1) {
            bookList[nowBookTotal++] = book;
        }
        fclose(bookDB);
    }
 
    void _exit(){
        saveDataToFile();
        system("cls");
        printf("正在退出");
        for (int i = 0; i < 10; i++) {
            Sleep(100);
            printf(".");
            fflush(stdout);//强制刷新缓存,输出显示
        }
        system("cls");
        printf("已退出!");
        exit(1);
    }
 
};
 
int main(){
    MediaLibraryManageSystem mediaLibraryManageSystem;
    mediaLibraryManageSystem._run();
    return 0;
}

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

原文链接:https://blog.csdn.net/m0_46525584/article/details/106967285

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部