关于#C语言#的问题,如何解决?

商品:分类、价格、剩余数量
1.商品信息:
2.商品分类:零食、日用品、瓜果蔬菜等
实现功能:管理员登录录入商品信息,客户可以查询商品菜单;
菜单显示商品目前数量和价格;
客户挑选商品后,自动计算所有准备购买的商品总价;
如果客户确认购买,商品数量减少被购买的数量;
如果商品售空,响铃提示管理员补货。
问:如何制作代码?

该回答引用ChatGPT

代码如下:

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

#define MAX_ITEMS 100 // 商品最大数量

// 商品结构体
struct commodity {
    char category[20]; // 商品分类
    float price; // 商品价格
    int quantity; // 商品剩余数量
};

// 全局变量
struct commodity items[MAX_ITEMS];
int item_count = 0; // 商品总数

// 声明函数
void add_item();
void show_menu();
void make_purchase();

int main() {
    int choice;

    // 管理员登录
    printf("Welcome, Admin!\n");
    printf("Please log in.\n");

    char username[20], password[20];
    printf("Username: ");
    scanf("%s", username);
    printf("Password: ");
    scanf("%s", password);

    // 假设只有一个管理员账号,用户名为 "admin",密码为 "password"
    if (strcmp(username, "admin") != 0 || strcmp(password, "password") != 0) {
        printf("Login failed. Exiting...\n");
        return 1;
    }

    // 循环显示菜单,直到用户选择退出
    while (1) {
        printf("\nPlease select an option:\n");
        printf("1. Add item\n");
        printf("2. Show menu\n");
        printf("3. Make purchase\n");
        printf("4. Exit\n");
        printf("Your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                add_item();
                break;
            case 2:
                show_menu();
                break;
            case 3:
                make_purchase();
                break;
            case 4:
                printf("Exiting...\n");
                return 0;
            default:
                printf("Invalid choice. Please try again.\n");
                break;
        }
    }
}

// 添加商品
void add_item() {
    struct commodity item;

    // 获取商品信息
    printf("\nPlease enter the following information:\n");
    printf("Category: ");
    scanf("%s", item.category);
    printf("Price: ");
    scanf("%f", &item.price);
    printf("Quantity: ");
    scanf("%d", &item.quantity);

    // 添加到商品数组
    items[item_count] = item;
    item_count++;

    printf("Item added successfully!\n");
}

// 显示菜单
void show_menu() {
    printf("\n========== Menu ==========\n");
    printf("%-10s %-10s %-10s\n", "Category", "Price", "Quantity");
    for (int i = 0; i < item_count; i++) {
        printf("%-10s %-10.2f %-10d\n", items[i].category, items[i].price, items[i].quantity);
    }
}

// 进行购买
void make_purchase() {
    float total_price = 0;
    int index, quantity;

    // 获取购买信息
    printf("\nPlease enter the item index and quantity (e.g. 1 3):\n");
    scanf("%d %d", &index, &quantity);
    index--; // 索引从 0 开始,用户输入从 1 开始

    // 检查商品是否存在且数量足够
    if (index < 0 || index >= item_count) {
        printf("Invalid index. Please try again.\n");
        return;
   
if (items[index].quantity < quantity) {
    printf("Insufficient quantity. Please try again.\n");
    return;
}

// 计算总价
total_price = items[index].price * quantity;

// 确认购买
printf("\nTotal price: %.2f\n", total_price);
printf("Confirm purchase? (y/n) ");
char confirm;
scanf(" %c", &confirm);

if (confirm == 'y' || confirm == 'Y') {
    // 更新商品数量
    items[index].quantity -= quantity;

    printf("Purchase successful!\n");
    printf("Thank you for shopping with us.\n");
} else {
    printf("Purchase cancelled.\n");
}

// 检查商品是否售空,需要补货
if (items[index].quantity == 0) {
    printf("Warning: The item \"%s\" is sold out. Please restock as soon as possible.\n", items[index].category);
}



该程序使用了三个函数:

  • add_item():用于向商品数组中添加商品信息。
  • show_menu():用于显示所有商品的分类、价格和剩余数量。
  • make_purchase():用于让客户购买商品,并更新商品数量。

程序还包括一个 main() 函数,用于管理用户登录、显示菜单选项等功能。用户选择菜单选项后,会调用相应的函数来完成相应的功能。

注意:本程序仅作为参考,可能存在潜在的 bug 或者需要优化的地方,建议根据实际需求进行修改。