-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
3024 lines (2750 loc) · 69.1 KB
/
main.cpp
File metadata and controls
3024 lines (2750 loc) · 69.1 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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
typedef int Status;
#define OK 1
#define ERROR 0
#define USER_FILE "user.txt"
#define PRODUCT_FILE "product.txt"
#define DISCOUNT_FILE "discount.txt"
#define LOG_FILE "sales_log.txt"
#define MAX_CATEGORIES_LENTH 100
// ANSI颜色代码
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
// 文本样式
#define BOLD "\033[1m"
#define UNDERLINE "\033[4m"
// 使用状态机模式
typedef enum
{
MAIN_MENU,
LOGIN,
REGISTER,
FORGOT_PASSWORD,
USER_MENU,
ADMIN_MENU,
VIEW_PRODUCTS,
SEARCH_PRODUCTS,
SHOPPING_CART,
CHECKOUT,
DISCOUNT_INFO,
PRODUCT_MANAGEMENT,
USER_MANAGEMENT,
DISCOUNT_MANAGEMENT,
SALES_REPORT,
SUPPLIER_MANAGEMENT,
EXIT,
TEST
}SystemState;
// 用户结构体
typedef struct User
{
char username[20];
char password[20];
int memberLevel; // 会员等级,0 表示管理员
int points; // 积分
struct User* next;
} UserNode, * UserList;
// 商品结构体
typedef struct Product
{
int id;
char name[50];
char category[2][15];
int stock;
double price;
double discount; // 优惠价(若无优惠价设为 -1)
struct Product* next;
}ProductNode, * ProductList;
// 购物车结构体
typedef struct ShoppingCart
{
int id;
int quantity; // 数量
struct ShoppingCart* next;
} CartNode, * CartList;
// 优惠结构体
typedef struct Discount
{
int id;
char category[2][15]; // 优惠商品类别
double discountRate; // 折扣百分比
double minAmount; // 最低消费金额
time_t endDate; // 结束日期
int minimumLevel; // 会员等级限制,用户等级 >= minimumLevel 才能享受优惠
struct Discount* next;
} DiscountNode, * DiscountList;
// 分类结构体(临时存储)
typedef struct Category
{
char category1[15];
char category2[15];
int productCount; // 商品数量
} CategoryNode, * CategoryList;
// 定义一个临时结构体来存储适用于折扣的商品
typedef struct DiscountItem
{
int productId; // 商品ID
int quantity; // 商品数量
double originalPrice; // 商品原价
char category[2][15]; // 商品分类
} DiscountItem;
void printSeparator(const char* separator_char, const char* color, int width);
void printCentered(const char* text, const char* color, int console_width);
void printHeader();
void printAligned(const char* str, int target_width);
int getDisplayWidth(const char* str);
int getConsoleWidth();
Status initUserList(UserList& L);
Status appendUserList(UserList& L, char* username, char* password, int memberLevel, int points);
Status readUserFromFile(const char* filename, UserList& L);
Status saveUserToFile(const char* filename, UserList L);
Status userExist(UserList L, char* username);
Status updatePassword(UserList L, char* username, char* newPassword);
Status printUserList(UserList L);
Status initProductList(ProductList& L);
Status appendProductList(ProductList& L, int id, char* name, char* category1, char* category2, int stock, double price, double discount);
Status deleteProductNode(ProductList& L, int id);
Status readProductFromFile(const char* filename, ProductList& L);
Status saveProductToFile(const char* filename, ProductList L);
Status productIDExist(ProductList L, int id);
Status getProductInfo(ProductList L, int id, ProductList& info);
Status searchProduct(ProductList L, const char* keyword, int& count);
Status printProductList(ProductList L);
Status addProduct(ProductList& L);
Status updateProduct(ProductList& L);
Status deleteProduct(ProductList& L);
Status generateCategories(ProductList L, CategoryList& categories, int& count);
Status initCartList(CartList& L);
Status appendCartList(CartList L, int id, int quantity);
Status findItemInCart(CartList L, int id, CartList& result);
Status deleteFromCart(CartList& L, int id);
Status initDiscountList(DiscountList& L);
Status addDiscount(DiscountList& L, int id, char category1[15], char category2[15], double discountRate, double minAmount, time_t endDate, int minimumLevel);
Status deleteDiscount(DiscountList& L, int id);
Status deleteExpiredDiscount(DiscountList& L);
Status addDiscountUI(ProductList Product_L, DiscountList& L);
Status readDiscountFromFile(const char* filename, DiscountList& L);
Status saveDiscountToFile(const char* filename, DiscountList L);
Status printDiscountList(DiscountList L);
Status salesLog(int pointsUsed, double totalPay, int pointsEarned);
bool isToday(time_t timestamp);
SystemState mainMenu();
SystemState userLogin(UserList& L);
SystemState userRegister(UserList& L);
SystemState forgotPassword(UserList& L);
SystemState userMenu();
SystemState viewProducts(ProductList& L);
SystemState searchProductUI(ProductList& L);
SystemState shoppingCart(ProductList Product_L);
SystemState checkout(ProductList Product_L, DiscountList Discount_L, UserList& User_L);
SystemState discountInfo(ProductList Product_L, DiscountList Discount_L, UserList& User_L);
SystemState adminMenu();
SystemState productManagement(ProductList& Product_L);
SystemState userManagement(UserList& User_L);
SystemState discountManagement(ProductList Product_L, DiscountList& Discount_L);
SystemState salesReport(ProductList Product_L);
SystemState supplierManagement(ProductList Product_L);
// 定义全局变量,指示当前用户
UserList currUser = NULL;
// 定义全局指针变量,指向购物车链表
CartList Cart_L = NULL;
int main()
{
// 初始化 UserList 并从文件中读取用户信息
UserList User_L;
initUserList(User_L);
readUserFromFile(USER_FILE, User_L);
// 初始化 ProductList
ProductList Product_L;
initProductList(Product_L);
readProductFromFile(PRODUCT_FILE, Product_L);
// 初始化 DiscountList
DiscountList Discount_L;
initDiscountList(Discount_L);
readDiscountFromFile(DISCOUNT_FILE, Discount_L);
deleteExpiredDiscount(Discount_L); // 删除过期的折扣
// 使用状态机模式管理程序流程
SystemState currState = MAIN_MENU;
while (currState != EXIT)
{
switch (currState)
{
case MAIN_MENU:
currState = mainMenu();
break;
case LOGIN:
currState = userLogin(User_L);
break;
case REGISTER:
currState = userRegister(User_L);
break;
case FORGOT_PASSWORD:
currState = forgotPassword(User_L);
break;
case USER_MENU:
currState = userMenu();
break;
case ADMIN_MENU:
currState = adminMenu();
break;
case VIEW_PRODUCTS:
currState = viewProducts(Product_L);
break;
case SEARCH_PRODUCTS:
currState = searchProductUI(Product_L);
break;
case SHOPPING_CART:
currState = shoppingCart(Product_L);
break;
case CHECKOUT:
currState = checkout(Product_L, Discount_L, User_L);
break;
case DISCOUNT_INFO:
currState = discountInfo(Product_L, Discount_L, User_L);
break;
case PRODUCT_MANAGEMENT:
currState = productManagement(Product_L);
break;
case USER_MANAGEMENT:
currState = userManagement(User_L);
break;
case DISCOUNT_MANAGEMENT:
currState = discountManagement(Product_L, Discount_L);
break;
case SALES_REPORT:
currState = salesReport(Product_L);
break;
case SUPPLIER_MANAGEMENT:
currState = supplierManagement(Product_L);
break;
case TEST:
return OK;
}
}
return OK;
}
// 打印一行分隔符
void printSeparator(const char* separator_char, const char* color, int width)
{
int i;
printf("%s", color);
for (i = 0; i < width; i++)
{
printf("%c", *separator_char);
}
printf("%s\n", RESET);
}
// 打印居中字符串
void printCentered(const char* text, const char* color, int console_width)
{
size_t text_len = strlen(text);
int padding = (console_width - text_len) / 2;
int i;
for (i = 0; i < padding; i++)
{
printf(" ");
}
printf("%s%s%s\n", color, text, RESET);
}
void printHeader()
{
int width = getConsoleWidth();
printSeparator("=", CYAN, width);
printCentered("欢迎光临一次买够!!!!!", WHITE, width);
printSeparator("=", CYAN, width);
}
// 打印对齐的字符串
void printAligned(const char* str, int target_width) {
printf("%s", str);
int actual_width = getDisplayWidth(str);
for (int i = 0; i < target_width - actual_width; ++i) {
putchar(' ');
}
}
// 获取字符串的显示宽度(考虑中文字符)
int getDisplayWidth(const char* str) {
int width = 0;
while (*str) {
unsigned char ch = (unsigned char)*str; // 用 unsigned char 来处理字符,是因为有可能是负数
if (ch == '\n' || ch == '\r' || ch == '\t' || ch < 32)
{
str++; // 跳过控制字符
continue;
}
if (ch >= 0x80) {
width += 2;
str += 2;
}
else {
width += 1;
str++;
}
}
return width;
}
// 使用 Windows.h 中的 GetConsoleScreenBufferInfo 获取终端宽度
int getConsoleWidth() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
int width = 80; // 默认宽度
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
}
return width;
}
// 初始化 UserList
Status initUserList(UserList& L)
{
L = new UserNode;
L->next = NULL;
return OK;
}
// 向 UserList 的末尾添加一个节点
Status appendUserList(UserList& L, char* username, char* password, int memberLevel, int points)
{
UserList p, newNode;
// 新建一个节点
newNode = new UserNode;
strcpy(newNode->username, username);
strcpy(newNode->password, password);
newNode->memberLevel = memberLevel;
newNode->points = points;
newNode->next = NULL;
// 将新节点添加到链表末尾
p = L;
while (p->next)
{
p = p->next;
}
p->next = newNode;
return OK;
}
// 从文件读取用户数据并存储到 UserList 中
Status readUserFromFile(const char* filename, UserList& L)
{
FILE* fp;
if ((fp = fopen(filename, "r")) == NULL)
{
return ERROR;
}
char username[20], password[20];
int memberLevel, points;
while (fscanf(fp, "%s %s %d %d", username, password, &memberLevel, &points) != EOF)
{
appendUserList(L, username, password, memberLevel, points);
}
fclose(fp);
return OK;
}
// 将 UserList 中的用户数据保存到文件
Status saveUserToFile(const char* filename, UserList L)
{
FILE* fp;
UserList p = L->next;
if ((fp = fopen(filename, "w")) == NULL)
{
return ERROR;
}
while (p)
{
fprintf(fp, "%s %s %d %d\n", p->username, p->password, p->memberLevel, p->points);
p = p->next;
}
fclose(fp);
return OK;
}
// 判断用户是否存在
Status userExist(UserList L, char* username)
{
UserList p = L->next;
while (p)
{
if (strcmp(p->username, username) == 0)
{
return OK;
}
p = p->next;
}
return ERROR;
}
// 修改用户密码
Status updatePassword(UserList L, char* username, char* newPassword)
{
UserList p = L->next;
while (p)
{
if (strcmp(p->username, username) == 0)
{
strcpy(p->password, newPassword);
return OK;
}
p = p->next;
}
return ERROR;
}
// 打印用户列表
Status printUserList(UserList L)
{
UserList p = L->next;
if (p == NULL)
{
printf("用户列表为空\n");
return ERROR;
}
printAligned("用户名", 20);
printAligned("会员等级", 10);
printAligned("积分", 10);
printf("\n");
printSeparator("-", WHITE, getConsoleWidth());
int count = 0;
while (p)
{
if (p->memberLevel == 0) // 管理员不显示
{
p = p->next;
continue;
}
printf("%-20s", p->username);
printf("%-10d", p->memberLevel);
printf("%-10d", p->points);
printf("\n");
p = p->next;
count++;
}
printSeparator("-", WHITE, getConsoleWidth());
printf("共 %d 位用户\n", count);
return OK;
}
// 初始化 ProductList
Status initProductList(ProductList& L)
{
L = new ProductNode;
L->next = NULL;
return OK;
}
// 向 ProductList 的末尾添加一个节点
Status appendProductList(ProductList& L, int id, char* name, char* category1, char* category2, int stock, double price, double discount)
{
ProductList p, newNode;
// 新建一个节点
newNode = new ProductNode;
newNode->id = id;
strcpy(newNode->name, name);
strcpy(newNode->category[0], category1);
strcpy(newNode->category[1], category2);
newNode->stock = stock;
newNode->price = price;
newNode->discount = discount;
newNode->next = NULL;
// 将新节点添加到链表末尾
p = L;
while (p->next)
{
p = p->next;
}
p->next = newNode;
return OK;
}
// 从 ProductList 中删除一个节点
Status deleteProductNode(ProductList& L, int id)
{
ProductList p = L->next;
ProductList prev = L;
while (p)
{
if (p->id == id)
{
prev->next = p->next;
delete p;
return OK;
}
prev = p;
p = p->next;
}
return ERROR; // 商品 ID 不存在
}
// 从文件中读取 ProductList
Status readProductFromFile(const char* filename, ProductList& L)
{
FILE* fp;
if ((fp = fopen(filename, "r")) == NULL)
{
return ERROR;
}
int id, stock;
char name[50] = { 0 };
double price, discount;
char category_t[31], category1[15], category2[15];
while (fscanf(fp, "%d %s %s %d %lf %lf", &id, name, category_t, &stock, &price, &discount) != EOF)
{
// 将商品名称中的逗号替换为空格
for (int i = 0; name[i] != '\0'; i++)
{
if (name[i] == ',')
{
name[i] = ' ';
}
}
// 处理商品分类
char* token;
token = strtok(category_t, "@");
strcpy(category1, token);
token = strtok(NULL, "@");
if (token != NULL)
{
strcpy(category2, token);
}
else
{
category2[0] = '\0';
}
appendProductList(L, id, name, category1, category2, stock, price, discount);
}
fclose(fp);
return OK;
}
Status saveProductToFile(const char* filename, ProductList L)
{
FILE* fp;
ProductList p = L->next;
if ((fp = fopen(filename, "w")) == NULL)
{
return ERROR;
}
while (p) // 遍历商品列表,逐个写入文件
{
// 将商品名称中的空格替换为逗号
char name[50];
strcpy(name, p->name);
for (int i = 0; name[i] != '\0'; i++)
{
if (name[i] == ' ')
{
name[i] = ',';
}
}
// 拼接商品分类,格式为 "一级分类@二级分类"(如果二级分类为空,则只写一级分类)
char category_t[31];
strcpy(category_t, p->category[0]);
if (p->category[1][0] != '\0')
{
strcat(category_t, "@");
strcat(category_t, p->category[1]);
}
// 写入文件,格式:id name category_t stock price discount
fprintf(fp, "%d %s %s %d %.2f %.2f\n", p->id, name, category_t, p->stock, p->price, p->discount);
p = p->next;
}
fclose(fp);
return OK;
}
// 判断商品 ID 是否存在
Status productIDExist(ProductList L, int id)
{
ProductList p = L->next;
while (p)
{
if (p->id == id)
{
return OK;
}
p = p->next;
}
return ERROR;
}
// 根据商品 ID 返回商品信息
Status getProductInfo(ProductList L, int id, ProductList& info)
{
if (L->next == NULL)
{
return ERROR;
}
ProductList p = L->next;
while (p)
{
if (p->id == id)
{
info = p;
return OK;
}
p = p->next;
}
return ERROR;
}
Status searchProduct(ProductList L, const char* keyword, int& count)
{
ProductList p = L->next;
int found = 0;
count = 0;
while (p)
{
if (strstr(p->name, keyword) != NULL || strstr(p->category[0], keyword) != NULL || strstr(p->category[1], keyword) != NULL)
{
if (currUser != 0 && p->stock == 0) // 普通用户不显示库存为 0 的商品
{
p = p->next;
continue;
}
else if (found == 0) // 首次找到匹配商品时打印表头
{
printAligned("ID", 4);
printAligned("商品名称", 50);
printAligned("商品分类", 30);
printAligned("库存", 10);
printAligned("价格", 10);
printAligned("优惠价", 10);
printf("\n");
printSeparator("-", WHITE, getConsoleWidth());
found = 1;
}
char category[31];
strcpy(category, p->category[0]);
if (p->category[1][0] != '\0') // 拼接商品分类
{
strcat(category, "->");
strcat(category, p->category[1]);
}
// 打印商品信息
printf("%-4d", p->id);
printAligned(p->name, 50);
printAligned(category, 30);
printf("%-10d", p->stock);
printf("%-10.2f", p->price);
if (p->discount != -1)
{
printf("%-10.2f", p->discount);
}
else
{
printf("%-10s", "无");
}
printf("\n");
count++;
}
p = p->next;
}
if (!found)
{
printf("%s没有找到匹配的商品%s\n\n", RED, RESET);
return ERROR;
}
else
{
printSeparator("-", WHITE, getConsoleWidth());
printf("%s共找到 %d 件商品%s\n\n", GREEN, count, RESET);
return OK;
}
}
// 打印商品列表
Status printProductList(ProductList L)
{
ProductList p = L->next;
char category[31];
int count = 0, sold_out = 0;
printAligned("ID", 4);
printAligned("商品名称", 50);
printAligned("商品分类", 30);
printAligned("库存", 10);
printAligned("价格", 10);
printAligned("优惠价", 10);
printf("\n");
printSeparator("-", WHITE, getConsoleWidth());
while (p)
{
if (p->price == -1 && currUser->memberLevel != 0) // 普通用户不显示赠品
{
p = p->next;
continue;
}
if (p->stock == 0) // 普通用户不显示库存为 0 的商品
{
sold_out++;
if (currUser->memberLevel != 0)
{
p = p->next;
continue;
}
else
{
printf("%s", YELLOW);
}
}
strcpy(category, p->category[0]);
if (p->category[1][0] != '\0')
{
strcat(category, "->");
strcat(category, p->category[1]);
}
printf("%-4d", p->id);
printAligned(p->name, 50);
printAligned(category, 30);
printf("%-10d", p->stock);
if (p->price != -1)
{
printf("%-10.2f", p->price);
if (p->discount != -1)
{
printf("%-10.2f", p->discount);
}
else
{
printf("%-10s", "无");
}
}
else
{
printAligned("赠品", 10);
printf("满%.2lf赠送", p->discount);
}
printf("%s\n", RESET);
count++;
p = p->next;
}
printSeparator("-", WHITE, getConsoleWidth());
printf("%s共找到 %d 件商品%s", GREEN, count, RESET);
if (currUser->memberLevel == 0)
{
printf(",其中 %d 件商品已售罄,%s请及时补货!%s", sold_out, RED, RESET);
}
printf("\n\n");
return OK;
}
Status addProduct(ProductList& L)
{
int max_id = 0, id, stock;
double price, discount;
char name[50], category1[15], category2[15];
ProductList p = L->next;
while (p)
{
if (p->id > max_id)
{
max_id = p->id;
}
p = p->next;
}
id = max_id + 1; // 新商品 ID 从最大 ID + 1 开始
while (1)
{
printf("商品名称: ");
scanf(" %[^\n]", name); // 读取包含空格的字符串
if (strlen(name) == 0) // 判断是否为空
{
printf("%s商品名称不能为空,请重新输入!%s\n", BOLD RED, RESET);
continue;
}
else
{
break;
}
}
while (1)
{
printf("商品一级分类(必填): ");
scanf("%s", category1);
if (strlen(category1) == 0)
{
printf("%s商品一级分类不能为空,请重新输入!%s\n", BOLD RED, RESET);
continue;
}
else
{
break;
}
}
// 清除输入缓冲区
getchar();
printf("商品二级分类(若无则留空): ");
fgets(category2, sizeof(category2), stdin);
category2[strcspn(category2, "\n")] = 0; // 去除末尾换行符
if (strlen(category2) == 0)
{
category2[0] = '\0'; // 如果没有输入二级分类,则将其设置为空字符串
}
while (1)
{
printf("商品数量: ");
scanf("%d", &stock);
if (stock <= 0)
{
printf("%s商品数量必须大于 0,请重新输入!%s\n", BOLD RED, RESET);
}
else
{
break;
}
}
while (1)
{
printf("商品价格: ");
scanf("%lf", &price);
if (price <= 0)
{
printf("%s商品价格必须大于 0,请重新输入!%s\n", BOLD RED, RESET);
}
else
{
break;
}
}
while (1)
{
printf("商品优惠价(若无优惠则输入 -1): ");
scanf("%lf", &discount);
if (discount == -1 || discount > 0 && discount < price)
{
break;
}
else
{
printf("%s优惠价必须介于 0 和商品价格之间,请重新输入!%s\n", BOLD RED, RESET);
}
}
// 添加商品到链表
appendProductList(L, id, name, category1, category2, stock, price, discount);
printf("%s商品 %s 添加成功!ID:%d%s\n\n", GREEN, name, id, RESET);
return OK;
}
// 修改商品信息
Status updateProduct(ProductList& L)
{
int id;
printf("请输入要修改的商品 ID(输入 0 取消修改):");
scanf("%d", &id);
if (id == 0)
{
return OK;
}
ProductList info;
getProductInfo(L, id, info);
if (info == NULL)
{
printf("%s商品 ID 不存在!%s\n", BOLD RED, RESET);
return ERROR;
}
printf("选中的商品:%s%s%s\n\n", YELLOW, info->name, RESET);
printf("选择要修改的信息:\n");
printf("1. 商品名称\n");
printf("2. 一级分类\n");
printf("3. 二级分类\n");
printf("4. 库存\n");
printf("5. 价格\n");
printf("6. 优惠价\n");
printf("0. 取消修改\n");
printf("%s请输入序号:%s ", YELLOW, RESET);
int choice;
scanf("%d", &choice);
printf("\n");
switch (choice)
{
case 1:
{
printf("当前商品名称: %s\n", info->name);
printf("请输入新的商品名称: ");
char name[50];
scanf(" %[^\n]", name);
strcpy(info->name, name);
printf("%s商品名已修改为 %s%s\n\n", BOLD GREEN, info->name, RESET);
break;
}
case 2:
{
printf("当前一级分类: %s\n", info->category[0]);
printf("请输入新的一级分类: ");
char category1[15];
scanf("%s", category1);
strcpy(info->category[0], category1);
printf("%s一级分类已修改为 %s%s\n\n", BOLD GREEN, info->category[0], RESET);
break;
}
case 3:
{
printf("当前二级分类: %s\n", strlen(info->category[1]) == 0 ? "无" : info->category[1]);
printf("请输入新的二级分类(留空表示无): ");
char category2[15];
fgets(category2, sizeof(category2), stdin);
category2[strcspn(category2, "\n")] = 0; // 去除末尾换行符
if (strlen(category2) == 0) {
info->category[1][0] = '\0'; // 设置为空字符串
printf("%s二级分类已清空%s\n\n", BOLD GREEN, RESET);
}
else {
strcpy(info->category[1], category2);
printf("%s二级分类已修改为 %s%s\n\n", BOLD GREEN, info->category[1], RESET);
}
break;
}
case 4:
{
printf("当前库存: %d\n", info->stock);
printf("请输入新的库存: ");
int stock;
scanf("%d", &stock);
info->stock = stock;
printf("%s库存已修改为 %d%s\n\n", BOLD GREEN, info->stock, RESET);
break;
}
case 5:
{
printf("当前价格: %.2f\n", info->price);
printf("请输入新的价格: ");
double price;
scanf("%lf", &price);
info->price = price;
printf("%s价格已修改为 %.2f%s\n\n", BOLD GREEN, info->price, RESET);
break;
}
case 6: