#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_ITEMS 100#define MAX_USERS 10#define ADMIN_USER "admin"#define ADMIN_PASS "admin123"typedef struct { char name[50]; char code[20]; int quantity; float price; char lastAction[10]; char lastUser[50]; char lastTime[30];} Item;typedef struct { char username[20]; char password[20];} User;Item items[MAX_ITEMS];User users[MAX_USERS] = {{"user", "1234"}}; // 默认普通用户 int itemCount = 0; int userCount = 1; char currentUser[20]; // 保存数据到文件 void saveData() { FILE *file = fopen("warehouse.dat", "wb"); if (file) { fwrite(&itemCount, sizeof(int), 1, file); fwrite(items, sizeof(Item), itemCount, file); fwrite(&userCount, sizeof(int), 1, file); fwrite(users, sizeof(User), userCount, file); fclose(file); } } // 加载数据 void loadData() { FILE *file = fopen("warehouse.dat", "rb"); if (file) { fread(&itemCount, sizeof(int), 1, file); fread(items, sizeof(Item), itemCount, file); fread(&userCount, sizeof(int), 1, file); fread(users, sizeof(User), userCount, file); fclose(file); } }// 添加物品void addItem() { if (itemCount >= MAX_ITEMS) { printf("仓库已满,无法添加更多物品。\n"); return; } printf("输入物品名称: "); scanf("%s", items[itemCount].name); printf("输入物品代码: "); scanf("%s", items[itemCount].code); printf("输入物品价格: "); scanf("%f", &items[itemCount].price); items[itemCount].quantity = 0; strcpy(items[itemCount].lastAction, "新增"); strcpy(items[itemCount].lastUser, currentUser); printf("物品 %s 添加成功。\n", items[itemCount].name); itemCount++; saveData();}// 物品入库void inputItem() { char code[20]; int quantity; printf("输入物品代码: "); scanf("%s", code); for (int i = 0; i < itemCount; i++) { if (strcmp(items[i].code, code) == 0) { printf("输入入库数量: "); scanf("%d", &quantity); items[i].quantity += quantity; strcpy(items[i].lastAction, "入库"); strcpy(items[i].lastUser, currentUser); printf("物品 %s 入库成功,当前数量: %d\n", items[i].name, items[i].quantity); saveData(); return; } } printf("未找到该物品。\n");}// 物品出库void outputItem() { char code[20]; int quantity; printf("输入物品代码: "); scanf("%s", code); for (int i = 0; i < itemCount; i++) { if (strcmp(items[i].code, code) == 0) { printf("输入出库数量: "); scanf("%d", &quantity); if (items[i].quantity < quantity) { printf("库存不足!\n"); return; } items[i].quantity -= quantity; strcpy(items[i].lastAction, "出库"); strcpy(items[i].lastUser, currentUser); printf("物品 %s 出库成功,当前数量: %d\n", items[i].name, items[i].quantity); saveData(); return; } } printf("未找到该物品。\n");}// 查询物品void searchItem() { char code[20]; printf("输入物品代码: "); scanf("%s", code); for (int i = 0; i < itemCount; i++) { if (strcmp(items[i].code, code) == 0) { printf("物品名称: %s\n", items[i].name); printf("库存数量: %d\n", items[i].quantity); printf("价格: %.2f\n", items[i].price); printf("最近操作: %s\n", items[i].lastAction); printf("操作人: %s\n", items[i].lastUser); return; } } printf("未找到该物品。\n");}// 添加用户(仅管理员可用)void addUser() { if (strcmp(currentUser, ADMIN_USER) != 0) { printf("权限不足,仅管理员可操作。\n"); return; } if (userCount >= MAX_USERS) { printf("用户数已满,无法添加。\n"); return; } printf("输入新用户名: "); scanf("%s", users[userCount].username); printf("输入密码: "); scanf("%s", users[userCount].password); userCount++; printf("用户添加成功。\n"); saveData();}// 添加用户(仅管理员可用)void addUser() { if (strcmp(currentUser, ADMIN_USER) != 0) { printf("权限不足,仅管理员可操作。\n"); return; } if (userCount >= MAX_USERS) { printf("用户数已满,无法添加。\n"); return; } printf("输入新用户名: "); scanf("%s", users[userCount].username); printf("输入密码: "); scanf("%s", users[userCount].password); userCount++; printf("用户添加成功。\n"); saveData();}// 登录int login() { char username[20], password[20]; printf("用户名: "); scanf("%s", username); printf("密码: "); scanf("%s", password); for (int i = 0; i < userCount; i++) { if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) { strcpy(currentUser, username); printf("登录成功,欢迎 %s!\n", currentUser); return 1; } } printf("登录失败,用户名或密码错误。\n"); return 0;}// 主菜单void menu() { int choice; do { printf("\n==== 仓库管理系统 ====\n"); printf("1. 添加物品\n"); printf("2. 物品入库\n"); printf("3. 物品出库\n"); printf("4. 查询物品\n"); printf("5. 添加用户(管理员)\n"); printf("6. 退出\n"); printf("选择操作: "); scanf("%d", &choice); switch (choice) { case 1: addItem(); break; case 2: inputItem(); break; case 3: outputItem(); break; case 4: searchItem(); break; case 5: addUser(); break; case 6: printf("退出系统。\n"); return; default: printf("无效选项。\n"); } } while (1);}// 主函数int main() { loadData(); while (!login()); menu(); return 0;}