c语言怎么使用数组保存实现超市商品管理系统

超市商品管理系统是一个比较常见的应用程序,可以帮助超市管理员更方便地管理超市的商品信息。在本文中,我们将介绍如何使用C语言和数组来实现一个基本的超市商品管理系统。

一、准备工作

在开始之前,我们需要准备一些工具和环境。首先,我们需要安装一个C语言编译器,这里我们选择使用gcc编译器。其次,我们需要一个能够编辑代码的文本编辑器,比如Notepad++或其他的代码编辑器。

二、实现步骤

1 定义商品结构体

在本例中,我们定义了一个包含多个商品信息的结构体。其中,商品ID(id)、名称(name)、价格(price)和库存量(stock)是必填项。其他字段(如描述description和生产日期production_date等)可以按需添加或省略。

// 商品结构体
struct Product {
    int id;
    char name[50];
    double price;
    int stock;
    char description[200];
    char production_date[20];
};
  1. 创建商品数组

接着,我们需要创建一个商品数组来保存所有的商品信息。在这个例子中,我们将创建一个包含100个商品的数组。您也可以根据实际情况调整数组大小。

// 创建商品数组
struct Product products[100];
  1. 添加商品信息

接下来,我们可以编写一个函数来添加新的商品信息。在这个例子中,我们将使用一个数组索引变量(index)来跟踪下一个可用的商品数组元素位置。当添加新的商品时,我们将会为其分配一个新的ID,并使用scanf函数来读取其他字段的值。

// 添加商品信息
void addProduct() {
    int id = 0;
    char name[50];
    double price = 0.0;
    int stock = 0;
    char description[200];
    char production_date[20];

    // 为新商品分配ID
    for (int i = 0; i < 100; i++) {
        if (products[i].id == 0) {
            id = i + 1;
            break;
        }
    }

    // 读取商品信息
    printf("请输入商品名称:");
    scanf("%s", name);
    printf("请输入商品价格:");
    scanf("%lf", &price);
    printf("请输入商品库存量:");
    scanf("%d", &stock);
    printf("请输入商品描述:");
    scanf("%s", description);
    printf("请输入商品生产日期:");
    scanf("%s", production_date);

    // 将新商品信息保存到数组中
    products[index].id = id;
    strcpy(products[index].name, name);
    products[index].price = price;
    products[index].stock = stock;
    strcpy(products[index].description, description);
    strcpy(products[index].production_date, production_date);

    // 增加索引变量
    index++;
    printf("新商品已添加!\n");
}
  1. 查找商品信息

我们还可以编写一个函数来查找特定商品的信息。在这个例子中,我们将使用商品ID来查找商品信息。如果找到了相应的商品,则将其信息打印出来。

// 查找商品信息
void findProduct() {
    int id = 0;
    printf("请输入要查找的商品ID:");
    scanf("%d", &id);

    for (int i = 0; i < index; i++) {
        if (products[i].id == id) {
            printf("商品信息如下:\n");
            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);
            printf("描述:%s\n", products[i].description);
            printf("生产日期:%s\n", products[i].production_date);
            return;
        }
    }

    printf("未找到该商品!\n");
}
  1. 修改商品信息

我们还可以编写一个函数来修改特定商品的信息。在这个例子中,我们将使用商品ID来查找要修改的商品。找到商品后,我们将使用scanf函数来读取新的信息。如果没有找到相应的商品,则输出相应的提示信息。

// 修改商品信息
void editProduct() {
    int id = 0;
    printf("请输入要修改的商品ID:");
    scanf("%d", &id);

    for (int i = 0; i < index; i++) {
        if (products[i].id == id) {
            char name[50];
            double price = 0.0;
            int stock = 0;
            char description[200];
            char production_date[20];

            // 读取新的商品信息
            printf("请输入新的商品名称:");
            scanf("%s", name);
            printf("请输入新的商品价格:");
            scanf("%lf", &price);
            printf("请输入新的商品库存量:");
            scanf("%d", &stock);
            printf("请输入新的商品描述:");
            scanf("%s", description);
            printf("请输入新的商品生产日期:");
            scanf("%s", production_date);

            // 更新商品信息
            strcpy(products[i].name, name);
            products[i].price = price;
            products[i].stock = stock;
            strcpy(products[i].description, description);
            strcpy(products[i].production_date, production_date);

            printf("商品信息已更新!\n");
            return;
        }
    }

    printf("未找到该商品!\n");
}
  1. 删除商品信息

最后,我们还可以编写一个函数来删除特定商品的信息。在这个例子中,我们将使用商品ID来查找要删除的商品。找到商品后,我们将把其ID设置为0,并输出相应的提示信息。如果没有找到相应的商品,则输出相应的提示信息。

// 删除商品信息
void deleteProduct() {
    int id = 0;
    printf("请输入要删除的商品ID:");
    scanf("%d", &id);

    for (int i = 0; i < index; i++) {
        if (products[i].id == id) {
            products[i].id = 0;
            printf("商品信息已删除!\n");
            return;
        }
    }

    printf("未找到该商品!\n");
}

三、主函数

现在,我们可以把上述定义和函数组合在一起,构成一个完整的超市商品管理系统。下面是一个简单的实现:

#include <stdio.h>
#include <string.h>

// 商品结构体
struct Product {
    int id;
    char name[50];
    double price;
    int stock;
    char description[200];
    char production_date[20];
};

// 创建商品数组
struct Product products[100];

// 数组索引变量
int index = 0;

// 添加商品信息
void addProduct() {
    int id = 0;
    char name[50];
    double price = 0.0;
    int stock = 0;
    char description[200];
    char production_date[20];

    // 为新商品分配ID
    for (int i = 0; i < 100; i++) {
        if (products[i].id == 0) {
            id = i + 1;
            break;
        }
    }

    // 读取商品信息
    printf("请输入商品名称:");
    scanf("%s", name);
    printf("请输入商品价格:");
    scanf("%lf", &price);
    printf("请输入商品库存量:");
    scanf("%d", &stock);
    printf("请输入商品描述:");
    scanf("%s", description);
    printf("请输入商品生产日期:");
    scanf("%s", production_date);

    // 将新商品信息保存到数组中
    products[index].id = id;
    strcpy(products[index].name, name);
    products[index].price = price;
    products[index].stock = stock;
    strcpy(products[index].description, description);
    strcpy(products[index].production_date, production_date);

    // 增加索引变量
    index++;
    printf("新商品已添加!\n");
}

// 查找商品信息
void findProduct() {
    int id = 0;
    printf("请输入要查找的商品ID:");
    scanf("%d", &id);

    for (int i = 0; i < index; i++) {
        if (products[i].id == id) {
            printf("商品信息如下:\n");
            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);
            printf("描述:%s\n", products[i].description);
            printf("生产日期:%s\n", products[i].production_date);
            return;
        }
    }

    printf("未找到该商品!\n");
}

// 修改商品信息
void editProduct() {
    int id = 0;
    printf("请输入要修改的商品ID:");
    scanf("%d", &id);

    for (int i = 0; i < index; i++) {
        if (products[i].id == id) {
            char name[50];
            double price = 0.0;
            int stock = 0;
            char description[200];
            char production_date[20];

            // 读取新的商品信息
            printf("请输入新的商品名称:");
            scanf("%s", name);
            printf("请输入新的商品价格:");
            scanf("%lf", &price);
            printf("请输入新的商品库存量:");
            scanf("%d", &stock);
            printf("请输入新的商品描述:");
            scanf("%s", description);
            printf("请输入新的商品生产日期:");
            scanf("%s", production_date);

            // 更新商品信息
            strcpy(products[i].name, name);
            products[i].price = price;
            products[i].stock = stock;
            strcpy(products[i].description, description);
            strcpy(products[i].production_date, production_date);

            printf("商品信息已更新!\n");
            return;
        }
    }

    printf("未找到该商品!\n");
}

// 删除商品信息
void deleteProduct() {
    int id = 0;
    printf("请输入要删除的商品ID:");
    scanf("%d", &id);

    for (int i = 0; i < index; i++) {
        if (products[i].id == id) {
            products[i].id = 0;
            printf("商品信息已删除!\n");
            return;
        }
    }

    printf("未找到该商品!\n");
}

// 显示所有商品信息
void showAllProducts() {
    printf("所有商品信息如下:\n");

    for (int i = 0; i < index; i++) {
        if (products[i].id != 0) {
            printf("ID:%d\n", products[i].id);
            printf("名称:%s\n", products[i].name);
            printf("价格:%.2f\n", products[i

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值