喜迎
牛年
C

C语言 增删查改

Posted by 陈陈菌 on 2020-06-16
Estimated Reading Time 9 Minutes
Words 2k In Total
Viewed Times

此次练习使用了结构体、数组、指针、文件操作实现,主要数据存放生成的(Book.db)(Author.db)二进制文件中,不过数据是文本。

代码相关分享

GitHub : https://github.com/2670637999/Book-management-System
码云:https://gitee.com/chenjiehai/Book-management-System

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//临时值
int xx = 0;
//临时的输入值 (数字) //临时的输入值 (字符串) //总共入库书籍 //书名数据文件指针(写) //作者名数据文件指针(写) //书名数据文件指针(读) //作者名数据文件指针(读)
int inputNumber; char input[50]; int sum; FILE *f; FILE *f2; FILE *f3; FILE *f4;

//容器的数据类型
typedef struct{
char name[26];
}TXT;
typedef struct{
char name[26];
}TXT2;
//书名容器
TXT Book[20]={
{"空"},{"C语言从入门到精通"},{"Java从入门到精通"},
{"Python从入门到精通"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},
};
//作者容器
TXT2 Author[20]={
{"空"},{"小蓝"},{"小红"},
{"小黄"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},{"空"},
{"空"},{"空"},
};
/**初始化书籍,判断本地数据是否存在,如果不存在则进行数据创建**/
void SetBook(){

f3 = fopen("Book.db","rb");
//[如果书名数据不存在, 创建数据]
if(f3 == NULL){
/**书名**/
f = fopen("Book.db","wb+");
if(f == NULL){
printf("文档打开失败\n");
}else{
printf("书籍文档打开成功\n");
printf("书籍名成功初始化数据\n");
fwrite(&Book[0],sizeof(TXT),20,f);
}
sum = 0;
for (int i = 0; i < 19; i++)
{
if(strcmp(Book[i].name,"空")!=0){
sum++;
}
}
fclose(f);
//[如果书名数据库存在, 不创建数据库]
}else{
sum = 0;
for (int i = 0; i < 19; i++)
{
if(strcmp(Book[i].name,"空")!=0){
sum++;
}
}
printf("\n查询到本地数据,不进行数据创建");
}
f4 = fopen("Author.db","rb");
//[如果作者数据不存在, 创建本地数据]
if(f4 == NULL){
/**作者名**/
f2 = fopen("Author.db","wb+");
if(f2 == NULL){
printf("\n文档打开失败\n");
}else{
printf("\n作者文档打开成功\n");
printf("作者名成功初始化数据\n");
fwrite(&Author[0],sizeof(TXT),20,f2);
}
fclose(f2);
printf("\n查询不到本地数据,进行数据创建");
//[如果作者数据存在, 不创建]
}else{

}
}
//程序结束前把内存数据写入本地数据
void setBook(){
/**书名**/
f = fopen("Book.db","wb+");
if(f == NULL){
printf("文档打开失败\n");
}else{
printf("书籍文档打开成功\n");
printf("书籍名成功初始化数据\n");
fwrite(&Book[0],sizeof(TXT),20,f);
}
sum = 0;
for (int i = 0; i < 19; i++)
{
if(strcmp(Book[i].name,"空")!=0){
sum++;
}
}
fclose(f);
/**作者名**/
f2 = fopen("Author.db","wb+");
if(f2 == NULL){
printf("\n文档打开失败\n");
}else{
printf("\n作者文档打开成功\n");
printf("作者名成功初始化数据\n");
fwrite(&Author[0],sizeof(TXT),20,f2);
}
fclose(f2);
}
//修改书名
void FileBook(){
/**书名**/
f = fopen("Book.db","wb+");
scanf("%s",&Book[inputNumber]);
if(f == NULL){
printf("| 文档打开失败\n");
printf("+------------------------------------------------------------------------------+\n");
}else{
printf("+------------------------------------------------------------------------------+\n");
printf("| 文档打开成功\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 成功写入数据\n");
fwrite(&Book[inputNumber],sizeof(TXT),20,f);
}
fclose(f);

}
//修改作者
void FileAuthor(){
/**作者**/
f2 = fopen("Author.db","wb+");
scanf("%s",&Author[inputNumber]);
if(f == NULL){
printf("| 文档打开失败\n");
printf("+------------------------------------------------------------------------------+\n");
}else{
printf("+------------------------------------------------------------------------------+\n");
printf("| 文档打开成功\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 成功写入数据\n");
fwrite(&Author[inputNumber],sizeof(TXT),20,f);
}
fclose(f2);
}
/**获取图书和作者信息模块(查询)**/
void GetData(){
f = fopen("Book.db","rb");
f2 = fopen("Author.db","rb");
fread(&Book[0],sizeof(TXT),20,f);
fread(&Author[0],sizeof(TXT),20,f2);
for (int i = 1; i < 20; i++)
{
printf("\n+------------------------------------------------------------------------------+");
printf("\n");
if(i<10){
printf("| 0%d |",i);
printf(" %s |",&Author[i]);
printf(" 《%s》 ",&Book[i]);
}else if(i>9){
printf("| %d |",i+1);
printf(" %s |",&Author[i]);
printf(" 《%s》 ",&Book[i]);
}
}
fclose(f);
fclose(f2);
printf("\n+------------------------------------------------------------------------------+");
printf("\n| 查询完毕.....");
system("pause");
}
/**修改图书信息模块**/
void SetBook2(){
printf("| 请输入要修改的图书的编号\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); scanf("%d",&inputNumber);
printf("+------------------------------------------------------------------------------+\n");
if(strcmp(Author[inputNumber].name,"空")==0){
printf("| 不可修改!,因为该编号为空\n");
printf("+------------------------------------------------------------------------------+\n");
system("pause");
}else{
printf("| 请选择要修改的项 (作者) (书名)\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); scanf("%s",&input);
printf("+------------------------------------------------------------------------------+\n");
if(strcmp(input,"书名")==0){
printf("| 请输入修改的数据\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); FileBook();
printf("+------------------------------------------------------------------------------+\n");
printf("| 修改完毕。。。\n");
printf("+------------------------------------------------------------------------------+\n");
system("pause");
}
if(strcmp(input,"作者")==0){
printf("| 请输入修改的数据\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); FileAuthor();
printf("+------------------------------------------------------------------------------+\n");
printf("| 修改完毕。。。\n");
printf("+------------------------------------------------------------------------------+\n");
system("pause");
}
}
setBook();

}
//入库书名
void sBook(){
/**书名**/
f = fopen("Book.db","wb+");
scanf("%s",&Book[inputNumber]);
if(f == NULL){
printf("| 文档打开失败\n");
printf("+------------------------------------------------------------------------------+\n");
}else{
printf("+------------------------------------------------------------------------------+\n");
printf("| 文档打开成功\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 成功写入数据\n");
fwrite(&Book[inputNumber],sizeof(TXT),20,f);
}
fclose(f);

}
//入库作者名
void sAuthor(){
/**作者**/
f2 = fopen("Book.db","wb+");
scanf("%s",&Author[inputNumber]);
if(f == NULL){
printf("| 文档打开失败\n");
printf("+------------------------------------------------------------------------------+\n");
}else{
printf("+------------------------------------------------------------------------------+\n");
printf("| 文档打开成功\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 成功写入数据\n");
fwrite(&Author[inputNumber],sizeof(TXT),20,f2);
}
fclose(f2);
}
/**图书入库模块**/
void SaveBook(){
printf("| 请输入需要入库的图书的编号\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); scanf("%d",&inputNumber);
printf("+------------------------------------------------------------------------------+\n");
if(strcmp(Author[inputNumber].name,"空")!=0){
printf("| 该编号已被占用\n");
printf("+------------------------------------------------------------------------------+\n");
system("pause");
}else{
printf("| 请输入即将入库的书名\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); sBook();
printf("+------------------------------------------------------------------------------+\n");
printf("| 请输入即将入库的作者\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); sAuthor();
printf("+------------------------------------------------------------------------------+\n");
printf("| 正在进行入库。。。\n");
printf("+------------------------------------------------------------------------------+\n");
system("pause");
}
setBook();
}
/**删除图书信息模块**/
void DelBook(){
printf("| 你想要删除哪个编号的图书信息?\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); scanf("%d",&inputNumber);
printf("+------------------------------------------------------------------------------+\n");
printf("| 正在进行删除。。。\n");
printf("+------------------------------------------------------------------------------+\n");
if(strcmp(Book[inputNumber].name,"空")==0){
printf("删除失败,因为它是空的");
}else{
Book[inputNumber] = Book[0];
Author[inputNumber] = Author[0];
printf("| 删除完毕。。。\n");
printf("+------------------------------------------------------------------------------+\n");
system("pause");
}
setBook();
}
/**查图书编号模块**/
void GetBookNumber(){
printf("| 你想要查那个编号的书籍信息?\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); scanf("%d",&inputNumber);
printf("+------------------------------------------------------------------------------+\n");
printf("| 正在查询。。。\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 查询完毕。。。\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| %d 号书籍是 %s 作者:%s\n",inputNumber,&Book[inputNumber],&Author[inputNumber]);
printf("+------------------------------------------------------------------------------+\n");
system("pause");
}
/**主界面**/
void Menu(){
while(1){
sum=0;
SetBook();
system("cls");
printf("\n\n+------------------------------------------------------------------------------+\n");
printf("| 【 图 书 管 理 系 统 】 |\n");
printf("+------------------------------------------------------------------------------+\n");
xx++;
if(xx>1){
printf("| 当前库中已有书籍:%d 本 请进行相关操作。 |\n",sum);
}else{
printf("| 当前库中已有书籍: 本 (未查询) 请进行相关操作。 |\n");
}
printf("+------------------------------------------------------------------------------+\n");
printf("| 查询图书 | 修改图书 | 图书入库 | 删除图书 | 查图书编号 | 退出程序 | 关于程序 |\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 输入: "); scanf("%s",input);
printf("+------------------------------------------------------------------------------+\n");
if(strcmp(input,"查询图书")==0){
printf("| 正在查询图书.....\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 编号 | 作者 | 书名 |");
GetData();
printf("\n+------------------------------------------------------------------------------+");
printf("\n| 当前的已入库图书: %d 本\n",sum);
printf("+------------------------------------------------------------------------------+\n\n");
}
if(strcmp(input,"修改图书")==0){
printf("| 修改图书.....\n");
printf("+------------------------------------------------------------------------------+\n");
SetBook2();
}
if(strcmp(input,"图书入库")==0){
printf("| 正在进行图书入库.....\n");
printf("+------------------------------------------------------------------------------+\n");
SaveBook();
}
if(strcmp(input,"删除图书")==0){
printf("| 正在进行图书信息删除.....\n");
printf("+------------------------------------------------------------------------------+\n");
DelBook();
}
if(strcmp(input,"查图书编号")==0){
GetBookNumber();
}
if(strcmp(input,"退出程序")==0){
printf("| 正在退出程序.....\n");
printf("+------------------------------------------------------------------------------+\n");
break;
}
if(strcmp(input,"关于程序")==0){
printf("| 本程序由 信息工程系 19级移动应用开发 陈杰海 完成开发.(目前已完成增删查改功能)|\n");
printf("+------------------------------------------------------------------------------+\n");
printf("| 是否继续使用该程序? 输入 是/否:\n"); printf("| 输入: "); scanf("%s",input);
printf("+------------------------------------------------------------------------------+\n");
if(strcmp(input,"否")==0){
printf("| 正在退出程序.....\n");
printf("+------------------------------------------------------------------------------+\n");
break;
}
}
}
setBook();
system("cls");
}
/**主程序**/
void main(){
Menu();
}