c语言 自动售卖机模拟系统

[问题描述]本项目是对自动售货机的简单模拟,商品信息可以包含商品编号、商品名、价格等信息,用菜单选择方式完成下列功能:
(1)商品信息的增删改查;
(2)订单信息可以包括订单发生的时间、销售的商品、交易金额、交易状态等;(3)日销售情况汇总;
[基本要求]本项目的实质是完成对自动售货机的简单模拟,可以首先定义项目的数据结构,然后将每个功能写成一个函数来完成对数据的操作,最后完成主函数以验证各个函数功能并得出运行结果。
具体要完成的任务是:
编制完成上述问题的C语言程序、进行程序调试并能得出正确的运行结果。

人工智能的思路
这是一个非常实用的项目,需要用到C语言编程,涉及到数据结构和函数的使用。下面是一些基本思路:

定义商品信息的数据结构,包括商品编号、商品名、价格等信息。
编写函数实现商品信息的增删改查,可以通过菜单选择方式来进行操作。
定义订单信息的数据结构,包括订单发生的时间、销售的商品、交易金额、交易状态等。
编写函数实现订单信息的增加和查询,可以通过菜单选择方式来进行操作。
编写函数实现日销售情况汇总,可以根据订单信息来计算每日的销售情况。
编写主函数来验证各个函数的功能,并得出正确的运行结果。

需要注意的是,程序调试是非常重要的一步,可以通过输出调试信息来定位问题,并进行修复。另外,代码的可读性和可维护性也是非常重要的,可以通过注释和模块化的设计来实现。

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7688110
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:C语言数据结构 笔记1 线性表之一顺序表的静态实现 & 动态分配实现| 顺序表基本操作 插入、删除、查找元素、相关时间复杂度分析
  • 除此之外, 这篇博客: 指针和数组试题解析(重置版)中的 小编,在这里想说一下,c语言的最后一节 C预处理,可能还需要一些时间,因为小编,昨天才下载了虚拟机 和 linux 系统,还没开始安装。所以无法着手写,因为 C预处理,vs2013很难表达,也就意味可能会讲不清楚。所以这篇文章可能需要点时间,再加上小编初期的文章,是没有排版的(而且可能有些错误,请大家以重置版为准),所以这几天我就把这些重新写。有兴趣的朋友可以看看。(ps:如果哪一天没有更新,意味着小编正在努力学习,为了能给大家呈现一片详细好懂的文章。) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:


  • 以下回答由chatgpt基于相关博客总结生成:

    我可以编写这样一个C语言的自动售卖机模拟系统的程序。具体的解决方案如下:

    首先,我们需要定义商品的数据结构,包括商品编号、名字和价格:

    struct goods {
        int id; // 商品编号
        char name[20]; // 商品名字
        float price; // 商品价格
    };
    

    然后,我们需要定义订单的数据结构,包括订单的时间、销售的商品、交易金额和交易状态:

    struct order {
        char time[20]; // 订单时间
        struct goods goods; // 销售的商品
        float amount; // 交易金额
        char status[10]; // 交易状态
    };
    

    接下来,我们需要为每个功能编写一个函数来操作数据。我们可以把这些函数都放在一个头文件中:

    #ifndef _VENDING_MACHINE_H_
    #define _VENDING_MACHINE_H_
    
    // 添加商品
    void add_goods(struct goods *goods_list, int *goods_count);
    
    // 删除商品
    void delete_goods(struct goods *goods_list, int *goods_count);
    
    // 修改商品
    void modify_goods(struct goods *goods_list, int *goods_count);
    
    // 查找商品
    void search_goods(struct goods *goods_list, int *goods_count);
    
    // 下订单
    void place_order(struct goods *goods_list, int *goods_count, struct order *order_list, int *order_count);
    
    // 订单退款
    void refund_order(struct order *order_list, int *order_count);
    
    // 查看每日销售汇总
    void daily_sales_summary(struct order *order_list, int *order_count);
    
    #endif /* _VENDING_MACHINE_H_ */
    

    下面,我们来详细介绍每个函数的实现。

    1. 添加商品

    添加商品功能可以通过以下方式实现:

    void add_goods(struct goods *goods_list, int *goods_count) {
        printf("请输入商品编号:");
        scanf("%d", &(goods_list[*goods_count].id));
        printf("请输入商品名字:");
        scanf("%s", goods_list[*goods_count].name);
        printf("请输入商品价格:");
        scanf("%f", &(goods_list[*goods_count].price));
        (*goods_count)++;
        printf("添加成功!\n");
    }
    
    1. 删除商品

    删除商品功能可以通过以下方式实现:

    void delete_goods(struct goods *goods_list, int *goods_count) {
        int id, i, j;
        printf("请输入要删除的商品编号:");
        scanf("%d", &id);
        for (i = 0; i < *goods_count; i++) {
            if (goods_list[i].id == id) {
                (*goods_count)--;
                for (j = i; j < *goods_count; j++) {
                    goods_list[j] = goods_list[j + 1];
                }
                printf("删除成功!\n");
                return;
            }
        }
        printf("找不到商品,请重新操作!\n");
    }
    
    1. 修改商品

    修改商品功能可以通过以下方式实现:

    void modify_goods(struct goods *goods_list, int *goods_count) {
        int id, i;
        printf("请输入要修改的商品编号:");
        scanf("%d", &id);
        for (i = 0; i < *goods_count; i++) {
            if (goods_list[i].id == id) {
                printf("请输入新的商品名字:");
                scanf("%s", goods_list[i].name);
                printf("请输入新的商品价格:");
                scanf("%f", &(goods_list[i].price));
                printf("修改成功!\n");
                return;
            }
        }
        printf("找不到商品,请重新操作!\n");
    }
    
    1. 查找商品

    查找商品功能可以通过以下方式实现:

    void search_goods(struct goods *goods_list, int *goods_count) {
        int id, i;
        printf("请输入要查询的商品编号:");
        scanf("%d", &id);
        for (i = 0; i < *goods_count; i++) {
            if (goods_list[i].id == id) {
                printf("商品编号:%d,商品名字:%s,商品价格:%.2f\n", goods_list[i].id, goods_list[i].name, goods_list[i].price);
                return;
            }
        }
        printf("找不到商品,请重新操作!\n");
    }
    
    1. 下订单

    下订单功能可以通过以下方式实现:

    void place_order(struct goods *goods_list, int *goods_count, struct order *order_list, int *order_count) {
        int id, i;
        printf("请输入要购买的商品编号:");
        scanf("%d", &id);
        for (i = 0; i < *goods_count; i++) {
            if (goods_list[i].id == id) {
                printf("请输入交易金额:");
                scanf("%f", &(order_list[*order_count].amount));
                order_list[*order_count].goods = goods_list[i];
                time_t timep;
                time(&timep);
                strftime(order_list[*order_count].time, sizeof(order_list[*order_count].time), "%Y-%m-%d %H:%M:%S", localtime(&timep));
                strcpy(order_list[*order_count].status, "已支付");
                (*order_count)++;
                printf("购买成功!\n");
                return;
            }
        }
        printf("找不到商品,请重新操作!\n");
    }
    
    1. 订单退款

    订单退款功能可以通过以下方式实现:

    void refund_order(struct order *order_list, int *order_count) {
        int i;
        printf("请输入要退款的订单编号:");
        scanf("%d", &i);
        if (i > 0 && i <= *order_count) {
            strcpy(order_list[i - 1].status, "已退款");
            printf("退款成功!\n");
        } else {
            printf("找不到订单,请重新操作!\n");
        }
    }
    
    1. 查看每日销售汇总

    查看每日销售汇总功能可以通过以下方式实现:

    void daily_sales_summary(struct order *order_list, int *order_count) {
        int i;
        float total_sales = 0;
        time_t timep;
        time(&timep);
        char today[11];
        strftime(today, sizeof(today), "%Y-%m-%d", localtime(&timep));
        printf("今日销售汇总:\n");
        for (i = 0; i < *order_count; i++) {
            if (strcmp(strtok(order_list[i].time, " "), today) == 0) {
                printf("商品名字:%s,交易金额:%.2f,交易状态:%s\n", order_list[i].goods.name, order_list[i].amount, order_list[i].status);
                total_sales += order_list[i].amount;
            }
        }
        printf("总销售额:%.2f\n", total_sales);
    }
    

    最后,我们可以在主函数中验证所有功能,并得出正确的运行结果。主函数可以通过以下方式实现:

    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include "vending_machine.h"
    
    int main() {
        struct goods goods_list[100];
        int goods_count = 0;
        struct order order_list[100];
        int order_count = 0;
        int choice;
        while (1) {
            printf("请选择功能:\n");
            printf("1. 增加商品\n");
            printf("2. 删除商品\n");
            printf("3. 修改商品\n");
            printf("4. 查找商品\n");
            printf("5. 下订单\n");
            printf("6. 订单退款\n");
            printf("7. 查看每日销售汇总\n");
            printf("8. 退出\n");
            scanf("%d", &choice);
            switch (choice) {
                case 1:
                    add_goods(goods_list, &goods_count);
                    break;
                case 2:
                    delete_goods(goods_list, &goods_count);
                    break;
                case 3:
                    modify_goods(goods_list, &goods_count);
                    break;
                case 4:
                    search_goods(goods_list, &goods_count);
                    break;
                case 5:
                    place_order(goods_list, &goods_count, order_list, &order_count);
                    break;
                case 6:
                    refund_order(order_list, &order_count);
                    break;
                case 7:
                    daily_sales_summary(order_list, &order_count);
                    break;
                case 8:
                    return 0;
                default:
                    printf("输入错误,请重新选择!\n");
                    break;
            }
        }
        return 0;
    }
    

    这样,一个C语言的自动售卖机模拟系统的程序就完成了。

C语言 自动售卖机器

#include <stdio.h>
#include <string.h>
int count=4;
int i,j;
typedef struct commodity
{
    char name[20];
    float price;
    float vip_p;
    int num;
    char bar_codes[10];
}goods,*goods_p;
int purchase_page(goods_p p)//购买界面
{
    int spbm;//记录商品编码
    float money;
    int h;//判断是否为会员
    while(1)
    {
        
        printf("商品编码\t商品名称\t普通价格\t会员价格\t商品数量\n");
        for(i=0 ; i<count ; i++)
        {
            printf("%d\t\t%s\t\t%.2f\t\t%.2f\t\t%d\n",
            i+1,(p+i)->name,(p+i)->price,(p+i)->vip_p,(p+i)->num);
        }
        printf("--------------------------------------------------------------------------\n");
        printf("----------------------------输入0返回上一级!-----------------------------\n");
        printf("请选择要购买商品的商品编码:");
        scanf("%d",&spbm);
        if(spbm == 0)
        {
            return 0;
        }
        printf("你有小店会员吗?有请输入'1'\n");
        getchar();
        scanf("%d",&h);
        if(h == 1)
        {
            printf("%s\t%.2f人民币\n",(p+spbm-1)->name,(p+spbm-1)->vip_p);
            printf("请输入你放入钞票的面值:");
            scanf("%f",&money);
            if(money < (p+spbm-1)->vip_p)
            {
                printf("金额不足,购买失败!");
                return 0;
            }
            printf("本次找零%.2f人民币!\n*********感谢你的消费,欢迎下次光临!*********\n*******如有问题,请拨打售后电话:1238438*******\n\n\n",
               money-((p+spbm-1)->vip_p));
        }
        else 
        {
            printf("%s\t%.2f人民币\n",(p+spbm-1)->name,(p+spbm-1)->price);
            printf("请输入你放入钞票的面值:");
            scanf("%f",&money);
            printf("本次找零%.2f人民币!\n感谢你的消费,欢迎下次光临!\n如有任何问题或不满意,请拨打售后电话:1238438\n\n\n",
            money-(p+spbm-1)->price);
        }
        ((p+spbm-1)->num)--;
    }
}
int management_page()//登录管理界面
{
    int error;
    char man_id[]="yyd";
    char man_mm[]="yyd";
    char id[20],mm[20];
    for(error=0 ; error<3 ; error++)
    {
        printf("账号:");
        scanf("%s",id);
        printf("密码:");
        scanf("%s",mm);
        if(strcmp(man_id,id) == 0)
        {
            if(strcmp(man_mm,mm) == 0)
            {
                printf("登录成功!\n");
                return 0;
            }
        }
        else
        {
            printf("账号有误,请重新输入,还有%d输入机会。\n",2-error);
        }
        if(error == 3)
        {
            return -1;
        }
    }
}
void add_goods(goods_p p)//添加商品
{
    printf("请输入要添加商品的信息:\n");
    printf("商品名称:");
    scanf("%s",(p+count)->name);
    printf("普通价格:");
    scanf("%f",&(p+count)->price);
    printf("会员价格:");
    scanf("%f",&(p+count)->vip_p);
    printf("商品数量:");
    scanf("%d",&(p+count)->num);
    printf("商品条码:");
    scanf("%s",(p+count)->bar_codes);
    count++;
}
void cat_goods(goods_p p)//查看商品
{
    printf("商品名称\t商品条码\t普通价格\t会员价格\t商品数量\n");
    for(i=0 ; i<count ; i++)
    {
        printf("%s \t\t%s \t\t%.2f \t\t%.2f \t\t%d\n",
        (p+i)->name,(p+i)->bar_codes,(p+i)->price,(p+i)->vip_p,(p+i)->num);
    }
}
void modify_goods(goods_p p)//修改商品
{
    char txbm[10]={0};
    printf("请输入要修改的商品的条形编码:\n");
    scanf("%s",txbm);
    for(i=0 ; i<count ; i++)
    {
        if(strcmp((p+i)->bar_codes,txbm) == 0)
        {
            printf("该条形编码所对应的商品名称是:%s\n",(p+i)->name);
            printf("商品名称:");
            scanf("%s",(p+i)->name);
            printf("普通价格:");
            scanf("%f",&(p+i)->price);
            printf("会员价格:");
            scanf("%f",&(p+i)->vip_p);
            printf("商品数量:");
            scanf("%d",&(p+i)->num);
            printf("商品条码:");
            scanf("%s",(p+i)->bar_codes);
        }
    }
}
int delete_goods(goods_p p)//删除商品
{
    char txbm[10]={0};
    printf("请输入要删除的商品的条形编码:\n");
    scanf("%s",txbm);
    for(i=0 ; i<count ; i++)
    {
        if(strcmp((p+i)->bar_codes,txbm) == 0)
        {
            for(j=i ; j<count ; j++)
            {
                strcpy((p+j)->name,(p+j+1)->name);
                (p+j)->price=(p+j+1)->price;
                (p+j)->vip_p=(p+j+1)->vip_p;
                (p+j)->num=(p+j+1)->num;
                strcpy((p+j)->bar_codes,(p+j+1)->bar_codes);
            }
        }
        else 
        {
            printf("没有找到该条形编码所对应的商品!\n");
            return 0;
        }
    }
    count--;
}
int choose_function(goods_p p)//选择功能
{
    while(1)
    {
        int a,b,c;
        printf("---------------------------1:购买模式---------------------------\n");
        printf("---------------------------2:管理模式---------------------------\n");
        printf("---------------------------0:退出系统---------------------------\n");
        scanf("%d",&a);
        switch(a)
        {
            case 0:return 0;
                break;
            case 1:
                purchase_page(p);
                break;
            case 2:
                b=management_page();
                if(b == 0)
                {    
                    while(1)
                    {
                        printf("------------------------1:添加商品种类------------------------\n");
                        printf("------------------------2:修改商品信息------------------------\n");
                        printf("------------------------3:删除商品信息------------------------\n");
                        printf("------------------------4:查看商品信息------------------------\n");
                        printf("------------------------0:返回上一级  ------------------------\n");
                        scanf("%d",&c);
                        switch(c)
                        {
                            case 0:
                                break;
                            case 1:
                                add_goods(p);
                                break;
                            case 2:
                                modify_goods(p);
                                break;
                            case 3:
                                delete_goods(p);
                                break;
                            case 4:
                                cat_goods(p);
                                break;
                            default :
                                break;
                        }
                        if(c==0) break;
                    }
                }
                break;
            default:
                break;
        }
    }
}
int main(int argc, char const *argv[])
{
    goods g_type[20] = {{"可乐",2.5,2.0,10,"00001"},
                        {"绿茶",3.0,2.5,10,"00002"},
                        {"桶面",4.0,3.5,10,"01003"},
                        {"卫生纸",8.5,8.0,16,"02004"}};
    goods_p p= g_type;
    choose_function(p);
    return 0;
}

c语言实现自动售货机程序,主要是考察程序的输入输出和条件语句,循环语句等基本知识的使用。由于比较基础,网上资料挺多的,建议你搜索下。这里也为你找到一些资料,看下是否是你想要的。
C语言实现最全自动售货机:https://m.jb51.net/article/235558.htm
https://blog.csdn.net/m0_58863372/article/details/122532615

可以参考一下


#include <stdio.h>
#include <string.h>
 
typedef struct {
    int id; // 商品编号
    char name[50]; // 商品名称
    float price; // 商品价格
} Product;
 
typedef struct {
    char time[20]; // 订单时间
    int productId; // 销售的商品ID
    float amount; // 交易金额
    char status[20]; // 交易状态
} Order;
 
// 添加一个商品
void addProduct(Product products[], int *count) {
    int id;
    char name[50];
    float price;
    printf("请输入商品编号:");
    scanf("%d", &id);
    printf("请输入商品名称:");
    scanf("%s", name);
    printf("请输入商品价格:");
    scanf("%f", &price);
    // 创建商品对象并添加到数组中
    Product p = {id, name, price};
    products[*count] = p;
    *count += 1;
    printf("商品添加成功!\n");
}
 
// 删除一个商品
void deleteProduct(Product products[], int *count) {
    int id, index = -1;
    printf("请输入要删除的商品编号:");
    scanf("%d", &id);
    // 查找要删除的商品的索引
    for (int i = 0; i < *count; i++) {
        if (products[i].id == id) {
            index = i;
            break;
        }
    }
    // 没有找到对应的商品
    if (index == -1) {
        printf("找不到要删除的商品!\n");
        return;
    }
    // 将后面的商品向前移动一个位置
    for (int i = index; i < *count - 1; i++) {
        products[i] = products[i + 1];
    }
    *count -= 1;
    printf("商品删除成功!\n");
}
 
// 修改一个商品
void modifyProduct(Product products[], int count) {
    int id, index = -1;
    printf("请输入要修改的商品编号:");
    scanf("%d", &id);
    // 查找要修改的商品的索引
    for (int i = 0; i < 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("商品修改成功!\n");
}
 
// 查询所有商品
void queryAllProducts(Product products[], int count) {
    printf("商品编号\t商品名称\t商品价格\n");
    for (int i = 0; i < count; i++) {
        printf("%d\t\t%s\t\t%.2f\n", products[i].id, products[i].name, products[i].price);
    }
}
 
// 添加一个订单
void addOrder(Order orders[], int *count, Product products[], int productCount) {
    int productId;
    float amount;
    // 显示商品列表
    printf("商品编号\t商品名称\t商品价格\n");
    for (int i = 0; i < productCount; i++) {
        printf("%d\t\t%s\t\t%.2f\n", products[i].id, products[i].name, products[i].price);
    }
    printf("请输入要购买的商品编号:");
    scanf("%d", &productId);
    // 查找商品是否存在
    int index = -1;
    for (int i = 0; i < productCount; i++) {
        if (products[i].id == productId) {
            index = i;
            break;
        }
    }
    // 没有找到对应的商品
    if (index == -1) {
        printf("找不到对应的商品!\n");
        return;
    }
    // 计算交易金额
    amount = products[index].price;
    // 创建订单对象并添加到数组中
    Order o = {"", productId, amount, "待支付"};
    orders[*count] = o;
    *count += 1;
    printf("订单生成成功!\n");
}
 
// 删除一个订单
void deleteOrder(Order orders[], int *count) {
    int index;
    printf("请输入要删除的订单序号:");
    scanf("%d", &index);
    // 没有找到对应的订单
    if (index < 0 || index >= *count) {
        printf("找不到要删除的订单!\n");
        return;
    }
    // 将后面的订单向前移动一个位置
    for (int i = index; i < *count - 1; i++) {
        orders[i] = orders[i + 1];
    }
    *count -= 1;
    printf("订单删除成功!\n");
}
 
// 修改一个订单
void modifyOrder(Order orders[], int count) {
    int index;
    printf("请输入要修改的订单序号:");
    scanf("%d", &index);
    // 没有找到对应的订单
    if (index < 0 || index >= count) {
        printf("找不到要修改的订单!\n");
        return;
    }
    // 修改订单状态
    printf("请输入新的订单状态:");
    scanf("%s", orders[index].status);
    printf("订单修改成功!\n");
}
 
// 查询所有订单
void queryAllOrders(Order orders[], int count) {
    printf("订单序号\t下单时间\t商品名称\t交易金额\t交易状态\n");
    for (int i = 0; i < count; i++) {
        printf("%d\t\t%s\t%d\t\t%.2f\t\t%s\n", i+1, orders[i].time, orders[i].productId, orders[i].amount, orders[i].status);
    }
}
 
// 日销售情况汇总
void dailySummary(Order orders[], int count) {
    float totalAmount = 0;
    int paidCount = 0;
    for (int i = 0; i < count; i++) {
        // 计算已支付订单的交易金额总和
        if (strcmp(orders[i].status, "已支付") == 0) {
            totalAmount += orders[i].amount;
            paidCount += 1;
        }
    }
    printf("当日销售情况汇总:\n");
    printf("已支付订单数:%d\n", paidCount);
    printf("交易金额总计:%.2f\n", totalAmount);
}
 
int main() {
    int productCount = 0;
    Product products[100];
    int orderCount = 0;
    Order orders[100];
    while (1) {
        // 显示菜单
        printf("\n-------------------------\n");
        printf("欢迎使用自动售货机管理系统!\n");
        printf("1.添加商品 2.删除商品 3.修改商品 4.查询所有商品\n");
        printf("5.生成订单 6.删除订单 7.修改订单 8.查询所有订单\n");
        printf("9.日销售情况汇总 0.退出\n");
        printf("-------------------------\n");
        // 读取用户输入的选项
        int option;
        printf("请选择操作:");
        scanf("%d", &option);
        // 执行对应的操作
        switch (option) {
            case 1:
                addProduct(products, &productCount);
                break;
            case 2:
                deleteProduct(products, &productCount);
                break;
            case 3:
                modifyProduct(products, productCount);
                break;
            case 4:
                queryAllProducts(products, productCount);
                break;
            case 5:
                addOrder(orders, &orderCount, products, productCount);
                break;
            case 6:
                deleteOrder(orders, &orderCount);
                break;
            case 7:
                modifyOrder(orders, orderCount);
                break;
            case 8:
                queryAllOrders(orders, orderCount);
                break;
            case 9:
                dailySummary(orders, orderCount);
                break;
            case 0:
                printf("谢谢使用,再见!\n");
                return 0;
            default:
                printf("无效的选项,请重新选择!\n");
        }
    }
    return 0;
}

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

#define MAX_GOODS 50      // 商品最大数量
#define MAX_ORDERS 1000   // 订单最大数量
#define MAX_NAME_LEN 20   // 商品名最大长度

// 商品结构体
typedef struct {
    int id;                      // 商品编号
    char name[MAX_NAME_LEN];     // 商品名
    float price;                 // 单价
    int stock;                   // 库存
} Goods;

// 订单结构体
typedef struct {
    char time[20];               // 订单发生的时间,格式为"YYYY-MM-DD HH:mm:ss"
    int gid;                     // 商品编号
    float amount;                // 交易金额
    int status;                  // 交易状态,0表示未完成,1表示已完成
} Order;

// 全局变量
Goods goods[MAX_GOODS];       // 商品信息数组
Order orders[MAX_ORDERS];     // 订单信息数组
int gcount = 0;               // 商品数量
int ocount = 0;               // 订单数量

// 显示菜单
void show_menu() {
    printf("\n***************************\n");
    printf("* 1. 商品信息的增删改查  *\n");
    printf("* 2. 订单信息的记录      *\n");
    printf("* 3. 日销售情况汇总      *\n");
    printf("* 0. 退出                 *\n");
    printf("***************************\n");
}

// 判断商品编号是否已存在
int is_goods_exist(int id) {
    int i;
    for (i = 0; i < gcount; i++) {
        if (goods[i].id == id) {
            return 1;
        }
    }
    return 0;
}

// 增加商品信息
void add_goods() {
    if (gcount >= MAX_GOODS) {
        printf("商品数量已达到最大值!\n");
        return;
    }
    printf("请输入商品编号:");
    int id;
    scanf("%d", &id);
    if (is_goods_exist(id)) {
        printf("商品编号已存在!\n");
        return;
    }
    printf("请输入商品名称:");
    char name[MAX_NAME_LEN];
    scanf("%s", name);
    printf("请输入商品单价:");
    float price;
    scanf("%f", &price);
    printf("请输入商品库存:");
    int stock;
    scanf("%d", &stock);
    goods[gcount].id = id;
    strncpy(goods[gcount].name, name, MAX_NAME_LEN - 1);
    goods[gcount].name[MAX_NAME_LEN - 1] = '\0';
    goods[gcount].price = price;
    goods[gcount].stock = stock;
    gcount++;
    printf("商品信息添加成功!\n");
}

// 显示商品信息
void show_goods() {
    int i;
    printf("\n商品编号\t商品名称\t单价\t库存\n");
    for (i = 0; i < gcount; i++) {
        printf("%d\t\t%s\t\t%.2f\t%d\n", goods[i].id, goods[i].name,
            goods[i].price, goods[i].stock);
    }
}

// 按编号查找商品在数组中的位置
int find_goods(int id) {
    int i;
    for (i = 0; i < gcount; i++) {
        if (goods[i].id == id) {
            return i;
        }
    }
    return -1;
}

// 修改商品信息
void modify_goods() {
    printf("请输入要修改的商品编号:");
    int id;
    scanf("%d", &id);
    int pos = find_goods(id);
    if (pos == -1) {
        printf("该商品不存在!\n");
        return;
    }
    printf("请输入新的商品名称:");
    char name[MAX_NAME_LEN];
    scanf("%s", name);
    printf("请输入新的商品单价:");
    float price;
    scanf("%f", &price);
    printf("请输入新的商品库存:");
    int stock;
    scanf("%d", &stock);
    strncpy(goods[pos].name, name, MAX_NAME_LEN - 1);
    goods[pos].name[MAX_NAME_LEN - 1] = '\0';
    goods[pos].price =price;
    goods[pos].stock = stock;
    printf("商品信息修改成功!\n");
}

// 删除商品信息
void delete_goods() {
    printf("请输入要删除的商品编号:");
    int id;
    scanf("%d", &id);
    int pos = find_goods(id);
    if (pos == -1) {
        printf("该商品不存在!\n");
        return;
    }
    // 将最后一个商品信息覆盖要删除的商品信息
    goods[pos] = goods[gcount - 1];
    gcount--;
    printf("商品信息删除成功!\n");
}

// 查询商品信息
void search_goods() {
    printf("请输入要查询的商品编号:");
    int id;
    scanf("%d", &id);
    int pos = find_goods(id);
    if (pos == -1) {
        printf("该商品不存在!\n");
        return;
    }
    printf("\n商品编号\t商品名称\t单价\t库存\n");
    printf("%d\t\t%s\t\t%.2f\t%d\n", goods[pos].id, goods[pos].name,
           goods[pos].price, goods[pos].stock);
}

// 生成当前时间字符串
void get_current_time(char *buf, int size) {
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    strftime(buf, size, "%Y-%m-%d %H:%M:%S", tm);
}

// 添加订单信息
void add_order() {
    if (ocount >= MAX_ORDERS) {
        printf("订单数量已达到最大值!\n");
        return;
    }
    printf("请输入购买商品编号:");
    int gid;
    scanf("%d", &gid);
    int pos = find_goods(gid);
    if (pos == -1) {
        printf("该商品不存在!\n");
        return;
    }
    printf("请输入购买数量:");
    int quantity;
    scanf("%d", &quantity);
    if (quantity > goods[pos].stock) {
        printf("商品库存不足!\n");
        return;
    }
    // 更新商品库存
    goods[pos].stock -= quantity;
    // 添加订单信息
    orders[ocount].gid = gid;
    orders[ocount].amount = quantity * goods[pos].price;
    orders[ocount].status = 0;
    get_current_time(orders[ocount].time, 20);
    ocount++;
    printf("订单添加成功!\n");
}

// 显示订单信息
void show_orders() {
    int i;
    char status[10];
    printf("\n订单编号\t商品名称\t单价\t数量\t总价\t交易时间\t交易状态\n");
    for (i = 0; i < ocount; i++) {
        int pos = find_goods(orders[i].gid);
        if (pos != -1) {
            if (orders[i].status == 0) {
                strcpy(status, "未完成");
            } else {
                strcpy(status, "已完成");
            }
            printf("%d\t\t%s\t\t%.2f\t%d\t%.2f\t%s\t%s\n", i+1, goods[pos].name,
                   goods[pos].price, orders[i].amount / goods[pos].price,
                   orders[i].amount, orders[i].time, status);
        }
    }
}

// 完成订单
void complete_order() {
    printf("请输入要完成的订单编号:");
    int id;
    scanf("%d", &id);
    if (id <= 0 || id > ocount) {
        printf("订单编号无效!\n");
        return;
    }
    if (orders[id-1].status == 1) {
        printf("该订单已完成!\n");
        return;
    }
    orders[id-1].status = 1;
    printf("订单完成成功!\n");
}

// 查看日销售情况
void show_daily_sales() {
    printf("\n日期\t\t销售总额\n");
    float total_sales = 0;
    char cur_date[20] = "";
    int i;
    for (i = 0; i < ocount; i++) {
        if (strcmp(orders[i].time, cur_date) != 0) {
            // 日期发生变化,输出上一天的销售总额
            if (strlen(cur_date) != 0) {
                printf("%s\t%.2f\n", cur_date, total_sales);
            }
            strcpy(cur_date, orders[i].time);
            total_sales = 0;
        }
        total_sales += orders[i].amount;
    }
    // 输出最后一天的销售总额
    if (strlen(cur_date) != 0){
        printf("%s\t%.2f\n", cur_date, total_sales);
    }
}

int main() {
    int choice;
    do {
        show_menu();
        printf("请输入您的选择:");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                // 商品信息的增删改查
                printf("\n商品信息管理\n");
                printf("1. 添加商品\n");
                printf("2. 显示商品信息\n");
                printf("3. 修改商品信息\n");
                printf("4. 删除商品信息\n");
                printf("5. 查询商品信息\n");
                printf("0. 返回\n");
                printf("请选择:");
                scanf("%d", &choice);
                switch (choice) {
                    case 1:
                        add_goods();
                        break;
                    case 2:
                        show_goods();
                        break;
                    case 3:
                        modify_goods();
                        break;
                    case 4:
                        delete_goods();
                        break;
                    case 5:
                        search_goods();
                        break;
                    case 0:
                        break;
                    default:
                        printf("无效选择!\n");
                        break;
                }
                break;
            case 2:
                // 订单信息的记录
                printf("\n订单信息管理\n");
                printf("1. 添加订单\n");
                printf("2. 显示订单信息\n");
                printf("3. 完成订单\n");
                printf("0. 返回\n");
                printf("请选择:");
                scanf("%d", &choice);
                switch (choice) {
                    case 1:
                        add_order();
                        break;
                    case 2:
                        show_orders();
                        break;
                    case 3:
                        complete_order();
                        break;
                    case 0:
                        break;
                    default:
                        printf("无效选择!\n");
                        break;
                }
                break;
            case 3:
                // 日销售情况汇总
                show_daily_sales();
                break;
            case 0:
                printf("谢谢使用,再见!\n");
                break;
            default:
                printf("无效选择!\n");
                break;
        }
    } while (choice != 0);

    return 0;
}

```

img

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

#define MAX_PRODUCTS 100
#define MAX_NAME_LENGTH 50
#define MAX_ORDERS 100

typedef struct {
    int productNumber;
    char name[MAX_NAME_LENGTH];
    double price;
} Product;

typedef struct {
    int orderId;
    char dateTime[20];
    Product product;
    double transactionAmount;
    char status[20];
} Order;

Product products[MAX_PRODUCTS];
int numProducts = 0;

Order orders[MAX_ORDERS];
int numOrders = 0;

void addProduct() {
    if (numProducts >= MAX_PRODUCTS) {
        printf("Product storage is full. Cannot add more products.\n");
        return;
    }

    printf("Enter product number: ");
    scanf("%d", &products[numProducts].productNumber);

    printf("Enter product name: ");
    scanf("%s", products[numProducts].name);

    printf("Enter product price: ");
    scanf("%lf", &products[numProducts].price);

    numProducts++;
    printf("Product added successfully.\n");
}

void deleteProduct() {
    if (numProducts == 0) {
        printf("No products found.\n");
        return;
    }

    int productNumber;
    printf("Enter product number to delete: ");
    scanf("%d", &productNumber);

    int foundIndex = -1;
    for (int i = 0; i < numProducts; i++) {
        if (products[i].productNumber == productNumber) {
            foundIndex = i;
            break;
        }
    }

    if (foundIndex == -1) {
        printf("Product not found.\n");
        return;
    }

    for (int i = foundIndex; i < numProducts - 1; i++) {
        products[i] = products[i + 1];
    }

    numProducts--;
    printf("Product deleted successfully.\n");
}

void updateProduct() {
    if (numProducts == 0) {
        printf("No products found.\n");
        return;
    }

    int productNumber;
    printf("Enter product number to update: ");
    scanf("%d", &productNumber);

    int foundIndex = -1;
    for (int i = 0; i < numProducts; i++) {
        if (products[i].productNumber == productNumber) {
            foundIndex = i;
            break;
        }
    }

    if (foundIndex == -1) {
        printf("Product not found.\n");
        return;
    }

    printf("Enter new product name: ");
    scanf("%s", products[foundIndex].name);

    printf("Enter new product price: ");
    scanf("%lf", &products[foundIndex].price);

    printf("Product updated successfully.\n");
}

void searchProduct() {
    if (numProducts == 0) {
        printf("No products found.\n");
        return;
    }

    int productNumber;
    printf("Enter product number to search: ");
    scanf("%d", &productNumber);

    int foundIndex = -1;
    for (int i = 0; i < numProducts; i++) {
        if (products[i].productNumber == productNumber) {
            foundIndex = i;
            break;
        }
    }

    if (foundIndex == -1) {
        printf("Product not found.\n");
        return;
    }

    printf("Product found:\n");
    printf("Product Number: %d\n", products[foundIndex].productNumber);
    printf("Name: %s\n", products[foundIndex].name);
    printf("Price: %.2lf\n", products[foundIndex].price);
}

void addOrder() {
    if (numProducts == 0) {
        printf("No products available. Cannot create order.\n");
        return;
    }

    if (numOrders >= MAX_ORDERS) {
        printf("Order storage is full. Cannot create more orders.\n");
        return;
    }

    int productNumber;
    printf("Enter product number for the order: ");
    scanf("%d", &productNumber);

    int foundIndex = -1;
    for (int i = 0; i < numProducts; i++) {
        if (products[i].productNumber == productNumber) {
            foundIndex = i;
            break;
        }
    }

    if (foundIndex == -1) {
        printf("Product not found.\n");
        return;
    }

    orders[numOrders].orderId = numOrders + 1;

    printf("Enter order date and time (YYYY-MM-DD HH:MM): ");
    scanf("%s", orders[numOrders].dateTime);

    orders[numOrders].product = products[foundIndex];

    printf("Enter transaction amount: ");
    scanf("%lf", &orders[numOrders].transactionAmount);

    printf("Enter order status: ");
    scanf("%s", orders[numOrders].status);

    numOrders++;
    printf("Order created successfully.\n");
}

void displaySalesSummary() {
    if (numOrders == 0) {
        printf("No orders found.\n");
        return;
    }

    double totalSales = 0.0;

    for (int i = 0; i < numOrders; i++) {
        totalSales += orders[i].transactionAmount;
    }

    printf("Total Sales: %.2lf\n", totalSales);
    printf("Number of Orders: %d\n", numOrders);
}

int main() {
    int choice;

    do {
        printf("\n--- Vending Machine Simulation ---\n");
        printf("1. Add Product\n");
        printf("2. Delete Product\n");
        printf("3. Update Product\n");
        printf("4. Search Product\n");
        printf("5. Add Order\n");
        printf("6. Display Sales Summary\n");
        printf("7. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addProduct();
                break;
            case 2:
                deleteProduct();
                break;
            case 3:
                updateProduct();
                break;
            case 4:
                searchProduct();
                break;
            case 5:
                addOrder();
                break;
            case 6:
                displaySalesSummary();
                break;
            case 7:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 7);

    return 0;
}

参考GPT

回答部分参考、引用ChatGpt以便为您提供更准确的答案: 根据您的描述,您需要编写一个简单的自动售货机模拟程序,包括商品信息的增删改查、订单信息的记录和日销售情况汇总。下面是一个可能的C语言程序的框架,以满足您的需求:

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

// 商品信息结构体
struct Product {
    int id;
    char name[50];
    float price;
};

// 订单信息结构体
struct Order {
    char timestamp[20];
    struct Product product;
    float amount;
    char status[20];
};

// 全局变量
struct Product products[100];  // 商品信息数组
int productCount = 0;  // 商品数量
struct Order orders[1000];  // 订单信息数组
int orderCount = 0;  // 订单数量

// 商品信息的增加
void addProduct(int id, const char* name, float price) {
    if (productCount < 100) {
        products[productCount].id = id;
        strcpy(products[productCount].name, name);
        products[productCount].price = price;
        productCount++;
        printf("商品信息添加成功!\n");
    } else {
        printf("商品信息已满,无法添加新商品!\n");
    }
}

// 商品信息的删除
void deleteProduct(int id) {
    int i;
    for (i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            // 将最后一个商品信息覆盖到当前位置
            products[i] = products[productCount - 1];
            productCount--;
            printf("商品信息删除成功!\n");
            return;
        }
    }
    printf("未找到指定的商品信息!\n");
}

// 商品信息的修改
void modifyProduct(int id, const char* name, float price) {
    int i;
    for (i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            strcpy(products[i].name, name);
            products[i].price = price;
            printf("商品信息修改成功!\n");
            return;
        }
    }
    printf("未找到指定的商品信息!\n");
}

// 商品信息的查询
void queryProduct(int id) {
    int i;
    for (i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            printf("商品编号:%d\n", products[i].id);
            printf("商品名称:%s\n", products[i].name);
            printf("商品价格:%.2f\n", products[i].price);
            return;
        }
    }
    printf("未找到指定的商品信息!\n");
}

// 订单信息的记录
void recordOrder(const char* timestamp, int productId, float amount, const char* status) {
    if (orderCount < 1000) {
        strcpy(orders[orderCount].timestamp, timestamp);
        orders[orderCount].product = products[productId];
        orders[orderCount].amount = amount;
        strcpy(orders[orderCount].status, status);
        orderCount++;
        printf("订单信息记录成功!\n");
    } else {
        printf("订单信息已满,无法记录新订单!\n");
    }
}

// 日销售情况汇总
void summarizeSales() {
    float totalSales = 0.0;
    int i;
    for (i = 0; i < orderCount; i++) {
        totalSales += orders[i].amount;
    }
    printf("日销售总额:%.2f\n", totalSales);
}

int main() {
    // 测试代码
    addProduct(1, "商品1", 10.0);
    addProduct(2, "商品2", 15.0);
    queryProduct(1);
    queryProduct(2);
    deleteProduct(1);
    modifyProduct(2, "新商品2", 20.0);
    queryProduct(1);
    queryProduct(2);
    recordOrder("2023-06-12 10:00:00", 0, 10.0, "已完成");
    recordOrder("2023-06-12 11:00:00", 1, 15.0, "已完成");
    summarizeSales();

    return 0;
}

这个程序框架包含了商品信息的增删改查功能,订单信息的记录功能以及日销售情况的汇总功能。您可以根据实际需求进行修改和完善。

注意,这只是一个简单的示例程序框架,具体实现还需要根据您的需求进行调整和扩展,比如添加错误处理、用户界面等。

引用chatgpt:


#include <stdio.h>

#include <stdlib.h>

#include <string.h>



typedef struct {

    int id;

    char name[20];

    float price;

} Product;



typedef struct {

    int time;

    Product product;

    float amount;

    int status;

} Order;



void addProduct(Product *products, int *count) {

    printf("请输入商品编号:");

    scanf("%d", &products[*count].id);

    printf("请输入商品名称:");

    scanf("%s", products[*count].name);

    printf("请输入商品价格:");

    scanf("%f", &products[*count].price);

    (*count)++;

}



void deleteProduct(Product *products, int count) {

    int index;

    printf("请输入要删除的商品编号:");

    scanf("%d", &index);

    if (index >= count) {

        printf("商品编号不存在!

");

        return;

    }

    memmove(&products[index], &products[index + 1], (count - index) * sizeof(Product));

    (*count)--;

}



void updateProduct(Product *products, int count) {

    int index;

    printf("请输入要更新的商品编号:");

    scanf("%d", &index);

    if (index >= count) {

        printf("商品编号不存在!

");

        return;

    }

    printf("请输入新的商品名称:");

    scanf("%s", products[index].name);

    printf("请输入新的商品价格:");

    scanf("%f", &products[index].price);

}



void queryOrder(Order *orders, int count) {

    int index;

    int orderTime;

    int orderAmount;

    int orderStatus;

    int choice;

    int totalAmount = 0;

    int totalCount = count; // 总订单数,用于日销售情况汇总时使用

    int i = count; // 从最后一个订单开始遍历,因为前一个订单的结束时间是当前订单的开始时间,所以不需要考虑前一个订单的结束时间是否小于当前订单的开始时间的情况。

    int day = orders[i].time % (24 * 3600); // 将订单时间转换为天数,用于日销售情况汇总时使用。

    int month = (orders[i].time % (30 * 24 * 3600)) / (24 * 3600); // 将订单时间转换为月份,用于日销售情况汇总时使用。

    int year = orders[i].time % (12 * 30 * 24 * 3600) / (30 * 24 * 3600); // 将订单时间转换为年份,用于日销售情况汇总时使用。
    int dayOfMonth = orders[i].time % (24 * 3600) % (24 * 3600) + (orders[i].time % (24 * 3600) > (24 * 3600) || orders[i].time % (24 * 3600) == (24 * 3600) && orders[i].time % (24 * 3600) > (24 * 3600)) * (24 * 3600); // 将订单时间转换为当月的第几天,用于日销售情况汇总时使用。注意这里需要处理特殊情况,即当月第一天和最后一天的情况。

    int weekDay = orders[i].time % (7 * 24 * 3600) % (7 * 24 * 3600) + (orders[i].time % (7 * 24 *

GPT:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_PRODUCTS 100
#define MAX_ORDERS 100

typedef struct {
    int productID;
    char productName[50];
    float price;
} Product;

typedef struct {
    int orderID;
    char datetime[20];
    Product product;
    float amount;
    char status[20];
} Order;

Product products[MAX_PRODUCTS];
int numProducts = 0;

Order orders[MAX_ORDERS];
int numOrders = 0;

void addProduct() {
    if (numProducts >= MAX_PRODUCTS) {
        printf("已达到最大商品数量限制!\n");
        return;
    }

    printf("请输入商品编号: ");
    scanf("%d", &products[numProducts].productID);
    printf("请输入商品名字: ");
    scanf("%s", products[numProducts].productName);
    printf("请输入商品价格: ");
    scanf("%f", &products[numProducts].price);

    numProducts++;
    printf("商品添加成功!\n");
}

void deleteProduct() {
    if (numProducts == 0) {
        printf("没有可删除的商品!\n");
        return;
    }

    int productID, i, found = 0;
    printf("请输入要删除的商品编号: ");
    scanf("%d", &productID);

    for (i = 0; i < numProducts; i++) {
        if (products[i].productID == productID) {
            found = 1;
            break;
        }
    }

    if (found) {
        for (; i < numProducts - 1; i++) {
            products[i] = products[i + 1];
        }
        numProducts--;
        printf("商品删除成功!\n");
    }
    else {
        printf("未找到该商品编号!\n");
    }
}

void editProduct() {
    if (numProducts == 0) {
        printf("没有可编辑的商品!\n");
        return;
    }

    int productID, i, found = 0;
    printf("请输入要编辑的商品编号: ");
    scanf("%d", &productID);

    for (i = 0; i < numProducts; i++) {
        if (products[i].productID == productID) {
            found = 1;
            break;
        }
    }

    if (found) {
        printf("请输入新的商品名字: ");
        scanf("%s", products[i].productName);
        printf("请输入新的商品价格: ");
        scanf("%f", &products[i].price);
        printf("商品编辑成功!\n");
    }
    else {
        printf("未找到该商品编号!\n");
    }
}

void displayProducts() {
    if (numProducts == 0) {
        printf("暂无商品信息!\n");
        return;
    }

    printf("商品信息列表:\n");
    printf("---------------------------\n");
    printf("编号\t名称\t价格\n");
    printf("---------------------------\n");
    for (int i = 0; i < numProducts; i++) {
        printf("%d\t%s\t%.2f\n", products[i].productID, products[i].productName, products[i].price);
    }
    printf("---------------------------\n");
}

void placeOrder() {
    if (numProducts == 0) {
        printf("没有可供销售的商品!\n");
        return;
    }

    if (numOrders >= MAX_ORDERS) {
        printf("已达到最大订单数量限制!\n");
        return;
    }

    int productID, i, found = 0;
    printf("请输入要购买的商品编号: ");
    scanf("%d", &productID);

    for (i = 0; i < numProducts; i++) {
        if (products[i].productID == productID) {
            found = 1;
            break;
        }
    }

    if (found) {
        orders[numOrders].orderID = numOrders + 1;

        time_t now;
        time(&now);
        struct tm *local = localtime(&now);
        strftime(orders[numOrders].datetime, sizeof(orders[numOrders].datetime), "%Y-%m-%d %H:%M:%S", local);

        orders[numOrders].product = products[i];

        printf("请输入交易金额: ");
        scanf("%f", &orders[numOrders].amount);

        strcpy(orders[numOrders].status, "已完成");

        numOrders++;
        printf("订单创建成功!\n");
    }
    else {
        printf("未找到该商品编号!\n");
    }
}

void displayOrders() {
    if (numOrders == 0) {
        printf("暂无订单信息!\n");
        return;
    }

    printf("订单信息列表:\n");
    printf("---------------------------------------------\n");
    printf("订单ID\t时间\t\t\t商品名\t价格\t交易金额\t交易状态\n");
    printf("---------------------------------------------\n");
    for (int i = 0; i < numOrders; i++) {
        printf("%d\t%s\t%s\t%.2f\t%.2f\t\t%s\n", orders[i].orderID, orders[i].datetime,
            orders[i].product.productName, orders[i].product.price, orders[i].amount, orders[i].status);
    }
    printf("---------------------------------------------\n");
}

void dailySummary() {
    if (numOrders == 0) {
        printf("暂无销售情况!\n");
        return;
    }

    float totalSales = 0;
    for (int i = 0; i < numOrders; i++) {
        totalSales += orders[i].amount;
    }

    printf("日销售情况汇总:\n");
    printf("------------------------\n");
    printf("订单数量: %d\n", numOrders);
    printf("销售总金额: %.2f\n", totalSales);
    printf("------------------------\n");
}

int main() {
    int choice;

    while (1) {
        printf("\n自动售货机模拟\n");
        printf("1. 增加商品\n");
        printf("2. 删除商品\n");
        printf("3. 编辑商品\n");
        printf("4. 显示商品信息\n");
        printf("5. 下订单\n");
        printf("6. 显示订单信息\n");
        printf("7. 销售情况汇总\n");
        printf("0. 退出\n");
        printf("请输入您的选择: ");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            addProduct();
            break;
        case 2:
            deleteProduct();
            break;
        case 3:
            editProduct();
            break;
        case 4:
            displayProducts();
            break;
        case 5:
            placeOrder();
            break;
        case 6:
            displayOrders();
            break;
        case 7:
            dailySummary();
            break;
        case 0:
            printf("谢谢使用,再见!\n");
            exit(0);
        default:
            printf("无效的选择!\n");
            break;
        }
    }

    return 0;
}

img

img

来自gpt,能用就用,不必采纳

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

#define MAX_PRODUCTS 100
#define MAX_ORDERS 100

// 商品结构体
typedef struct {
    int id;
    char name[50];
    double price;
} Product;

// 订单结构体
typedef struct {
    int id;
    char timestamp[50];
    Product product;
    double amount;
    char status[50];
} Order;

// 全局变量
Product products[MAX_PRODUCTS];
Order orders[MAX_ORDERS];
int productCount = 0;
int orderCount = 0;

// 函数声明
void addProduct();
void deleteProduct();
void updateProduct();
void searchProduct();
void placeOrder();
void updateOrderStatus();
void summarizeSales();

// 主函数
int main() {
    int choice;
    
    while (1) {
        printf("\n-------------------------\n");
        printf("自动售货机管理系统\n");
        printf("-------------------------\n");
        printf("1. 添加商品\n");
        printf("2. 删除商品\n");
        printf("3. 修改商品信息\n");
        printf("4. 查询商品信息\n");
        printf("5. 下单购买\n");
        printf("6. 更新订单状态\n");
        printf("7. 汇总销售情况\n");
        printf("8. 退出\n");
        printf("-------------------------\n");
        printf("请选择操作: ");
        scanf("%d", &choice);
        getchar(); // 清空输入缓冲区
        
        switch (choice) {
            case 1:
                addProduct();
                break;
            case 2:
                deleteProduct();
                break;
            case 3:
                updateProduct();
                break;
            case 4:
                searchProduct();
                break;
            case 5:
                placeOrder();
                break;
            case 6:
                updateOrderStatus();
                break;
            case 7:
                summarizeSales();
                break;
            case 8:
                printf("谢谢使用,再见!\n");
                exit(0);
            default:
                printf("无效的选项,请重新选择。\n");
                break;
        }
    }
    
    return 0;
}

// 添加商品
void addProduct() {
    if (productCount >= MAX_PRODUCTS) {
        printf("商品数量已达到最大限制,无法添加新商品。\n");
        return;
    }
    
    Product newProduct;
    printf("请输入商品编号: ");
    scanf("%d", &newProduct.id);
    printf("请输入商品名称: ");
    scanf("%s", newProduct.name);
    printf("请输入商品价格: ");
    scanf("%lf", &newProduct.price);
    
    products[productCount++] = newProduct;
    printf("商品添加成功。\n");
}

// 删除商品
void deleteProduct() {
    if (productCount == 0) {
        printf("当前没有任何商品。\n");
        return;
    }
    
    int productId;
    printf("请输入要删除的商品编号: ");
    scanf("%d", &productId);
    
    int i;
    for (i = 0; i < productCount; i++) {
        if (products[i].id == productId) {
            // 将最后一个商品移到当前位置,实现删除
            products[i] = products[productCount - 1];
            productCount--;
            printf("商品删除成功。\n");
            return;
        }
    }
    
    printf("未找到指定编号的商品。\n");
}

// 修改商品信息
void updateProduct() {
    if (productCount == 0) {
        printf("当前没有任何商品。\n");
        return;
    }
    
    int productId;
    printf("请输入要修改的商品编号: ");
    scanf("%d", &productId);
    
    int i;
    for (i = 0; i < productCount; i++) {
        if (products[i].id == productId) {
            printf("当前商品信息如下:\n");
            printf("编号:%d\n", products[i].id);
            printf("名称:%s\n", products[i].name);
            printf("价格:%lf\n", products[i].price);
            
            printf("请输入新的商品名称(不修改请直接回车): ");
            scanf("%s", products[i].name);
            printf("请输入新的商品价格(不修改请直接回车): ");
            scanf("%lf", &products[i].price);
            
            printf("商品信息修改成功。\n");
            return;
        }
    }
    
    printf("未找到指定编号的商品。\n");
}

// 查询商品信息
void searchProduct() {
    if (productCount == 0) {
        printf("当前没有任何商品。\n");
        return;
    }
    
    int productId;
    printf("请输入要查询的商品编号: ");
    scanf("%d", &productId);
    
    int i;
    for (i = 0; i < productCount; i++) {
        if (products[i].id == productId) {
            printf("商品信息如下:\n");
            printf("编号:%d\n", products[i].id);
            printf("名称:%s\n", products[i].name);
            printf("价格:%lf\n", products[i].price);
            return;
        }
    }
    
    printf("未找到指定编号的商品。\n");
}

// 下单购买
void placeOrder() {
    if (productCount == 0) {
        printf("当前没有任何商品。\n");
        return;
    }
    
    int productId;
    printf("请输入要购买的商品编号: ");
    scanf("%d", &productId);
    
    int i;
    for (i = 0; i < productCount; i++) {
        if (products[i].id == productId) {
            Order newOrder;
            
            printf("请输入订单发生的时间: ");
            scanf("%s", newOrder.timestamp);
            newOrder.product = products[i];
            printf("请输入交易金额: ");
            scanf("%lf", &newOrder.amount);
            strcpy(newOrder.status, "待发货");
            
            orders[orderCount++] = newOrder;
            printf("订单下单成功。\n");
            return;
        }
    }
    
    printf("未找到指定编号的商品。\n");
}

// 更新订单状态
void updateOrderStatus() {
    if (orderCount == 0) {
        printf("当前没有任何订单。\n");
        return;
    }
    
    int orderId;
    printf("请输入要更新状态的订单编号: ");
    scanf("%d", &orderId);
    
    int i;
    for (i = 0; i < orderCount; i++) {
        if (orders[i].id == orderId) {
            printf("当前订单信息如下:\n");
            printf("编号:%d\n", orders[i].id);
            printf("时间:%s\n", orders[i].timestamp);
            printf("商品编号:%d\n", orders[i].product.id);
            printf("商品名称:%s\n", orders[i].product.name);
            printf("金额:%lf\n", orders[i].amount);
            printf("状态:%s\n", orders[i].status);
            
            printf("请输入新的订单状态(不修改请直接回车): ");
            scanf("%s", orders[i].status);
            
            printf("订单状态更新成功。\n");
            return;
        }
    }
    
    printf("未找到指定编号的订单。\n");
}

// 汇总销售情况
void summarizeSales() {
    if (orderCount == 0) {
        printf("当前没有任何订单。\n");
        return;
    }
    
    double totalSales = 0.0;
    
    int i;
    for (i = 0; i < orderCount; i++) {
        totalSales += orders[i].amount;
    }
    
    printf("销售总额:%lf\n", totalSales);
    printf("订单数量:%d\n", orderCount);
}



#include<stdio.h>
int main()
{
    printf("                自动售货机               \n");
    printf(" 2元:A康师傅矿泉水 B怡宝矿泉水 C农夫山泉\n");
    printf(" 3元:    A可乐      B雪碧      C果汁    \n");
    printf(" 5元:    A奶茶      B咖啡      C酸奶    \n");
    printf("                 欢迎选购!              \n");
    printf("请投入钱币:");
    int money;
    char drink;
    scanf("%d",&money);
    getchar();
    printf("请选择饮品:");
    drink=getchar();
    if(money==2)
      switch(drink)
      {
          case'A':printf("康师傅矿泉水\n");break;
          case'B':printf("怡宝矿泉水\n");break;
          case'C':printf("农夫山泉\n");break;
        }
      else if(money==3)
       switch(drink) 
       {
          case'A':printf("可乐\n");break;
          case'B':printf("雪碧\n");break;
          case'C':printf("果汁\n");break;
         }
     else if(money==5)
       switch(drink) 
       {
          case'A':printf("奶茶\n");break;
          case'B':printf("咖啡\n");break;
          case'C':printf("酸奶\n");break;
         }
     return 0; 
}