自动售货机的程序代码怎么写? C语言大作业要交的,大路大神帮帮忙椆( ✀ω✀ )䠆

2024-12-25 14:58:52
推荐回答(1个)
回答1:

整个程序的功能 :

商品入库  补货

商品购买  找零

还有乱七八糟的一堆验证

数据只有结构链表存储,退出不保存,你要写文件或数据库保存,你自己写吧,写不动了。

演示看下图:

#include
#include
#include
#define nameSize 21 //商品名称数组大小 可修改
typedef struct autoMA
{
    char *spName;//商品名
    int  spNum;//商品数量
    float spValue;//商品价格
    struct autoMA *next;
}AMA;

AMA *amaNEW,*amaP1,*amaSelect;//定义一个新节点指针  首节点指针  查找用指针
int contStrplen(char *p);
int eqStr(char *str1,char *str2);//比较字符串是否相等,相等返回1 否则返回0
AMA * selectSP(AMA * AMAhead,char *spName);//通过名称查找商品 有返回节点地址  没有返回NULL
void insertSP(AMA *AMAhead,AMA **AMAtail,char *VSPname,int VSPnum,float VSPvalue);//添加商品  如果已有该商品,数量+    如果没有 新增
float * buySP(AMA *AMAhead,char *name,int num,float pay);//购买   返回float数组[2]

int main()
{
    AMA *AMAtail,*AMAhead;//定义一个头节点指针  尾节点指针
    float *b,value;
    int num,answer;
    char name[nameSize];
    amaSelect=(AMA *)malloc(sizeof(AMA));
    AMAhead=(AMA *)malloc(sizeof(AMA));
    AMAtail=(AMA *)malloc(sizeof(AMA));
    AMAhead->next=NULL;

mu: answer=0;
    memset(name,0,nameSize);
    num=0;
    value=0;
    printf("=====系统菜单====\n");
    printf("补充库存,输入1\n");
    printf("购买商品,输入2\n");
    scanf("%d",&answer);
    if(answer==2 && AMAhead->next==NULL)
    {
        printf("当前没有商品库存,请先补充库存\n");
        goto mu;
    }
    if(answer==1)
    {
        printf("输入 ESC 0 0 返回菜单\n");
        while(1)
        {
            printf("\n请录入补充的商品名称(名称小于%d个字符,每项空格分隔):",(nameSize-1)/2);
            scanf("%s",name);
            printf("请录入补充的数量:");
            scanf("%d",&num);
            printf("请录入单价:");
            scanf("%f",&value);
            if(!eqStr(name,"ESC") || num!=0 || value!=0)
            {
                insertSP(AMAhead,&AMAtail,name,num,value);
            }
            else
            {
                printf("\n录入结束返回主菜单\n");
                goto mu;
            }
        }

    }
    if(answer==2 && AMAhead->next!=NULL)
    {
tb:     memset(name,0,nameSize);
        num=0;
        value=0;
        printf("输入 ESC 0 0 返回菜单\n");
        while(1)
        {
            printf("\n请输入要购买的商品名称(名称小于%d个字符):",(nameSize-1)/2);
            scanf("%s",name);
            printf("请输入要购买的数量:");
            scanf("%d",&num);
            printf("请输入支付金额:");
            scanf("%f",&value);
            if(!eqStr(name,"ESC") || num!=0 || value!=0)
            {
                b=buySP(AMAhead,name,num,value);
                switch ((int)b[0])
                {
                    case 1:
                        printf("\n购买商品成功,找回零钱%.2f元\n",b[1]);
                        break;
                    case 2:
                        printf("\n商品库存不足,当前剩余数量%d,请重新选择商品\n",(int)b[1]);
                        goto tb;
                        break;
                    case 3:
                        printf("\n支付金额不足,还需支付%.2f元\n",b[1]);
                        goto tb;
                        break;
                    default:
                        printf("\n发生错误!交易失败!\n");
                        break;
                }
                free(b);
                b=NULL;
            }
            else
            {
                printf("\n购买结束返回主菜单\n");
                goto mu;
            }
        }
    }



    return 0;
}
int contStrplen(char *p)//计算字符串指针指向空间的字符串长度  并返回长度
{
    int len=0,i=0;
    while(p[i]!='\0')
    {
        len++;
        i++;
    }
    return len;
}
int eqStr(char *str1,char *str2)//比较字符串是否相等,相等返回1 否则返回0
{
    int i;
    if(strlen(str1)!=strlen(str2))
        return 0;
    for(i=0;i    {
        if(str1[i]!=str2[i])
            return 0;
    }
    return 1;
}
AMA * selectSP(AMA * AMAhead,char *spName)//通过名称查找商品 有返回节点地址  没有返回NULL
{
    if(AMAhead->next==NULL)
    {
        return NULL;
    }

    else
    {
        AMAhead->next=amaP1;//遍历查询前将头节点链表指针重置到首节点 为下次查询准备
        while(AMAhead->next!=NULL)
        {
            if(eqStr(AMAhead->next->spName,spName))
            {
                return AMAhead->next;
            }
            AMAhead=AMAhead->next;
        }
    }
    return NULL;
}
void insertSP(AMA *AMAhead,AMA **AMAtail,char *VSPname,int VSPnum,float VSPvalue)//添加商品  如果已有该商品,数量+    如果没有 新增
//参数: 头结点指针地址    尾节点指针地址    商品名称
{
    amaSelect=selectSP(AMAhead,VSPname);
    if(amaSelect!=NULL)//商品已存在 数量++  核实价格
    {
        printf("\n商品%s已存在库存%d个,现添加%d个,现在共有库存%d个\n",amaSelect->spName,amaSelect->spNum,VSPnum,(amaSelect->spNum)+VSPnum);
        (amaSelect->spNum)=(amaSelect->spNum)+VSPnum;
        if(amaSelect->spValue!=VSPvalue)
        {
            printf("--该录入的价格与原价格不一致,保留原价格,如要更新价格,请在【更新商品信息】功能中修改(该功能暂未实现)\n");

        }
    }
    else// 新增商品
    {
        amaNEW=(AMA*)malloc(sizeof(AMA));
        amaNEW->spName=(char *)malloc(sizeof(char)*(contStrplen(VSPname)+1));//按照输入字符串长度申请内存空间大小
        strcpy(amaNEW->spName,VSPname);
        amaNEW->spNum=VSPnum;
        amaNEW->spValue=VSPvalue;
        amaNEW->next=NULL;

        if(AMAhead->next==NULL)//首次新增  该节点为首节点  头结点链表指针和尾节点指针均指向该节点首地址
        {
            amaP1=amaNEW;
            AMAhead->next=amaP1;
            *AMAtail=amaP1;
        }
        else//添加到链表
        {
            (*AMAtail)->next=amaNEW;
            *AMAtail=amaNEW;
        }
        printf("\n商品%s,数量%d个,价格%.1f元 已添加到贩卖机中\n",VSPname,VSPnum,VSPvalue);
    }

}
float * buySP(AMA *AMAhead,char *name,int num,float pay)//购买   返回float数组[2]
//购买商品成功返回[0]=1 [1]=找还金额
//失败库存不足返回[0]=2 [1]=当前库存
//失败金额不足返回[0]=3 [1]=还需支付金额
//失败货物不存在(一般不可能出现) [0]=4 [1]=4
{
    float *err=(float *)malloc(sizeof(float)*2);
    amaSelect=selectSP(AMAhead,name);
    if(amaSelect!=NULL)//商品已存在 核实数量和价格
    {
        if((amaSelect->spNum)        {
            err[0]=2;
            err[1]=(float)(amaSelect->spNum);
        }
        if((amaSelect->spNum)>=num && num*(amaSelect->spValue)>pay)
        {
            err[0]=3;
            err[1]=num*(amaSelect->spValue)-pay;
        }
        if((amaSelect->spNum)>=num && num*(amaSelect->spValue)<=pay)
        {
            err[0]=1;
            err[1]=pay-num*(amaSelect->spValue);
            amaSelect->spNum=amaSelect->spNum-num;
        }
    }
    else
    {
        err[0]=4;
        err[1]=4;
    }
    return err;
}