使用c语言写一个商品库存管理系统代码实现功能
时间: 2023-09-03 19:06:08 浏览: 106
C语言实现库存管理系统
好的,下面是一个简单的商品库存管理系统的代码实现,您可以参考:
```C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
// 商品结构体
typedef struct {
int id; // 商品编号
char name[MAX_NAME_LEN]; // 商品名称
float price; // 商品价格
int quantity; // 商品数量
} Product;
// 链表节点
typedef struct ProductNode {
Product data; // 商品信息
struct ProductNode *next; // 指向下一个节点的指针
} ProductNode;
// 创建新的节点
ProductNode *createNode(Product p)
{
ProductNode *node = (ProductNode *) malloc(sizeof(ProductNode));
node->data = p;
node->next = NULL;
return node;
}
// 添加商品
ProductNode *addProduct(ProductNode *head, Product p)
{
ProductNode *node = createNode(p);
if (head == NULL) {
head = node;
} else {
ProductNode *curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = node;
}
return head;
}
// 删除商品
ProductNode *deleteProduct(ProductNode *head, int id)
{
if (head == NULL) {
printf("商品库存为空,无法删除商品!\n");
} else {
ProductNode *curr = head;
ProductNode *prev = NULL;
while (curr != NULL && curr->data.id != id) {
prev = curr;
curr = curr->next;
}
if (curr == NULL) {
printf("没有找到编号为%d的商品!\n", id);
} else {
if (prev == NULL) {
head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("商品删除成功!\n");
}
}
return head;
}
// 修改商品
void modifyProduct(ProductNode *head, int id)
{
if (head == NULL) {
printf("商品库存为空,无法修改商品!\n");
} else {
ProductNode *curr = head;
while (curr != NULL && curr->data.id != id) {
curr = curr->next;
}
if (curr == NULL) {
printf("没有找到编号为%d的商品!\n", id);
} else {
printf("请输入修改后的商品名称:");
scanf("%s", curr->data.name);
printf("请输入修改后的商品价格:");
scanf("%f", &curr->data.price);
printf("请输入修改后的商品数量:");
scanf("%d", &curr->data.quantity);
printf("商品修改成功!\n");
}
}
}
// 查询商品
void queryProduct(ProductNode *head, int id)
{
if (head == NULL) {
printf("商品库存为空,无法查询商品!\n");
} else {
ProductNode *curr = head;
while (curr != NULL && curr->data.id != id) {
curr = curr->next;
}
if (curr == NULL) {
printf("没有找到编号为%d的商品!\n", id);
} else {
printf("商品编号:%d\n", curr->data.id);
printf("商品名称:%s\n", curr->data.name);
printf("商品价格:%.2f\n", curr->data.price);
printf("商品数量:%d\n", curr->data.quantity);
}
}
}
// 显示商品列表
void showProductList(ProductNode *head)
{
if (head == NULL) {
printf("商品库存为空!\n");
} else {
printf("商品列表如下:\n");
printf("编号\t名称\t价格\t数量\n");
ProductNode *curr = head;
while (curr != NULL) {
printf("%d\t%s\t%.2f\t%d\n", curr->data.id, curr->data.name, curr->data.price, curr->data.quantity);
curr = curr->next;
}
}
}
// 保存商品信息到文件中
void saveToFile(ProductNode *head)
{
if (head == NULL) {
printf("商品库存为空,无法保存商品信息!\n");
} else {
FILE *fp = fopen("product.dat", "wb");
if (fp == NULL) {
printf("无法打开文件!\n");
} else {
ProductNode *curr = head;
while (curr != NULL) {
fwrite(&(curr->data), sizeof(Product), 1, fp);
curr = curr->next;
}
fclose(fp);
printf("商品信息保存成功!\n");
}
}
}
// 从文件中读取商品信息
ProductNode *readFromFile()
{
ProductNode *head = NULL;
FILE *fp = fopen("product.dat", "rb");
if (fp == NULL) {
printf("无法打开文件!\n");
} else {
Product p;
while (fread(&p, sizeof(Product), 1, fp) == 1) {
head = addProduct(head, p);
}
fclose(fp);
printf("商品信息读取成功!\n");
}
return head;
}
// 释放链表内存
void freeList(ProductNode *head)
{
ProductNode *curr = head;
while (curr != NULL) {
ProductNode *temp = curr;
curr = curr->next;
free(temp);
}
}
int main()
{
ProductNode *head = NULL;
int choice, id;
Product p;
head = readFromFile();
do {
printf("请选择操作:\n");
printf("1. 添加商品\n");
printf("2. 删除商品\n");
printf("3. 修改商品\n");
printf("4. 查询商品\n");
printf("5. 显示商品列表\n");
printf("6. 保存商品信息到文件\n");
printf("0. 退出程序\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入商品编号:");
scanf("%d", &p.id);
printf("请输入商品名称:");
scanf("%s", p.name);
printf("请输入商品价格:");
scanf("%f", &p.price);
printf("请输入商品数量:");
scanf("%d", &p.quantity);
head = addProduct(head, p);
printf("商品添加成功!\n");
break;
case 2:
printf("请输入要删除的商品编号:");
scanf("%d", &id);
head = deleteProduct(head, id);
break;
case 3:
printf("请输入要修改的商品编号:");
scanf("%d", &id);
modifyProduct(head, id);
break;
case 4:
printf("请输入要查询的商品编号:");
scanf("%d", &id);
queryProduct(head, id);
break;
case 5:
showProductList(head);
break;
case 6:
saveToFile(head);
break;
case 0:
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
} while (choice != 0);
freeList(head);
return 0;
}
```
此代码实现了商品的添加、删除、修改、查询、显示列表和保存到文件的功能。可以运行测试一下。
阅读全文