您好,登录后才能下订单哦!
商品管理系统是现代商业活动中不可或缺的一部分,它能够帮助企业高效地管理商品信息、库存、销售等关键业务。本文将详细介绍如何使用C语言开发一个简单的商品管理系统。通过本系统,用户可以方便地进行商品信息的录入、查询、修改、删除等操作,同时还能管理库存和销售记录。
在开发商品管理系统之前,首先需要进行详细的需求分析,明确系统需要实现的功能和用户的需求。以下是本系统的主要功能需求:
本系统采用模块化设计,主要分为以下几个模块:
为了高效地管理商品信息,我们需要设计合适的数据结构。以下是本系统中使用的主要数据结构:
typedef struct {
int id; // 商品ID
char name[50]; // 商品名称
float price; // 商品价格
int stock; // 商品库存
} Product;
typedef struct {
int id; // 销售记录ID
int product_id; // 商品ID
int quantity; // 销售数量
float total_price; // 总价
char date[20]; // 销售日期
} SaleRecord;
typedef struct {
char username[20]; // 用户名
char password[20]; // 密码
int role; // 用户角色(0: 管理员, 1: 普通用户)
} User;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PRODUCTS 100
typedef struct {
int id;
char name[50];
float price;
int stock;
} Product;
Product products[MAX_PRODUCTS];
int product_count = 0;
void add_product() {
if (product_count >= MAX_PRODUCTS) {
printf("商品数量已达上限,无法添加新商品。\n");
return;
}
Product new_product;
new_product.id = product_count + 1;
printf("请输入商品名称: ");
scanf("%s", new_product.name);
printf("请输入商品价格: ");
scanf("%f", &new_product.price);
printf("请输入商品库存: ");
scanf("%d", &new_product.stock);
products[product_count++] = new_product;
printf("商品添加成功!\n");
}
void delete_product() {
int id;
printf("请输入要删除的商品ID: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < product_count; i++) {
if (products[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该商品。\n");
return;
}
for (int i = index; i < product_count - 1; i++) {
products[i] = products[i + 1];
}
product_count--;
printf("商品删除成功!\n");
}
void modify_product() {
int id;
printf("请输入要修改的商品ID: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < product_count; i++) {
if (products[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该商品。\n");
return;
}
printf("请输入新的商品名称: ");
scanf("%s", products[index].name);
printf("请输入新的商品价格: ");
scanf("%f", &products[index].price);
printf("请输入新的商品库存: ");
scanf("%d", &products[index].stock);
printf("商品修改成功!\n");
}
void query_product() {
char name[50];
printf("请输入要查询的商品名称: ");
scanf("%s", name);
for (int i = 0; i < product_count; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("商品ID: %d\n", products[i].id);
printf("商品名称: %s\n", products[i].name);
printf("商品价格: %.2f\n", products[i].price);
printf("商品库存: %d\n", products[i].stock);
return;
}
}
printf("未找到该商品。\n");
}
void update_stock() {
int id, quantity;
printf("请输入要更新库存的商品ID: ");
scanf("%d", &id);
printf("请输入库存变化量: ");
scanf("%d", &quantity);
int index = -1;
for (int i = 0; i < product_count; i++) {
if (products[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该商品。\n");
return;
}
products[index].stock += quantity;
printf("库存更新成功!\n");
}
void stock_alert() {
int threshold;
printf("请输入库存预警阈值: ");
scanf("%d", &threshold);
for (int i = 0; i < product_count; i++) {
if (products[i].stock < threshold) {
printf("商品ID: %d, 商品名称: %s, 当前库存: %d\n", products[i].id, products[i].name, products[i].stock);
}
}
}
#define MAX_SALES 100
typedef struct {
int id;
int product_id;
int quantity;
float total_price;
char date[20];
} SaleRecord;
SaleRecord sales[MAX_SALES];
int sale_count = 0;
void add_sale_record() {
if (sale_count >= MAX_SALES) {
printf("销售记录数量已达上限,无法添加新记录。\n");
return;
}
SaleRecord new_sale;
new_sale.id = sale_count + 1;
printf("请输入商品ID: ");
scanf("%d", &new_sale.product_id);
printf("请输入销售数量: ");
scanf("%d", &new_sale.quantity);
printf("请输入销售日期: ");
scanf("%s", new_sale.date);
int index = -1;
for (int i = 0; i < product_count; i++) {
if (products[i].id == new_sale.product_id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该商品。\n");
return;
}
if (products[index].stock < new_sale.quantity) {
printf("库存不足,无法完成销售。\n");
return;
}
new_sale.total_price = products[index].price * new_sale.quantity;
products[index].stock -= new_sale.quantity;
sales[sale_count++] = new_sale;
printf("销售记录添加成功!\n");
}
void generate_sales_report() {
printf("销售报表:\n");
for (int i = 0; i < sale_count; i++) {
printf("销售记录ID: %d, 商品ID: %d, 销售数量: %d, 总价: %.2f, 销售日期: %s\n",
sales[i].id, sales[i].product_id, sales[i].quantity, sales[i].total_price, sales[i].date);
}
}
#define MAX_USERS 10
typedef struct {
char username[20];
char password[20];
int role; // 0: 管理员, 1: 普通用户
} User;
User users[MAX_USERS];
int user_count = 0;
int login() {
char username[20], password[20];
printf("请输入用户名: ");
scanf("%s", username);
printf("请输入密码: ");
scanf("%s", password);
for (int i = 0; i < user_count; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
return users[i].role;
}
}
printf("用户名或密码错误。\n");
return -1;
}
void add_user() {
if (user_count >= MAX_USERS) {
printf("用户数量已达上限,无法添加新用户。\n");
return;
}
User new_user;
printf("请输入用户名: ");
scanf("%s", new_user.username);
printf("请输入密码: ");
scanf("%s", new_user.password);
printf("请输入用户角色 (0: 管理员, 1: 普通用户): ");
scanf("%d", &new_user.role);
users[user_count++] = new_user;
printf("用户添加成功!\n");
}
void save_data() {
FILE *file = fopen("products.dat", "wb");
if (file == NULL) {
printf("无法打开文件。\n");
return;
}
fwrite(&product_count, sizeof(int), 1, file);
fwrite(products, sizeof(Product), product_count, file);
fwrite(&sale_count, sizeof(int), 1, file);
fwrite(sales, sizeof(SaleRecord), sale_count, file);
fwrite(&user_count, sizeof(int), 1, file);
fwrite(users, sizeof(User), user_count, file);
fclose(file);
printf("数据保存成功!\n");
}
void load_data() {
FILE *file = fopen("products.dat", "rb");
if (file == NULL) {
printf("无法打开文件。\n");
return;
}
fread(&product_count, sizeof(int), 1, file);
fread(products, sizeof(Product), product_count, file);
fread(&sale_count, sizeof(int), 1, file);
fread(sales, sizeof(SaleRecord), sale_count, file);
fread(&user_count, sizeof(int), 1, file);
fread(users, sizeof(User), user_count, file);
fclose(file);
printf("数据加载成功!\n");
}
在完成代码实现后,我们需要对系统进行全面的测试,以确保各个功能模块能够正常工作。以下是测试的主要步骤:
通过本文的介绍,我们详细讲解了如何使用C语言开发一个简单的商品管理系统。该系统涵盖了商品信息管理、库存管理、销售管理、用户管理和数据持久化等核心功能。尽管该系统功能较为基础,但它为后续的功能扩展和优化提供了良好的基础。
未来,我们可以考虑以下改进方向:
”`c
#include
#define MAX_PRODUCTS 100 #define MAX_SALES 100 #define MAX_USERS 10
typedef struct { int id; char name[50]; float price; int stock; } Product;
typedef struct { int id; int product_id; int quantity; float total_price; char date[20]; } SaleRecord;
typedef struct { char username[20]; char password[20]; int role; // 0: 管理员, 1: 普通用户 } User;
Product products[MAX_PRODUCTS]; int product_count = 0;
SaleRecord sales[MAX_SALES]; int sale_count = 0;
User users[MAX_USERS]; int user_count = 0;
void add_product() { if (product_count >= MAX_PRODUCTS) { printf(“商品数量已达上限,无法添加新商品。\n”); return; }
Product new_product;
new_product.id = product_count + 1;
printf("请输入商品名称: ");
scanf("%s", new_product.name);
printf("请输入商品价格: ");
scanf("%f", &new_product.price);
printf("请输入商品库存: ");
scanf("%d", &new_product.stock);
products[product_count++] = new_product;
printf("商品添加成功!\n");
}
void delete_product() { int id; printf(“请输入要删除的商品ID: “); scanf(”%d”, &id);
int index = -1;
for (int i = 0; i < product_count; i++) {
if (products[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该商品。\n");
return;
}
for (int i = index; i < product_count - 1; i++) {
products[i] = products[i + 1];
}
product_count--;
printf("商品删除成功!\n");
}
void modify_product() { int id; printf(“请输入要修改的商品ID: “); scanf(”%d”, &id);
int index = -1;
for (int i = 0; i < product_count; i++) {
if (products[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该商品。\n");
return;
}
printf("请输入新的商品名称: ");
scanf("%s", products[index].name);
printf("请输入新的商品价格: ");
scanf("%f", &products[index].price);
printf("请输入新的商品库存: ");
scanf("%d", &products[index].stock);
printf("商品修改成功!\n");
}
void query_product() { char name[50]; printf(“请输入要查询的商品名称: “); scanf(”%s”, name);
for (int i = 0; i < product_count; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("商品ID: %d\n", products[i].id);
printf("商品名称: %s\n", products[i].name);
printf("商品价格: %.2f\n", products[i].price);
printf("商品库存: %d\n", products[i].stock);
return;
}
}
printf("未找到该商品。\n");
}
void update_stock() { int id, quantity; printf(“请输入要更新库存的商品ID: “); scanf(”%d”, &id); printf(“请输入库存变化量: “); scanf(”%d”, &quantity);
int index = -1;
for (int i = 0; i < product_count; i++) {
if (products[i].id == id) {
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。