-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
executable file
·637 lines (514 loc) · 20.6 KB
/
Copy pathdatabase.cpp
File metadata and controls
executable file
·637 lines (514 loc) · 20.6 KB
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#include "database.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*************************************************
*************************************************/
char path[300] = "./database/";
/*************************************************
*************************************************/
STU_admin_information admin_information;
int database_admin_information(char user_id[30], int read_type) {
FILE *fwrite; // 声明读取文件所需指针
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "admin_information/"); // 加入路径"admin_information/"
strcat(file_name, user_id); // 文件名为"user_id"
strcat(file_name, ".txt"); // 后缀为.txt
if (!read_type) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
return 0; // 不存在, 返回"0"
// 读取数据
fscanf(fwrite, "%s %s %s %s",
admin_information.shop_id, // 饭店名
admin_information.name, // 管理员姓名
admin_information.email, // 邮箱
admin_information.password // 密码
);
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
fprintf(fwrite, "%s %s %s %s",
admin_information.shop_id, // 饭店名
admin_information.name, // 管理员姓名
admin_information.email, // 邮箱
admin_information.password // 密码
);
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_consumer_information consumer_information;
int database_consumer_information(char user_id[30], int read_type) {
FILE *fwrite; // 声明读取文件所需指针
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "consumer_information/"); // 加入路径"consumer_information/"
strcat(file_name, user_id); // 文件名为"user_id"
strcat(file_name, ".txt"); // 后缀为.txt
if (!read_type) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
return 0; // 不存在, 返回"0"
// 读取数据
fscanf(fwrite, "%s %s %s %s %s %s %f",
consumer_information.name, // 顾客姓名
consumer_information.sex, // 顾客性别
consumer_information.tel, // 手机号
consumer_information.password, // 密码
consumer_information.email, // 邮箱
consumer_information.address, // 地址
&consumer_information.money // 余额
);
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
fprintf(fwrite, "%s %s %s %s %s %s %0.2f",
consumer_information.name, // 顾客姓名
consumer_information.sex, // 顾客性别
consumer_information.tel, // 手机号
consumer_information.password, // 密码
consumer_information.email, // 邮箱
consumer_information.address, // 地址
consumer_information.money // 余额
);
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_goods_index goods_index[100];
int database_goods_index(char user_id[30], int read_type) {
FILE *fwrite; // 声明读取文件所需指针
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "goods_index/"); // 加入路径"goods_index/"
strcat(file_name, user_id); // 文件名为"user_id"
strcat(file_name, ".txt"); // 后缀为.txt
int i = 0;
if (!read_type) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
return 0; // 不存在, 返回"0"
// 读取数据
while (!feof(fwrite)) {
fscanf(fwrite, "%s %f %f %d %d %f %d:%d:%d:%d:%d %d:%d:%d:%d:%d",
goods_index[i].shop_id, // 饭店ID
&goods_index[i].unit_price, // 单价
&goods_index[i].in_price, // 进价
&goods_index[i].sales_volume, // 销量
&goods_index[i].goods_in_stock, // 库存
&goods_index[i].discount_price, // 折扣价格
&goods_index[i].time_begin.tm_year, // 折扣开始时间
&goods_index[i].time_begin.tm_mon, // ...
&goods_index[i].time_begin.tm_mday, // ...
&goods_index[i].time_begin.tm_hour, // ...
&goods_index[i].time_begin.tm_min, // ...
&goods_index[i].time_end.tm_year, // 折扣结束时间
&goods_index[i].time_end.tm_mon, // ...
&goods_index[i].time_end.tm_mday, // ...
&goods_index[i].time_end.tm_hour, // ...
&goods_index[i].time_end.tm_min // ...
);
i++;
}
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
for (i = 0; i <= 98; i++)
fprintf(fwrite,
"%s %0.2f %0.2f %d %d %0.2f %04d:%02d:%02d:%02d:%02d "
"%04d:%02d:%02d:%02d:%02d\n",
goods_index[i].shop_id, // 饭店ID
goods_index[i].unit_price, // 单价
goods_index[i].in_price, // 进价
goods_index[i].sales_volume, // 销量
goods_index[i].goods_in_stock, // 库存
goods_index[i].discount_price, // 折扣价格
goods_index[i].time_begin.tm_year, // 折扣开始时间
goods_index[i].time_begin.tm_mon, // ...
goods_index[i].time_begin.tm_mday, // ...
goods_index[i].time_begin.tm_hour, // ...
goods_index[i].time_begin.tm_min, // ...
goods_index[i].time_end.tm_year, // 折扣结束时间
goods_index[i].time_end.tm_mon, // ...
goods_index[i].time_end.tm_mday, // ...
goods_index[i].time_end.tm_hour, // ...
goods_index[i].time_end.tm_min // ...
);
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_order_admin_all order_admin_all[100]; // 最多存放100笔订单
int database_order_admin_all(char shop_id[30], int read_type) {
// 声明读取文件所需指针
FILE *fwrite;
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "order_admin/"); // 加入路径"order_admin/"
// 处理文件名
strcat(file_name, shop_id);
strcat(file_name, "_all.txt");
int i = 0;
if (!read_type) {
// 打开特定订单数量文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
return 0; // 不存在, 返回"0"
// 读取数据
while (!feof(fwrite)) {
fscanf(fwrite, "%s %s %d:%d:%d:%d:%d %s %d %f %f",
order_admin_all[i].order_id, // 订单编号
order_admin_all[i].consumer_id, // 顾客编号
&order_admin_all[i].sold_time.tm_year, // 购买时间
&order_admin_all[i].sold_time.tm_mon, // ...
&order_admin_all[i].sold_time.tm_mday, // ...
&order_admin_all[i].sold_time.tm_hour, // ...
&order_admin_all[i].sold_time.tm_min, // ...
order_admin_all[i].goods_name, // 菜品名
&order_admin_all[i].purchase_num, // 购买数量
&order_admin_all[i].unit_price, // 单价
&order_admin_all[i].all_price // 总价
);
i++;
}
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
while (order_admin_all[i].purchase_num != 0) {
fprintf(fwrite, "%s %s %04d:%02d:%02d:%02d:%02d %s %d %0.2f %0.2f\n",
order_admin_all[i].order_id, // 订单编号
order_admin_all[i].consumer_id, // 顾客编号
order_admin_all[i].sold_time.tm_year, // 购买时间
order_admin_all[i].sold_time.tm_mon, // ...
order_admin_all[i].sold_time.tm_mday, // ...
order_admin_all[i].sold_time.tm_hour, // ...
order_admin_all[i].sold_time.tm_min, // ...
order_admin_all[i].goods_name, // 菜品名
order_admin_all[i].purchase_num, // 购买数量
order_admin_all[i].unit_price, // 单价
order_admin_all[i].all_price // 总价
);
i++;
}
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_order_admin_consumer order_admin_consumer[100]; // 最多存放100笔订单
int database_order_admin_consumer(char shop_id[30], int read_type) {
// 声明读取文件所需指针
FILE *fwrite;
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "order_admin/"); // 加入路径"order_admin/"
// 处理文件名
strcat(file_name, shop_id);
strcat(file_name, "_consumer.txt");
int i = 0;
if (read_type <= 0) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
return 0; // 不存在, 返回"0"
// 读取数据
i = 0;
fscanf(fwrite, "%s %s %d",
order_admin_consumer[i].consumer_id, // 顾客ID
order_admin_consumer[i].goods_name, //菜品名
&order_admin_consumer[i].purchase_num // 购买数量
);
if (read_type == -1) {
if (order_admin_consumer[i].purchase_num != 0) {
printf("以下为该饭店最受欢迎的菜品名, 欢迎选购:\n");
printf("%s\n", order_admin_consumer[i].goods_name);
}
}
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
while (order_admin_consumer[i].purchase_num != 0) {
fprintf(fwrite, "%s %s %d\n",
order_admin_consumer[i].consumer_id, // 顾客ID
order_admin_consumer[i].goods_name, //菜品名
order_admin_consumer[i].purchase_num // 购买数量
);
i++;
}
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_order_admin_goods order_admin_goods[100]; // 最多存放100笔订单
int database_order_admin_goods(char shop_id[30], int read_type) {
// 声明读取文件所需指针
FILE *fwrite;
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "order_admin/"); // 加入路径"order_admin/"
strcat(file_name, shop_id);
strcat(file_name, "_goods.txt");
int i = 0;
if (!read_type) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及��读
return 0; // 不存在, 返回"0"
// 读取数据
while (!feof(fwrite)) {
fscanf(fwrite, "%s %d %f %f",
order_admin_goods[i].goods_name, // 菜品名
&order_admin_goods[i].purchase_num, // 购买数量
&order_admin_goods[i].all_price, // 营业额
&order_admin_goods[i].profit // 利润
);
i++;
}
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
while (order_admin_goods[i].purchase_num != 0) {
fprintf(fwrite, "%s %d %0.2f %0.2f\n",
order_admin_goods[i].goods_name, // 菜品名
order_admin_goods[i].purchase_num, // 购买数量
order_admin_goods[i].all_price, // 营业额
order_admin_goods[i].profit // 利润
);
i++;
}
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_order_consumer order_consumer[100]; // 最多存放一百笔订单
int database_order_consumer(char user_id[30], int read_type) {
// 声明读取文件所需指针
FILE *fwrite;
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "order_consumer/"); // 加入路径"order_consumer/"
strcat(file_name, user_id);
strcat(file_name, ".txt");
int i = 0;
if (!read_type) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
return 0; // 不存在, 返回"0"
// 读取数据
while (!feof(fwrite)) {
fscanf(fwrite, "%s %s %s %d %f %f",
order_consumer[i].order_id, // 订单编号
order_consumer[i].sold_time, // 购买时间
order_consumer[i].goods_name, // 菜品名
&order_consumer[i].purchase_num, // 购买数量
&order_consumer[i].unit_price, // 单价
&order_consumer[i].all_price // 总价
);
i++;
}
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
for (i = 0; i <= 98; i++)
fprintf(fwrite, "%s %s %s %d %0.2f %0.2f\n",
order_consumer[i].order_id, // 订单编号
order_consumer[i].sold_time, // 购买时间
order_consumer[i].goods_name, // 菜品名
order_consumer[i].purchase_num, // 购买数量
order_consumer[i].unit_price, // 单价
order_consumer[i].all_price // 总价
);
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_shop_index shop_index[100];
int database_shop_index(char user_id[30], int read_type) {
// 声明读取文件所需指针
FILE *fwrite;
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "shop_index/"); // 加入路径"shop_index/"
strcat(file_name, user_id);
strcat(file_name, ".txt");
int i = 0;
if (!read_type) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
{
fclose(fwrite);
return 0;
} // 不存在, 返回"0"
for (i = 0; i <= 98; i++) {
// 读取数据
fscanf(fwrite, "%s %f %f %d %d %f %d:%d:%d:%d:%d %d:%d:%d:%d:%d",
shop_index[i].goods_name, // ����品编号
&shop_index[i].unit_price, // 零售价格
&shop_index[i].in_price, // 进货价�����������������������
&shop_index[i].sales_volume, // 销量
&shop_index[i].goods_in_stock, // 存货
&shop_index[i].discount_price, // 折扣价
&shop_index[i].time_begin.tm_year, // 折扣开始时间
&shop_index[i].time_begin.tm_mon, // ...
&shop_index[i].time_begin.tm_mday, // ...
&shop_index[i].time_begin.tm_hour, // ...
&shop_index[i].time_begin.tm_min, // ...
&shop_index[i].time_end.tm_year, // 折扣结束时间
&shop_index[i].time_end.tm_mon, // ...
&shop_index[i].time_end.tm_mday, // ...
&shop_index[i].time_end.tm_hour, // ...
&shop_index[i].time_end.tm_min // ...
);
}
} else {
// 打开特定的订单数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
for (i = 0; i <= 90; i++) {
fprintf(fwrite,
"%s %0.2f %0.2f %d %d %0.2f %04d:%02d:%02d:%02d:%02d "
"%04d:%02d:%02d:%02d:%02d\n",
shop_index[i].goods_name, // 菜品名
shop_index[i].unit_price, // 零售价格
shop_index[i].in_price, // 进货价格
shop_index[i].sales_volume, // 销量
shop_index[i].goods_in_stock, // 存货
shop_index[i].discount_price, // 折扣价
shop_index[i].time_begin.tm_year, // 折扣开始时间
shop_index[i].time_begin.tm_mon, // ...
shop_index[i].time_begin.tm_mday, // ...
shop_index[i].time_begin.tm_hour, // ...
shop_index[i].time_begin.tm_min, // ...
shop_index[i].time_end.tm_year, // 折扣结束时间
shop_index[i].time_end.tm_mon, // ...
shop_index[i].time_end.tm_mday, // ...
shop_index[i].time_end.tm_hour, // ...
shop_index[i].time_end.tm_min // ...
);
}
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
STU_shopping_cart shopping_cart[100]; // 最多存放100笔订单
int database_shopping_cart(char user_id[30], int read_type) {
// 声明读取文件所需指针
FILE *fwrite;
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "shopping_cart/"); // 加入路径"shopping_cart/"
// 处理文件名
strcat(file_name, user_id);
strcat(file_name, ".txt");
int i = 0;
if (!read_type) {
// 打开特定的订单数据文件
if ((fwrite = fopen(file_name, "r")) == NULL) // 判断文件是否存在及可读
return 0;
// 不存在, 返回"0"
// 读取数据
while (!feof(fwrite)) {
fscanf(fwrite, "%s %s %d",
shopping_cart[i].goods_name, // 菜品名
shopping_cart[i].shop_id, // 管理员ID
&shopping_cart[i].purchase_num // 购买数量
);
i++;
}
} else {
// 打开特定的数据文件
fwrite = fopen(file_name, "w+");
// 写入数据
for (i = 0; i <= 98; i++) {
if (shopping_cart[i].purchase_num != -1)
fprintf(fwrite, "%s %s %d\n",
shopping_cart[i].goods_name, // 菜品名
shopping_cart[i].shop_id, // 管理员ID
shopping_cart[i].purchase_num // 购买数量
);
}
}
fclose(fwrite);
return 1; // 成功读写返回"1"
}
/*************************************************
*************************************************/
void database_all_index(int read_type, char goods_name[30], char shop_id[30]) {
char file_name[300];
strcpy(file_name, path); // 该�����符串用于处理文件名
// 处理文件名
strcat(file_name, "all_index.txt");
if (read_type == 1) {
FILE *fwrite;
fwrite = fopen(file_name, "a");
fprintf(fwrite, "%s %s\n", goods_name, shop_id);
fclose(fwrite);
} else {
char goods_name[30];
char shop_id[30];
FILE *fwrite;
fwrite = fopen(file_name, "r");
int i = 0;
while (!feof(fwrite)) {
if (i >= 20)
break;
fscanf(fwrite, "%s %s",
goods_name, // 菜品名
shop_id // 管理员ID
);
if (strcmp(goods_name, "") == 0)
break;
printf("%s : %s\n", goods_name, shop_id);
strcpy(goods_name, "");
strcpy(shop_id, "");
i++;
}
fclose(fwrite);
}
return;
}
/*************************************************
*************************************************/
char name_to_id_goods_id[30];
int database_name_to_id(char goods_name[30], int read_type) {
// 声明读取文件所需指针
FILE *fwrite;
char file_name[300];
strcpy(file_name, path); // 该字符串用于处理文件名
strcat(file_name, "name_to_id/"); // 加入路径"name_to_id/"
// 处理文件名
strcat(file_name, goods_name);
strcat(file_name, ".txt");
if (!read_type) {
if ((fwrite = fopen(file_name, "r")) == NULL)
return 0;
fscanf(fwrite, "%s", name_to_id_goods_id);
} else {
fwrite = fopen(file_name, "w+");
fprintf(fwrite, "%s\n", name_to_id_goods_id);
}
fclose(fwrite);
return 1;
}