题目描述
要求:模拟书店销售管理,每本图书的信息包括ISBN号、书名 、单价、库存量等 ,请设计程序,至少完成书店图书的初始数据录入、修改单价或库存、按书名关键字查询图书等功能。

实验步骤
1、编写图书信息类
2、编写图书销售管理员
3、编写销售类中的函数,实现初始数据的录入(写文件);
4、编写销售类中的函数,当输入书名关键字时,可输出书名中包含此关键字的所有图书信息。
5、编写销售类中的函数,输入ISBN号时,可修改此图书的库存(或单价)
6、编写主函数,显示主菜单,根据用户的选择,调用销售管理员的相关代码完成指定的功能;
7、测试程序。

第一版:不通过文件流

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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
const int MAX = 500;
/*
每本图书的信息包括ISBN号、书名 、单价、库存量

请设计程序,至少完成书店图书的初始数据录入、修改单价或库存、按书名关键字查询图书等功能。
实验步骤
1、编写图书信息类
2、编写图书销售管理员
3、编写销售类中的函数,实现初始数据的录入(写文件);
4、编写销售类中的函数,当输入书名关键字时,可输出书名中包含此关键字的所有图书信息。
5、编写销售类中的函数,输入ISBN号时,可修改此图书的库存(或单价)
6、编写主函数,显示主菜单,根据用户的选择,调用销售管理员的相关代码完成指定的功能;
7、测试程序。
*/
fstream shu("book.txt", ios::in | ios::out);
class Book{
string isbn;
string name;
double price;
unsigned long nums;
friend class XS;
public:
Book(string isbn, string name, double price, unsigned long nums) :isbn(isbn),
name(name), price(price), nums(nums){
}
void print(){
cout << "ISBN= " << isbn << " name= " << name << " price= " << price << " nums=" << nums << endl;
}
void rxiuprice(double price_){
price = price_;
}
void rxiunums(unsigned long num){
nums = num;
}


};
class XS{//销售类
Book *p[MAX];

public:
static unsigned long totalbooks;
void add(Book* b){
if (b != NULL){
p[totalbooks] = b;
totalbooks++;
cout << "添加成功" << endl;
}
}
void addbook(){
string isbn;
string name;
double price;
unsigned long nums;
cout << "输入书籍的信息:" << endl;;
cin >> isbn >> name >> price >> nums;
p[totalbooks] = new Book(isbn, name, price, nums);
totalbooks++;
cout << "添加成功" << endl;
}
void showbook(){
cout << "请输入书名:";
string name;
cin >> name;
int i = 0;
for (i = 0; i < totalbooks; ++i){
if ((*(p + i))->name == name){
break;
}
}
(*(p + i))->print();
}
void showallbook(){
int i = 0;
for (i = 0; i < totalbooks; ++i){
//cout << "i= "<<i << endl;
(*(p + i))->print();
}
}
void cinbisn(){
int i = 0;
string bisn;
cout << "输入ISBN码:";
cin >> bisn;

for (i=0; i < totalbooks; ++i){
if ((*(p + i))->isbn == bisn){
break;
}
}


cout << "1.修改单价\n2.修改库存" << endl;
int cina;
cin >> cina;
if (cina == 1){
double prince = 0;
cout << "修改单价为:";
cin >> prince;
(*(p + i))->rxiuprice(prince);
cout << "修改单价成功" << endl;
}
else if (cina == 2){
unsigned long nums;
cout << "修改库存为:";
cin >> nums;
(*(p + i))->rxiunums(nums);
cout << "修改库存成功" << endl;
}
else
{
cout << "输入错误不发生修改" << endl;
}

}
};
unsigned long XS::totalbooks = 0;
/*
3
1 a 30 30
2 b 30 31
3 cc 32 32
*/

int main(){
unsigned long numss;
cout << "初始化书本的数量:";
cin >> numss;
Book *p;
XS xs;
string isbn;
string name;
double price;
unsigned long nums;
for (int i = 0; i < numss; ++i){
p = NULL;
cin >> isbn >> name >> price >> nums;
p = new Book(isbn, name, price, nums);
xs.add(p);
}
cout << "size= " << xs.totalbooks << endl;
int ch;
while (true)
{
cout << "-----------------------菜单---------------------- " << endl;
cout << " 1.添加书本 " << endl;
cout << " 2.修改书籍 " << endl;
cout << " 3.查找书籍 " << endl;
cout << " 4.显示所有书籍 " << endl;
// cout << " 5. " << endl;
cin >> ch;
switch (ch)
{
case 1:
xs.addbook();
break;
case 2:
xs.cinbisn();
break;
case 3:
xs.showbook();
break;
case 4:
xs.showallbook();
break;
default:
break;
}
}
return 0;
}

在这里插入图片描述

第二版
经过文件,可储存修改

~~

注意: 需要在vs里面运行,dev不行,需要创建book.dat文件,txt格式可能会发生错误

~~
在这里插入图片描述

在这里插入图片描述

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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
const int MAX = 500;
fstream shu;
void init(){
shu.open("book.dat", ios::in | ios::out | ios::binary);
}
class Book{
friend class XS;
public:
string isbn;
string name;
double price;
unsigned long nums;
Book(){
}
Book(string isbn, string name, double price, unsigned long nums) :isbn(isbn),
name(name), price(price), nums(nums){
}
void print(){
printf("isbn:%-5s 书名:%-5s 价格:%-5.lf 库存:%lu\n", isbn, name, price, nums);
}
void rxiuprice(double price_){
price = price_;
}
void rxiunums(unsigned long num){
nums = num;
}
};
int gethang(){
int num = 0;
shu.seekg(0, ios::end);
num = shu.tellg() / sizeof(Book);
return num;
}
class XS{//销售类
Book p[MAX];
public:
static unsigned long totalbooks;
void addbook(){
string isbn;
string name;
double price;
unsigned long nums;
cout << "输入书籍的信息:" << endl;;
cin >> isbn >> name >> price >> nums;
Book book(isbn, name, price, nums);
totalbooks++;
shu.seekp((totalbooks - 1)*sizeof(Book), ios::beg);
shu.write((char*)&book, sizeof(Book));
shu.flush();
cout << "size = " << totalbooks << endl;
cout << "添加成功" << endl;
}
void showbook(){
cout << "请输入书名:";
string name;
cin >> name;

shu.seekg(0, ios::end);
totalbooks = shu.tellg() / sizeof(Book);
shu.seekg(0);
for (int i = 0; i<totalbooks; ++i){
Book book;
shu.read((char*)&book, sizeof(Book));
if (book.name == name){
book.print();
}
}
}
void showallbook(){
shu.seekg(0, ios::end);
cout << "书本数 " << shu.tellg() / sizeof(Book) << endl;
totalbooks = shu.tellg() / sizeof(Book);
shu.seekg(0);
for (int i = 0; i<totalbooks; ++i){
Book book;
shu.read((char*)&book, sizeof(Book));
book.print();
}
}
void cinbisn(){
shu.seekg(0, ios::end);
int size = shu.tellg() / sizeof(Book);
cout << "size=" << size << endl;
string bisn;
cout << "输入ISBN码:";
cin >> bisn;
Book book;
shu.seekg(0);
int i = 0;
for ( i= 0; i < size;++i)
{
shu.read((char*)&book, sizeof(Book));
if (book.isbn == bisn){
break;
}
}
cout << "i=" << i << "book is " << book.isbn << "name =" << book.name << endl;
cout << "1.修改单价\n2.修改库存" << endl;
int cina;
cin >> cina;
if (cina == 1){
double prince = 0;
cout << "修改单价为:";
cin >> prince;
book.rxiuprice(prince);
shu.seekp(sizeof(Book)*i, ios::beg);
shu.write((char*)&book, sizeof(Book));
shu.flush();
cout << "修改单价成功" << endl;
}
else if (cina == 2){
unsigned long nums;
cout << "修改库存为:";
cin >> nums;
book.rxiunums(nums);
shu.seekp(sizeof(Book)*i, ios::beg);
shu.write((char*)&book, sizeof(Book));
shu.flush();
cout << "修改库存成功" << endl;
}
else
{
cout << "输入错误不发生修改" << endl;
}
}
};
unsigned long XS::totalbooks = 0;

int main(){
init();
if (shu.fail()){
cout << "打开文件失败" << endl;
exit(0);
}
XS xs;
shu.seekg(0, ios::end);
xs.totalbooks = shu.tellg() / sizeof(Book);
cout << "size= " << xs.totalbooks << endl;
int ch;
while (true)
{
cout << "-----------------------菜单------------------------|" << endl;
cout << "| 1.添加书本 |" << endl;
cout << "| 2.修改书籍 |" << endl;
cout << "| 3.查找书籍 |" << endl;
cout << "| 4.显示所有书籍 |" << endl;
cout << "| 5.保存并退出程序 | " << endl;
cout << "|--------------------------------------------------|" << endl;
cout << "输入选项:";
cin >> ch;
switch (ch)
{
case 1:
xs.addbook();
cout << "size= " << xs.totalbooks << endl;
break;
case 2:
xs.cinbisn();
break;
case 3:
xs.showbook();
break;
case 4:
xs.showallbook();
break;
case 5:
shu.flush();
shu.close();
return 0;
default:
break;
}
}
shu.close();
return 0;
}

运行截图
在这里插入图片描述
在这里插入图片描述
第三版:
新增加两个功能
1.添加按书名或者ISBN码删除书籍
2.添加删除所有书籍;

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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
fstream shu;
void init(){
shu.open("book.dat", ios::in | ios::out | ios::binary);

if (shu.fail()){
shu.open("book.dat",ios::out | ios::binary);
shu.close();
shu.open("book.dat", ios::in | ios::out | ios::binary);
if (shu.is_open()){
cout << "创建文件成功"<< endl;
}
else{
cout << "打开文件失败" << endl;

exit(0);
}



}
}
class Book{

public:
string isbn;
string name;
double price;
unsigned long nums;
Book(){
}
Book(string isbn, string name, double price, unsigned long nums) :isbn(isbn),
name(name), price(price), nums(nums){
}
void print(){
printf("ISBN码: %-5s 书名:《%-5s》 价格:%-3.lf 元 库存: %lu 本\n", isbn, name, price, nums);
}
void rxiuprice(double price_){
price = price_;
}
void rxiunums(unsigned long num){
nums = num;
}
};
int gethang(){
int num = 0;
shu.seekg(0, ios::end);
num = shu.tellg() / sizeof(Book);
return num;
}
class XS{//销售类

public:
static unsigned long totalbooks;
void addbook(){
string isbn;
string name;
double price;
unsigned long nums;
cout << "输入书籍的信息\n" << endl;;
cout << "ISBN码:";
cin >> isbn;
cout << "书名:";
cin >> name;
cout << "价格:";
cin >> price;
cout << "库存:";
cin >> nums;
cout << endl;
Book book(isbn, name, price, nums);
totalbooks++;
shu.seekp((totalbooks - 1)*sizeof(Book), ios::beg);
shu.write((char*)&book, sizeof(Book));
shu.flush();
cout << "size = " << totalbooks << endl;
cout << "添加成功" << endl;
}
void deletebook(){
cout << "请输入书名或者isbn码:";
string name;
cin >> name;
fstream shu1("temp.dat", ios::out | ios::binary|ios::trunc);//如果存在则清空

shu.seekg(0, ios::end);
// cout << "书本数 " << shu.tellg() / sizeof(Book) << endl;
totalbooks = shu.tellg() / sizeof(Book);
shu.seekg(0);
for (int i = 0; i<totalbooks; ++i){
Book book;
shu.read((char*)&book, sizeof(Book));
if (name == book.isbn || name == book.name){
continue;
}
else{
shu1.write((char*)&book, sizeof(Book));
}
}
shu1.close();
shu.close();
fstream shu2("temp.dat");
ofstream shu3("book.dat", ios::in | ios::binary|ios::trunc);
for (int i = 0; i<totalbooks-1; ++i){
Book book;
shu2.read((char*)&book, sizeof(Book));
shu3.write((char*)&book, sizeof(Book));
}
shu3.close();
shu2.close();
system("del temp.dat");
init();
cout << "删除成功" << endl;
--totalbooks;
}
void deletel_all_book(){
shu.close();
system("del book.dat");
init();
cout << "删除成功" << endl;
totalbooks = 0;
}
void show_one_book(){
cout << "请输入书名:";
string name;
cin >> name;

shu.seekg(0, ios::end);
totalbooks = shu.tellg() / sizeof(Book);
shu.seekg(0);
for (int i = 0; i<totalbooks; ++i){
Book book;
shu.read((char*)&book, sizeof(Book));
if (book.name == name){
book.print();
}
}
}
void showallbook(){
shu.seekg(0, ios::end);
cout << "书本数 " << shu.tellg() / sizeof(Book) << endl;
totalbooks = shu.tellg() / sizeof(Book);
shu.seekg(0);
for (int i = 0; i<totalbooks; ++i){
Book book;
shu.read((char*)&book, sizeof(Book));
book.print();
}
}
void cinbisn(){
shu.seekg(0, ios::end);
int size = shu.tellg() / sizeof(Book);
cout << "size=" << size << endl;
string bisn;
cout << "输入ISBN码:";
cin >> bisn;
Book book;
shu.seekg(0);
int i = 0;
for ( i= 0; i < size;++i)
{
shu.read((char*)&book, sizeof(Book));
if (book.isbn == bisn){
break;
}
}
cout << "i=" << i << "book is " << book.isbn << "name =" << book.name << endl;
cout << "1.修改单价\n2.修改库存" << endl;
int cina;
cin >> cina;
if (cina == 1){
double prince = 0;
cout << "修改单价为:";
cin >> prince;
book.rxiuprice(prince);
shu.seekp(sizeof(Book)*i, ios::beg);
shu.write((char*)&book, sizeof(Book));
shu.flush();
cout << "修改单价成功" << endl;
}
else if (cina == 2){
unsigned long nums;
cout << "修改库存为:";
cin >> nums;
book.rxiunums(nums);
shu.seekp(sizeof(Book)*i, ios::beg);
shu.write((char*)&book, sizeof(Book));
shu.flush();
cout << "修改库存成功" << endl;
}
else
{
cout << "输入错误不发生修改" << endl;
}
}
};
unsigned long XS::totalbooks = 0;

int main(){
init();

XS xs;
shu.seekg(0, ios::end);
xs.totalbooks = shu.tellg() / sizeof(Book);
cout << "size= " << xs.totalbooks << endl;
int ch;
while (true)
{
cout << "-----------------------菜单------------------------|" << endl;
cout << "| 1.添加书本 |" << endl;
cout << "| 2.删除书本 |" << endl;
cout << "| 3.修改书籍 |" << endl;
cout << "| 4.查找书籍 |" << endl;
cout << "| 5.显示所有书籍 |" << endl;
cout << "| 6.删除所有书籍 |" << endl;
cout << "| 7.保存并退出程序 | " << endl;
cout << "|--------------------------------------------------|" << endl;
cout << "输入选项:";
cin >> ch;
switch (ch)
{
case 1:
xs.addbook();
cout << "书本数:" << xs.totalbooks << endl;
break;
case 2:
xs.deletebook();
cout << "书本数: " << xs.totalbooks << endl;
break;
case 3:
xs.cinbisn();
break;
case 4:
xs.show_one_book();
break;
case 5:
xs.showallbook();
break;
case 6:
xs.deletel_all_book();
cout << "书本数: " << xs.totalbooks << endl;
break;
case 7:
shu.flush();
shu.close();
return 0;
default:
break;
}
}
shu.close();
return 0;
}

截图:
在这里插入图片描述