本文实例为大家分享了C++实现图书管理系统的具体代码,供大家参考,具体内容如下
(1)定义图书类;
(2)图书信息包括:书名name,价格price,库存num;
(3)可以查询、增加、删除、修改功能;
(4)使用文件保存及读取图书数据;
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
|
#include<iostream> using namespace std; #include<fstream> #define filename "booklist.txt" #include<list> #include<cstring> #include<iomanip> class Book //创建Book类,存放图书信息 { public : Book(string na= " " , int p=0, int n=0) { name = na; num = n; price = p; } void Show() { cout << "书名:" << std::left << setw(20) << name << std::right << setw(6) << "\t价格:" << price << "\t数量:" << num << endl; } void Set() { cout << "请输入书名:" ; cin >> name; cout << "请输入价格:" ; cin >> price; cout << "请输入数量:" ; cin >> num; } void Addnum() { int n; cout << "请输入归还的数量:" ; cin >> n; num += n; } void Borrownum() { int n; cout << "请输入借出的数量:" ; cin >> n; num -= n; } public : string name; int price; int num; }; void menu() { cout << "--------------------------------------欢迎进入图书管理系统--------------------------------------" << endl; cout << endl << "0 - 退出系统;" << "1 - 显示库存;" << "2 - 查询图书;" << "3 - 借阅图书;" << "4 - 归还图书;" << "5 - 增加图书;" << "6 - 删除图书;" << endl; } class Booklist //创建BookList类,数据成员有Book还有图书数量 { public : void save() //新建图书的话保存数据,用app方式打开文件 { ofstream fout(filename, ios::app); list<Book>::iterator it = BList.begin(); for ( int i = 0; i < num-1; i++) //偏移迭代器,指向新加入的Book并写入文件 { it++; } for (; it != BList.end(); it++) { fout << (*it).name << ' ' << (*it).price << ' ' << (*it).num << '\n' ; } fout.close(); } void resave() { ofstream fout(filename, ios::out); //重新写入数据,因为删除了某个元素 if (fout.is_open()) { for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { fout << (*it).name << ' ' << (*it).price << ' ' << (*it).num << '\n' ; } } fout.close(); } void Show() { for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { (*it).Show(); } } void adddata() //添加数据 { Book B; B.Set(); BList.push_back(B); num++; } void start() //程序一开始读取文件里的数据 { string na; int n; int p; ifstream fin(filename, ios::in); if (fin.is_open()) { while (fin >> na >> p >> n) { Book B(na, p, n); BList.push_back(B); num++; } } fin.close(); } void increase() { cout << "请输入书名:" << endl; string n; cin >> n; for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { if ((*it).name == n) (*it).Addnum(); } resave(); } void decrease() { cout << "请输入书名:" << endl; string n; cin >> n; for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { if ((*it).name == n) (*it).Borrownum(); } resave(); } void FindBook() { string name; cin >> name; for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) //遍历整个list,所以符合关键字的都会被找到 { int index = (*it).name.find(name); //如果没找到返回值是一个很大的数 if (index < (*it).name.length()) (*it).Show(); } } void DeleteBook() { string name; cout << "请输入书名:" ; cin >> name; int i = 0; for (list<Book>::iterator it = BList.begin(); it != BList.end();it++) { if ((*it).name == name) break ; ++i; } list<Book>::iterator it = BList.begin(); advance(it, i); BList.erase(it); --num; resave(); } public : list<Book>BList; int num = 0; }; int main() { Booklist B1; B1.start(); while (1) { menu(); int key; cout << "请输入要进行的操作:" ; cin >> key; switch (key) { case 0: return 0; break ; case 1: B1.Show(); break ; case 2: B1.FindBook(); break ; case 3: B1.decrease(); break ; case 4: B1.increase(); break ; case 5: { B1.adddata(); B1.save(); break ; } case 6: B1.DeleteBook(); break ; } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_55037029/article/details/117790096