#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_PRODUCTS 1000
#define MAX_USERS 500
#define MAX_ORDERS 5000
#define MAX_NAME_LENGTH 50
#define MAX_ADDRESS_LENGTH 100
#define MAX_CATEGORY_LENGTH 30
#define RESTOCK_THRESHOLD 10
#define CLEANUP_DAYS 30
// 商品信息结构体
typedef struct {
int id; // 商品ID
char name[MAX_NAME_LENGTH]; // 商品名称
char category[MAX_CATEGORY_LENGTH]; // 商品类别
float price; // 商品价格
int stock; // 库存数量
int sales; // 销售数量统计
} Product;
// 用户信息结构体
typedef struct {
int id; // 用户ID
char name[MAX_NAME_LENGTH]; // 用户姓名
char address[MAX_ADDRESS_LENGTH]; // 用户地址
char contact[MAX_NAME_LENGTH]; // 联系方式
} User;
// 订单状态枚举
typedef enum {
PENDING_PAYMENT, // 待付款
PAID, // 已付款
SHIPPED, // 已发货
COMPLETED // 已完成
} OrderStatus;
// 订单信息结构体
typedef struct {
int id; // 订单ID
int user_id; // 用户ID
int product_id; // 商品ID
int quantity; // 商品数量
time_t order_time; // 下单时间
OrderStatus status; // 订单状态
} Order;
// 补货任务结构体
typedef struct {
int product_id; // 商品ID
int priority; // 优先级(1-高优先级, 2-中优先级, 3-低优先级)
} RestockTask;
// 商品类别树节点
typedef struct CategoryNode {
char name[MAX_CATEGORY_LENGTH]; // 类别名称
struct CategoryNode *parent; // 父类别
struct CategoryNode *children[MAX_PRODUCTS]; // 子类别
int child_count; // 子类别数量
int product_count; // 该类别下商品数量
float sales_total; // 该类别销售总额
} CategoryNode;
// 全局数据存储
Product products[MAX_PRODUCTS]; // 商品数组
int product_count = 0; // 商品数量
User users[MAX_USERS]; // 用户数组
int user_count = 0; // 用户数量
Order orders[MAX_ORDERS]; // 订单数组
int order_count = 0; // 订单数量
RestockTask restock_queue[MAX_PRODUCTS]; // 补货任务队列
int restock_count = 0; // 补货任务数量
// 栈结构用于订单状态变更历史
typedef struct {
int order_id; // 订单ID
OrderStatus old_status; // 原状态
time_t timestamp; // 变更时间
} OrderHistoryItem;
OrderHistoryItem order_history[MAX_ORDERS]; // 订单历史记录
int history_count = 0; // 历史记录数量
// 哈希表结构用于用户信息快速查找
typedef struct HashNode {
int key; // 键(用户ID)
User *user; // 用户指针
struct HashNode *next; // 链表指针
} HashNode;
HashNode *user_hash_table[100]; // 用户哈希表
// 文件路径定义
const char *PRODUCTS_FILE = "D:\\c\\text\\products.csv";
const char *USERS_FILE = "D:\\c\\text\\users.csv";
const char *ORDERS_FILE = "D:\\c\\text\\orders.csv";
// 函数声明
void initialize_system(); // 初始化系统
void initialize_hash_table(); // 初始化哈希表
int add_product(int id, const char *name, const char *category, float price, int stock); // 添加商品
int find_product_by_id(int id); // 根据ID查找商品
int find_product_by_name(const char *name); // 根据名称查找商品
void sort_products_by_name(); // 按名称排序商品
void print_product(int index); // 打印商品信息
int add_user(int id, const char *name, const char *address, const char *contact); // 添加用户
User* find_user_by_id(int id); // 根据ID查找用户
int find_user_by_name(const char *name); // 根据名称查找用户
void print_user(int index); // 打印用户信息
int create_order(int user_id, int product_id, int quantity); // 创建订单
int process_payment(int order_id); // 处理付款
int process_shipment(int order_id); // 处理发货
int confirm_receipt(int order_id); // 确认收货
Order* find_order_by_id(int id); // 根据ID查找订单
void sort_orders_by_time(); // 按时间排序订单
void print_order(int index); // 打印订单信息
int update_stock(int product_id, int quantity); // 更新库存
void check_low_stock(); // 检查低库存
void process_restock_tasks(); // 处理补货任务
void push_order_history(int order_id, OrderStatus old_status); // 记录订单历史
OrderHistoryItem pop_order_history(); // 获取订单历史
int has_order_history(int order_id); // 检查订单是否有历史记录
void enqueue_restock_task(int product_id, int priority); // 添加补货任务
RestockTask dequeue_restock_task(); // 获取补货任务
int is_restock_queue_empty(); // 检查补货队列是否为空
CategoryNode* create_category_node(const char *name); // 创建类别节点
void add_child_category(CategoryNode *parent, CategoryNode *child); // 添加子类别
void traverse_category_tree(CategoryNode *node, int depth); // 遍历类别树
void update_category_stats(CategoryNode *node, Product *product, float sales_amount); // 更新类别统计
unsigned int hash_function(int key); // 哈希函数
void hash_table_insert(int key, User *user); // 哈希表插入
User* hash_table_lookup(int key); // 哈希表查找
void print_system_status(); // 打印系统状态
void cleanup_old_orders(); // 清理旧订单
void show_menu(); // 显示菜单
void input_product(); // 输入商品信息
void list_products(); // 列出所有商品
void input_user(); // 输入用户信息
void list_users(); // 列出所有用户
void input_order(); // 输入订单信息
void list_orders(); // 列出所有订单
void search_product(); // 搜索商品
void search_user(); // 搜索用户
void search_order(); // 搜索订单
void wait_for_user(); // 等待用户输入
// 从文件加载商品数据
int load_products_from_file() {
FILE *file = fopen(PRODUCTS_FILE, "r");
if (file == NULL) {
printf("警告: 商品文件不存在,将使用空数据初始化\n");
return 0;
}
char line[512];
int count = 0;
// 跳过标题行
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return 0;
}
while (fgets(line, sizeof(line), file) != NULL && product_count < MAX_PRODUCTS) {
Product p;
char *token;
// 解析ID
token = strtok(line, ",");
if (token == NULL) continue;
p.id = atoi(token);
// 解析名称
token = strtok(NULL, ",");
if (token == NULL) continue;
strncpy(p.name, token, MAX_NAME_LENGTH-1);
p.name[MAX_NAME_LENGTH-1] = '\0';
// 解析类别
token = strtok(NULL, ",");
if (token == NULL) continue;
strncpy(p.category, token, MAX_CATEGORY_LENGTH-1);
p.category[MAX_CATEGORY_LENGTH-1] = '\0';
// 解析价格
token = strtok(NULL, ",");
if (token == NULL) continue;
p.price = atof(token);
// 解析库存
token = strtok(NULL, ",");
if (token == NULL) continue;
p.stock = atoi(token);
// 解析销量
token = strtok(NULL, ",\n");
if (token != NULL) {
p.sales = atoi(token);
} else {
p.sales = 0;
}
// 添加商品
if (add_product(p.id, p.name, p.category, p.price, p.stock) != -1) {
// 更新销量
products[product_count-1].sales = p.sales;
count++;
}
}
fclose(file);
printf("从文件加载了 %d 个商品\n", count);
return count;
}
// 从文件加载用户数据
int load_users_from_file() {
FILE *file = fopen(USERS_FILE, "r");
if (file == NULL) {
printf("警告: 用户文件不存在,将使用空数据初始化\n");
return 0;
}
char line[512];
int count = 0;
// 跳过标题行
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return 0;
}
while (fgets(line, sizeof(line), file) != NULL && user_count < MAX_USERS) {
User u;
char *token;
// 解析ID
token = strtok(line, ",");
if (token == NULL) continue;
u.id = atoi(token);
// 解析名称
token = strtok(NULL, ",");
if (token == NULL) continue;
strncpy(u.name, token, MAX_NAME_LENGTH-1);
u.name[MAX_NAME_LENGTH-1] = '\0';
// 解析地址
token = strtok(NULL, ",");
if (token == NULL) continue;
strncpy(u.address, token, MAX_ADDRESS_LENGTH-1);
u.address[MAX_ADDRESS_LENGTH-1] = '\0';
// 解析联系方式
token = strtok(NULL, ",\n");
if (token != NULL) {
strncpy(u.contact, token, MAX_NAME_LENGTH-1);
u.contact[MAX_NAME_LENGTH-1] = '\0';
} else {
u.contact[0] = '\0';
}
// 添加用户
if (add_user(u.id, u.name, u.address, u.contact) != -1) {
count++;
}
}
fclose(file);
printf("从文件加载了 %d 个用户\n", count);
return count;
}
// 从文件加载订单数据
int load_orders_from_file() {
FILE *file = fopen(ORDERS_FILE, "r");
if (file == NULL) {
printf("警告: 订单文件不存在,将使用空数据初始化\n");
return 0;
}
char line[512];
int count = 0;
// 跳过标题行
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return 0;
}
while (fgets(line, sizeof(line), file) != NULL && order_count < MAX_ORDERS) {
Order o;
char *token;
// 解析ID
token = strtok(line, ",");
if (token == NULL) continue;
o.id = atoi(token);
// 解析用户ID
token = strtok(NULL, ",");
if (token == NULL) continue;
o.user_id = atoi(token);
// 解析商品ID
token = strtok(NULL, ",");
if (token == NULL) continue;
o.product_id = atoi(token);
// 解析数量
token = strtok(NULL, ",");
if (token == NULL) continue;
o.quantity = atoi(token);
// 解析下单时间
token = strtok(NULL, ",");
if (token == NULL) continue;
o.order_time = (time_t)atol(token);
// 解析状态
token = strtok(NULL, ",\n");
if (token == NULL) continue;
int status = atoi(token);
if (status >= PENDING_PAYMENT && status <= COMPLETED) {
o.status = (OrderStatus)status;
} else {
o.status = PENDING_PAYMENT;
}
// 检查用户和商品是否存在
User *user = find_user_by_id(o.user_id);
if (user == NULL) {
printf("警告: 订单 %d 的用户ID %d 不存在,跳过此订单\n", o.id, o.user_id);
continue;
}
if (find_product_by_id(o.product_id) == -1) {
printf("警告: 订单 %d 的商品ID %d 不存在,跳过此订单\n", o.id, o.product_id);
continue;
}
// 添加订单(绕过库存检查,直接添加)
orders[order_count] = o;
order_count++;
count++;
}
fclose(file);
printf("从文件加载了 %d 个订单\n", count);
return count;
}
// 添加商品
int add_product(int id, const char *name, const char *category, float price, int stock) {
// 检查商品ID是否已存在
if (find_product_by_id(id) != -1) {
printf("错误: 商品ID %d 已存在\n", id);
return -1;
}
// 检查商品数量是否超过最大值
if (product_count >= MAX_PRODUCTS) {
printf("错误: 商品数量已达到最大值\n");
return -1;
}
// 添加商品
products[product_count].id = id;
strncpy(products[product_count].name, name, MAX_NAME_LENGTH-1);
strncpy(products[product_count].category, category, MAX_CATEGORY_LENGTH-1);
products[product_count].price = price;
products[product_count].stock = stock;
products[product_count].sales = 0;
product_count++;
return 0;
}
// 根据ID查找商品
int find_product_by_id(int id) {
for (int i = 0; i < product_count; i++) {
if (products[i].id == id) {
return i;
}
}
return -1; // 未找到
}
// 根据名称查找商品
int find_product_by_name(const char *name) {
for (int i = 0; i < product_count; i++) {
if (strcmp(products[i].name, name) == 0) {
return i;
}
}
return -1; // 未找到
}
// 按名称排序商品
void sort_products_by_name() {
for (int i = 0; i < product_count - 1; i++) {
for (int j = i + 1; j < product_count; j++) {
if (strcmp(products[i].name, products[j].name) > 0) {
// 交换商品
Product temp = products[i];
products[i] = products[j];
products[j] = temp;
}
}
}
}
// 打印商品信息
void print_product(int index) {
if (index < 0 || index >= product_count) {
printf("错误: 无效的商品索引\n");
return;
}
printf("商品ID: %d\n", products[index].id);
printf("商品名称: %s\n", products[index].name);
printf("商品类别: %s\n", products[index].category);
printf("商品价格: %.2f\n", products[index].price);
printf("库存数量: %d\n", products[index].stock);
printf("销售数量: %d\n", products[index].sales);
printf("------------------------\n");
}
// 添加用户
int add_user(int id, const char *name, const char *address, const char *contact) {
// 检查用户ID是否已存在
if (find_user_by_id(id) != NULL) {
printf("错误: 用户ID %d 已存在\n", id);
return -1;
}
// 检查用户数量是否超过最大值
if (user_count >= MAX_USERS) {
printf("错误: 用户数量已达到最大值\n");
return -1;
}
// 添加用户
users[user_count].id = id;
strncpy(users[user_count].name, name, MAX_NAME_LENGTH-1);
strncpy(users[user_count].address, address, MAX_ADDRESS_LENGTH-1);
strncpy(users[user_count].contact, contact, MAX_NAME_LENGTH-1);
// 添加到哈希表
hash_table_insert(id, &users[user_count]);
user_count++;
return 0;
}
// 根据ID查找用户
User* find_user_by_id(int id) {
return hash_table_lookup(id);
}
// 根据名称查找用户
int find_user_by_name(const char *name) {
for (int i = 0; i < user_count; i++) {
if (strcmp(users[i].name, name) == 0) {
return i;
}
}
return -1; // 未找到
}
// 打印用户信息
void print_user(int index) {
if (index < 0 || index >= user_count) {
printf("错误: 无效的用户索引\n");
return;
}
printf("用户ID: %d\n", users[index].id);
printf("用户姓名: %s\n", users[index].name);
printf("用户地址: %s\n", users[index].address);
printf("联系方式: %s\n", users[index].contact);
printf("------------------------\n");
}
// 创建订单
int create_order(int user_id, int product_id, int quantity) {
// 检查用户是否存在
if (find_user_by_id(user_id) == NULL) {
printf("错误: 用户ID %d 不存在\n", user_id);
return -1;
}
// 检查商品是否存在
int product_index = find_product_by_id(product_id);
if (product_index == -1) {
printf("错误: 商品ID %d 不存在\n", product_id);
return -1;
}
// 检查库存是否充足
if (products[product_index].stock < quantity) {
printf("错误: 商品库存不足,现有库存: %d\n", products[product_index].stock);
return -1;
}
// 检查订单数量是否超过最大值
if (order_count >= MAX_ORDERS) {
printf("错误: 订单数量已达到最大值\n");
return -1;
}
// 创建订单
orders[order_count].id = order_count + 1; // 订单ID从1开始
orders[order_count].user_id = user_id;
orders[order_count].product_id = product_id;
orders[order_count].quantity = quantity;
orders[order_count].order_time = time(NULL); // 当前时间
orders[order_count].status = PENDING_PAYMENT; // 初始状态为待付款
// 更新库存
update_stock(product_id, -quantity);
order_count++;
printf("订单创建成功,订单ID: %d\n", orders[order_count-1].id);
return orders[order_count-1].id;
}
// 处理付款
int process_payment(int order_id) {
Order *order = find_order_by_id(order_id);
if (order == NULL) {
printf("错误: 订单ID %d 不存在\n", order_id);
return 0;
}
if (order->status != PENDING_PAYMENT) {
printf("错误: 订单状态不是未付款状态,当前状态: ");
switch (order->status) {
case PAID: printf("已付款\n"); break;
case SHIPPED: printf("已发货\n"); break;
case COMPLETED: printf("已完成\n"); break;
default: printf("未知状态\n");
}
return 0;
}
// 记录历史
push_order_history(order_id, order->status);
// 更新状态
order->status = PAID;
printf("订单 %d 付款处理成功,当前状态: 已付款\n", order_id);
return 1;
}
// 处理发货
int process_shipment(int order_id) {
Order *order = find_order_by_id(order_id);
if (order == NULL) {
printf("错误: 订单ID %d 不存在\n", order_id);
return 0;
}
if (order->status != PAID) {
printf("错误: 订单状态不是已付款状态,当前状态: ");
switch (order->status) {
case PENDING_PAYMENT: printf("未付款\n"); break;
case SHIPPED: printf("已发货\n"); break;
case COMPLETED: printf("已完成\n"); break;
default: printf("未知状态\n");
}
return 0;
}
// 记录历史
push_order_history(order_id, order->status);
// 更新状态
order->status = SHIPPED;
printf("订单 %d 发货处理成功,当前状态: 已发货\n", order_id);
return 1;
}
// 确认收货
int confirm_receipt(int order_id) {
Order *order = find_order_by_id(order_id);
if (order == NULL) {
printf("错误: 订单ID %d 不存在\n", order_id);
return 0;
}
if (order->status != SHIPPED) {
printf("错误: 订单状态不是已发货状态,当前状态: ");
switch (order->status) {
case PENDING_PAYMENT: printf("未付款\n"); break;
case PAID: printf("已付款\n"); break;
case COMPLETED: printf("已完成\n"); break;
default: printf("未知状态\n");
}
return 0;
}
// 记录历史
push_order_history(order_id, order->status);
// 更新状态
order->status = COMPLETED;
// 更新商品销量
int product_index = find_product_by_id(order->product_id);
if (product_index != -1) {
products[product_index].sales += order->quantity;
}
printf("订单 %d 收货确认成功,当前状态: 已完成\n", order_id);
return 1;
}
// 根据ID查找订单
Order* find_order_by_id(int id) {
for (int i = 0; i < order_count; i++) {
if (orders[i].id == id) {
return &orders[i];
}
}
return NULL; // 未找到
}
// 按时间排序订单
void sort_orders_by_time() {
for (int i = 0; i < order_count - 1; i++) {
for (int j = i + 1; j < order_count; j++) {
if (orders[i].order_time > orders[j].order_time) {
// 交换订单
Order temp = orders[i];
orders[i] = orders[j];
orders[j] = temp;
}
}
}
}
// 打印订单信息
void print_order(int index) {
if (index < 0 || index >= order_count) {
printf("错误: 无效的订单索引\n");
return;
}
char status_str[20];
switch (orders[index].status) {
case PENDING_PAYMENT: strcpy(status_str, "待付款"); break;
case PAID: strcpy(status_str, "已付款"); break;
case SHIPPED: strcpy(status_str, "已发货"); break;
case COMPLETED: strcpy(status_str, "已完成"); break;
default: strcpy(status_str, "未知状态");
}
User *user = find_user_by_id(orders[index].user_id);
int product_index = find_product_by_id(orders[index].product_id);
printf("订单ID: %d\n", orders[index].id);
printf("用户信息: %s (ID: %d)\n", user ? user->name : "未知用户", orders[index].user_id);
printf("商品信息: %s (ID: %d)\n", product_index != -1 ? products[product_index].name : "未知商品", orders[index].product_id);
printf("数量: %d\n", orders[index].quantity);
if (product_index != -1) {
printf("总价: %.2f\n", products[product_index].price * orders[index].quantity);
}
printf("下单时间: %s", ctime(&orders[index].order_time));
printf("订单状态: %s\n", status_str);
printf("------------------------\n");
}
// 更新库存
int update_stock(int product_id, int quantity) {
int index = find_product_by_id(product_id);
if (index == -1) {
printf("错误: 商品ID %d 不存在\n", product_id);
return -1;
}
// 检查库存是否充足(仅当减少库存时)
if (quantity < 0 && products[index].stock < (-quantity)) {
printf("错误: 商品库存不足,现有库存: %d\n", products[index].stock);
return -1;
}
products[index].stock += quantity;
return 0;
}
// 检查低库存
void check_low_stock() {
printf("低库存商品列表:\n");
int low_stock_count = 0;
for (int i = 0; i < product_count; i++) {
if (products[i].stock <= RESTOCK_THRESHOLD) {
printf("商品ID: %d, 商品名称: %s, 库存: %d\n",
products[i].id, products[i].name, products[i].stock);
// 添加补货任务
int priority = (products[i].stock == 0) ? 1 : 2; // 缺货为高优先级
enqueue_restock_task(products[i].id, priority);
low_stock_count++;
}
}
if (low_stock_count == 0) {
printf("所有商品库存充足\n");
}
}
// 处理补货任务
void process_restock_tasks() {
if (is_restock_queue_empty()) {
printf("没有需要处理的补货任务\n");
return;
}
printf("处理补货任务:\n");
while (!is_restock_queue_empty()) {
RestockTask task = dequeue_restock_task();
int product_index = find_product_by_id(task.product_id);
if (product_index != -1) {
// 增加库存(模拟补货100个)
update_stock(task.product_id, 100);
printf("已补货: 商品ID %d, 商品名称: %s, 优先级: %d\n",
task.product_id, products[product_index].name, task.priority);
} else {
printf("错误: 商品ID %d 不存在,无法补货\n", task.product_id);
}
}
}
// 记录订单历史
void push_order_history(int order_id, OrderStatus old_status) {
if (history_count >= MAX_ORDERS) {
printf("错误: 订单历史记录已满\n");
return;
}
order_history[history_count].order_id = order_id;
order_history[history_count].old_status = old_status;
order_history[history_count].timestamp = time(NULL);
history_count++;
}
// 获取订单历史
OrderHistoryItem pop_order_history() {
OrderHistoryItem item;
item.order_id = -1;
item.old_status = PENDING_PAYMENT;
item.timestamp = 0;
if (history_count <= 0) {
printf("错误: 订单历史记录为空\n");
return item;
}
history_count--;
return order_history[history_count];
}
// 检查订单是否有历史记录
int has_order_history(int order_id) {
for (int i = 0; i < history_count; i++) {
if (order_history[i].order_id == order_id) {
return 1;
}
}
return 0;
}
// 添加补货任务
void enqueue_restock_task(int product_id, int priority) {
if (restock_count >= MAX_PRODUCTS) {
printf("错误: 补货任务队列已满\n");
return;
}
restock_queue[restock_count].product_id = product_id;
restock_queue[restock_count].priority = priority;
restock_count++;
// 按优先级排序
for (int i = 0; i < restock_count - 1; i++) {
for (int j = i + 1; j < restock_count; j++) {
if (restock_queue[i].priority > restock_queue[j].priority) {
// 交换任务
RestockTask temp = restock_queue[i];
restock_queue[i] = restock_queue[j];
restock_queue[j] = temp;
}
}
}
}
// 获取补货任务
RestockTask dequeue_restock_task() {
RestockTask task;
task.product_id = -1;
task.priority = 0;
if (restock_count <= 0) {
printf("错误: 补货任务队列为空\n");
return task;
}
// 获取优先级最高的任务(队列头部)
task = restock_queue[0];
// 将后面的任务前移
for (int i = 0; i < restock_count - 1; i++) {
restock_queue[i] = restock_queue[i + 1];
}
restock_count--;
return task;
}
// 检查补货队列是否为空
int is_restock_queue_empty() {
return restock_count == 0;
}
// 创建类别节点
CategoryNode* create_category_node(const char *name) {
CategoryNode *node = (CategoryNode*)malloc(sizeof(CategoryNode));
if (node == NULL) {
printf("错误: 内存分配失败\n");
return NULL;
}
strncpy(node->name, name, MAX_CATEGORY_LENGTH-1);
node->parent = NULL;
node->child_count = 0;
node->product_count = 0;
node->sales_total = 0.0;
for (int i = 0; i < MAX_PRODUCTS; i++) {
node->children[i] = NULL;
}
return node;
}
// 添加子类别
void add_child_category(CategoryNode *parent, CategoryNode *child) {
if (parent == NULL || child == NULL) {
return;
}
if (parent->child_count >= MAX_PRODUCTS) {
printf("错误: 类别子节点数量已达到最大值\n");
return;
}
child->parent = parent;
parent->children[parent->child_count] = child;
parent->child_count++;
}
// 遍历类别树
void traverse_category_tree(CategoryNode *node, int depth) {
if (node == NULL) {
return;
}
// 缩进显示层级
for (int i = 0; i < depth; i++) {
printf(" ");
}
printf("%s (商品数量: %d, 销售总额: %.2f)\n",
node->name, node->product_count, node->sales_total);
// 递归遍历子类别
for (int i = 0; i < node->child_count; i++) {
traverse_category_tree(node->children[i], depth + 1);
}
}
// 更新类别统计
void update_category_stats(CategoryNode *node, Product *product, float sales_amount) {
if (node == NULL || product == NULL) {
return;
}
// 更新当前类别统计
node->product_count++;
node->sales_total += sales_amount;
// 递归更新父类别统计
if (node->parent != NULL) {
update_category_stats(node->parent, product, sales_amount);
}
}
// 哈希函数
unsigned int hash_function(int key) {
return key % 100;
}
// 哈希表插入
void hash_table_insert(int key, User *user) {
unsigned int index = hash_function(key);
HashNode *new_node = (HashNode*)malloc(sizeof(HashNode));
if (new_node == NULL) {
printf("错误: 内存分配失败\n");
return;
}
new_node->key = key;
new_node->user = user;
new_node->next = user_hash_table[index];
user_hash_table[index] = new_node;
}
// 哈希表查找
User* hash_table_lookup(int key) {
unsigned int index = hash_function(key);
HashNode *current = user_hash_table[index];
while (current != NULL) {
if (current->key == key) {
return current->user;
}
current = current->next;
}
return NULL; // 未找到
}
// 打印系统状态
void print_system_status() {
printf("===== 系统状态报告 =====\n");
printf("商品总数: %d\n", product_count);
printf("用户总数: %d\n", user_count);
printf("订单总数: %d\n", order_count);
int pending_count = 0, paid_count = 0, shipped_count = 0, completed_count = 0;
for (int i = 0; i < order_count; i++) {
switch (orders[i].status) {
case PENDING_PAYMENT: pending_count++; break;
case PAID: paid_count++; break;
case SHIPPED: shipped_count++; break;
case COMPLETED: completed_count++; break;
}
}
printf("订单状态统计:\n");
printf(" 待付款: %d\n", pending_count);
printf(" 已付款: %d\n", paid_count);
printf(" 已发货: %d\n", shipped_count);
printf(" 已完成: %d\n", completed_count);
printf("补货任务数量: %d\n", restock_count);
printf("订单历史记录数量: %d\n", history_count);
printf("=======================\n");
}
// 清理旧订单
void cleanup_old_orders() {
time_t current_time = time(NULL);
time_t cutoff_time = current_time - (CLEANUP_DAYS * 24 * 60 * 60); // N天前
int cleaned_count = 0;
int moved_count = 0;
// 创建临时数组存储需要保留的订单
Order temp_orders[MAX_ORDERS];
int temp_count = 0;
for (int i = 0; i < order_count; i++) {
// 只清理已完成的订单
if (orders[i].status == COMPLETED && orders[i].order_time < cutoff_time) {
cleaned_count++;
} else {
// 保留的订单移动到临时数组
temp_orders[temp_count] = orders[i];
temp_count++;
}
}
// 将临时数组复制回原数组
if (temp_count != order_count) {
memcpy(orders, temp_orders, temp_count * sizeof(Order));
order_count = temp_count;
moved_count = cleaned_count;
}
printf("清理完成: 共清理 %d 个旧订单\n", moved_count);
}
// 初始化系统
void initialize_system() {
product_count = 0;
user_count = 0;
order_count = 0;
restock_count = 0;
history_count = 0;
initialize_hash_table();
// 从文件加载数据
load_products_from_file();
load_users_from_file();
load_orders_from_file();
printf("系统初始化完成\n");
// 如果文件加载失败或为空,添加一些测试数据
if (product_count == 0) {
add_product(101, "智能手机", "电子产品", 4999.0, 50);
add_product(102, "笔记本电脑", "电子产品", 7999.0, 30);
add_product(103, "T恤", "服装", 99.0, 100);
}
if (user_count == 0) {
add_user(1, "张三", "北京市朝阳区", "13800138000");
add_user(2, "李四", "上海市浦东新区", "13900139000");
}
}
// 初始化哈希表
void initialize_hash_table() {
for (int i = 0; i < 100; i++) {
user_hash_table[i] = NULL;
}
}
// 显示菜单
void show_menu() {
printf("\n===== 电商平台管理系统 =====\n");
printf("1. 添加商品\n");
printf("2. 查看所有商品\n");
printf("3. 搜索商品\n");
printf("4. 添加用户\n");
printf("5. 查看所有用户\n");
printf("6. 搜索用户\n");
printf("7. 创建订单\n");
printf("8. 查看所有订单\n");
printf("9. 搜索订单\n");
printf("10. 处理付款\n");
printf("11. 处理发货\n");
printf("12. 确认收货\n");
printf("13. 检查低库存商品\n");
printf("14. 处理补货任务\n");
printf("15. 查看系统状态\n");
printf("16. 清理旧订单\n");
printf("0. 退出系统\n");
printf("===========================\n");
}
// 等待用户输入,按任意键返回菜单
void wait_for_user() {
printf("\n按Enter键返回菜单...");
while (getchar() != '\n'); // 清除输入缓冲区
getchar(); // 等待用户按下Enter键
}
// 输入商品信息
void input_product() {
int id;
char name[MAX_NAME_LENGTH];
char category[MAX_CATEGORY_LENGTH];
float price;
int stock;
printf("请输入商品ID: ");
scanf("%d", &id);
getchar(); // 消耗换行符
printf("请输入商品名称: ");
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = 0; // 去除换行符
printf("请输入商品类别: ");
fgets(category, MAX_CATEGORY_LENGTH, stdin);
category[strcspn(category, "\n")] = 0; // 去除换行符
printf("请输入商品价格: ");
scanf("%f", &price);
getchar(); // 消耗换行符
printf("请输入商品库存: ");
scanf("%d", &stock);
getchar(); // 消耗换行符
if (add_product(id, name, category, price, stock) == 0) {
printf("商品添加成功\n");
}
}
// 列出所有商品
void list_products() {
if (product_count == 0) {
printf("没有商品记录\n");
return;
}
sort_products_by_name(); // 按名称排序
printf("商品列表:\n");
for (int i = 0; i < product_count; i++) {
printf("%d. %s (ID: %d, 价格: %.2f, 库存: %d)\n",
i + 1, products[i].name, products[i].id, products[i].price, products[i].stock);
}
}
// 输入用户信息
void input_user() {
int id;
char name[MAX_NAME_LENGTH];
char address[MAX_ADDRESS_LENGTH];
char contact[MAX_NAME_LENGTH];
printf("请输入用户ID: ");
scanf("%d", &id);
getchar(); // 消耗换行符
printf("请输入用户姓名: ");
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = 0; // 去除换行符
printf("请输入用户地址: ");
fgets(address, MAX_ADDRESS_LENGTH, stdin);
address[strcspn(address, "\n")] = 0; // 去除换行符
printf("请输入联系方式: ");
fgets(contact, MAX_NAME_LENGTH, stdin);
contact[strcspn(contact, "\n")] = 0; // 去除换行符
if (add_user(id, name, address, contact) == 0) {
printf("用户添加成功\n");
}
}
// 列出所有用户
void list_users() {
if (user_count == 0) {
printf("没有用户记录\n");
return;
}
printf("用户列表:\n");
for (int i = 0; i < user_count; i++) {
printf("%d. %s (ID: %d, 联系方式: %s)\n",
i + 1, users[i].name, users[i].id, users[i].contact);
}
}
// 输入订单信息
void input_order() {
int user_id, product_id, quantity;
printf("请输入用户ID: ");
scanf("%d", &user_id);
getchar(); // 消耗换行符
printf("请输入商品ID: ");
scanf("%d", &product_id);
getchar(); // 消耗换行符
printf("请输入商品数量: ");
scanf("%d", &quantity);
getchar(); // 消耗换行符
create_order(user_id, product_id, quantity);
}
// 列出所有订单
void list_orders() {
if (order_count == 0) {
printf("没有订单记录\n");
return;
}
sort_orders_by_time(); // 按时间排序
printf("订单列表:\n");
for (int i = 0; i < order_count; i++) {
printf("%d. 订单ID: %d, 用户ID: %d, 商品ID: %d, 数量: %d, 状态: ",
i + 1, orders[i].id, orders[i].user_id, orders[i].product_id, orders[i].quantity);
switch (orders[i].status) {
case PENDING_PAYMENT: printf("待付款\n"); break;
case PAID: printf("已付款\n"); break;
case SHIPPED: printf("已发货\n"); break;
case COMPLETED: printf("已完成\n"); break;
default: printf("未知状态\n");
}
}
}
// 搜索商品
void search_product() {
int choice;
printf("搜索方式:\n");
printf("1. 按ID搜索\n");
printf("2. 按名称搜索\n");
printf("请选择: ");
scanf("%d", &choice);
getchar(); // 消耗换行符
if (choice == 1) {
int id;
printf("请输入商品ID: ");
scanf("%d", &id);
getchar(); // 消耗换行符
int index = find_product_by_id(id);
if (index != -1) {
print_product(index);
} else {
printf("未找到商品ID为 %d 的记录\n", id);
}
} else if (choice == 2) {
char name[MAX_NAME_LENGTH];
printf("请输入商品名称: ");
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = 0; // 去除换行符
int index = find_product_by_name(name);
if (index != -1) {
print_product(index);
} else {
printf("未找到商品名称为 %s 的记录\n", name);
}
} else {
printf("无效的选择\n");
}
}
// 搜索用户
void search_user() {
int choice;
printf("搜索方式:\n");
printf("1. 按ID搜索\n");
printf("2. 按名称搜索\n");
printf("请选择: ");
scanf("%d", &choice);
getchar(); // 消耗换行符
if (choice == 1) {
int id;
printf("请输入用户ID: ");
scanf("%d", &id);
getchar(); // 消耗换行符
User *user = find_user_by_id(id);
if (user != NULL) {
// 查找用户在数组中的索引
int index = -1;
for (int i = 0; i < user_count; i++) {
if (users[i].id == id) {
index = i;
break;
}
}
if (index != -1) {
print_user(index);
}
} else {
printf("未找到用户ID为 %d 的记录\n", id);
}
} else if (choice == 2) {
char name[MAX_NAME_LENGTH];
printf("请输入用户姓名: ");
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = 0; // 去除换行符
int index = find_user_by_name(name);
if (index != -1) {
print_user(index);
} else {
printf("未找到用户姓名为 %s 的记录\n", name);
}
} else {
printf("无效的选择\n");
}
}
// 搜索订单
void search_order() {
int id;
printf("请输入订单ID: ");
scanf("%d", &id);
getchar(); // 消耗换行符
Order *order = find_order_by_id(id);
if (order != NULL) {
// 查找订单在数组中的索引
int index = -1;
for (int i = 0; i < order_count; i++) {
if (orders[i].id == id) {
index = i;
break;
}
}
if (index != -1) {
print_order(index);
}
} else {
printf("未找到订单ID为 %d 的记录\n", id);
}
}
// 主函数
int main() {
initialize_system();
int choice;
do {
show_menu();
printf("请输入您的选择: ");
if (scanf("%d", &choice) != 1) {
printf("输入错误,请输入一个数字\n");
while (getchar() != '\n'); // 清除错误输入
continue;
}
getchar(); // 消耗换行符
switch (choice) {
case 1: input_product(); wait_for_user(); break;
case 2: list_products(); wait_for_user(); break;
case 3: search_product(); wait_for_user(); break;
case 4: input_user(); wait_for_user(); break;
case 5: list_users(); wait_for_user(); break;
case 6: search_user(); wait_for_user(); break;
case 7: input_order(); wait_for_user(); break;
case 8: list_orders(); wait_for_user(); break;
case 9: search_order(); wait_for_user(); break;
case 10:
printf("请输入要处理付款的订单ID: ");
int order_id;
scanf("%d", &order_id);
getchar(); // 消耗换行符
process_payment(order_id);
wait_for_user();
break;
case 11:
printf("请输入要处理发货的订单ID: ");
scanf("%d", &order_id);
getchar(); // 消耗换行符
process_shipment(order_id);
wait_for_user();
break;
case 12:
printf("请输入要确认收货的订单ID: ");
scanf("%d", &order_id);
getchar(); // 消耗换行符
confirm_receipt(order_id);
wait_for_user();
break;
case 13: check_low_stock(); wait_for_user(); break;
case 14: process_restock_tasks(); wait_for_user(); break;
case 15: print_system_status(); wait_for_user(); break;
case 16: cleanup_old_orders(); wait_for_user(); break;
case 0: printf("退出系统...\n"); break;
default: printf("无效的选择,请重新输入\n");
}
} while (choice != 0);
return 0;
}