AC_wy 2023-04-06 16:45 采纳率: 83.3%
浏览 64
已结题

c语言工程实践小超市商品管理系统

`
小超市商品管理系统
• 使用文件、链表,系统以菜单方式工作。
• 某商店每天有进货、售货、统计销售额、计算毛利率、查看商品剩余量等业务。设计一个菜单,实现下述功能
• 创建商品档案。每一个商品信息包括编号、品名、进价、售价、进货量、销售量、销售额、剩余数、毛利;考虑商品信息的存储。
• 编辑商品信息。例如向商品档案添加新商品,修改商品信息,删除原有商品、处理原有商品的新进货量、新销售量、报废量;
• 统计销售情况。在此项中统计每种商品的销售额、剩余数、毛利(毛利=销售额-进价*销售量);
• 查询商品信息。如:根据商品名、剩余数(小于5或大于20)进行查询;
• 显示商品信息。显示方式有3种:即按原来商品顺序显示、按销售额高到低的顺序显示、按毛利高到低的顺序显示。由于商品较多,所以要求分屏显示。
• 退出系统。

出现的问题有:无法将数据写入文件,新增数据将原来创建的商品档案覆盖
求帮助!


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXSIZE 50

void meal();/*菜单*/
int create(struct goods **head);/*创建商品*/
int edit(struct goods **head);/*修改商品信息*/
int del(struct goods **head);/*删除原有商品*/
int purchase(struct goods **head);/*原有商品的新进货量*/
int sales(struct goods **head);/*原有商品的新销售量*/
int baofei(struct goods **head);/*原有商品的报废量*/
int sale(struct goods **head);/*统计销售情况*/
void baocun(struct goods **head);/*保存商品信息*/
void namechaxun(struct goods **head);/*根据商品名查询*/
void shuchaxun(struct goods **head);/*根据剩余数查询*/
void xianshi(struct goods **head);/*按原来商品顺序显示*/
void amount(struct goods **head);/*按销售额由高到低的顺序显示*/
void gross_profit(struct goods **head);/*按毛利由高到低的顺序显示*/
void add(struct goods **head);/*添加新商品*/

struct goods
{
    char id[20];/*编号*/
    char name[20];/*品名*/
    double purchase_price;/*进价*/
    double selling_price;/*售价*/
    int purchase_quantity;/*进货量*/
    int sales_volume;/*销售量*/
    double amount;/*销售额*/
    int remaining_quantity;/*剩余数*/
    double gross_profit;/*毛利*/
    struct goods *next;
};


int main()
{
    struct goods *L=NULL;
    int i;
    while(1)
    {
        meal();
        scanf("%d",&i);
        if(i==1)
        {
            system("cls");
            create(&L);
            system("cls");
        }
        else if(i==2)
        {
            system("cls");
            edit(&L);
        }
        else if(i==3)
        {
            system("cls");
            del(&L);
        }
        else if(i==4)
        {
            system("cls");
            purchase(&L);
        }
        else if(i==5)
        {
            system("cls");
            sales(&L);
        }
        else if(i==6)
        {
            system("cls");
            baofei(&L);
        }
        else if(i==7)
        {
            system("cls");
            sale(&L);
        }
        else if(i==8)
        {
            system("cls");
            baocun(&L);
        }
        else if(i==9)
        {
            system("cls");
            namechaxun(&L);
        }
        else if(i==10)
        {
            system("cls");
            shuchaxun(&L);
        }
        else if(i==11)
        {
            system("cls");
            xianshi(&L);
        }
        else if(i==12)
        {
            system("cls");
            amount(&L);
        }
        else if(i==13)
        {
            system("cls");
            gross_profit(&L);
        }
        else if(i==15)
        {
            system("cls");
            add(&L);
        }
        else if(i==0)
        {
            system("cls");
            break;
        }
        else
        {
            printf("请重新选择:  ");
            scanf("%d",&i);
        }
    }
}


/*菜单*/
void meal()
{
    printf("-------------------菜单-------------------\n");
    printf("|      0. 退出系统                       |\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("|      9. 根据商品名查询                 |\n");
    printf("|      10.根据剩余数查询                 |\n");
    printf("|      11.按原来商品顺序显示             |\n");
    printf("|      12.按销售额由高到低的顺序显示     |\n");
    printf("|      13.按毛利由高到低的顺序显示       |\n");
    printf("|      14.读取商品信息                   |\n");
    printf("|      15.添加新商品                     |\n");
    printf("------------------------------------------");
    printf("\n");
    printf("\n");
    printf("请选择您要进行的操作选项:   ");
}


/*创建商品*/
int create(struct goods **head)
{
    struct goods *p,*tail=NULL;
    *head=NULL;
    int i,num;
    printf("请输入需添加的商品个数: \n");
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
        p=(struct goods *)malloc(sizeof(struct goods));
        p->next=NULL;
        printf("请输入商品编号: \n");
        scanf("%s",p->id);
        printf("请输入商品品名: \n");
        scanf("%s",p->name);
        printf("请输入商品进价: \n");
        scanf("%lf",&p->purchase_price);
        printf("请输入商品售价: \n");
        scanf("%lf",&p->selling_price);
        printf("请输入商品进货量: \n");
        scanf("%d",&p->purchase_quantity);
        printf("请输入商品销售量: \n");
        scanf("%d",&p->sales_volume);
        p->amount=(p->selling_price)*(p->sales_volume);
        printf("该商品销售额为: %lf\n",p->amount);
        p->remaining_quantity=(p->purchase_quantity)-(p->sales_volume);
        printf("该商品剩余数为: %d\n",p->remaining_quantity);
        p->gross_profit=(p->amount)-(p->purchase_price)*(p->sales_volume);
        printf("该商品毛利为: %lf\n",p->gross_profit);
        printf("\n\n");
        if(*head==NULL)
            *head=p;
        else
            tail->next=p;
        tail=p;
    }
}


/*修改商品信息*/
int edit(struct goods **head)
{
    struct goods *p=*head,*p1=*head;
    int flag=0;
    char num[MAXSIZE];
    printf("请输入需修改的商品的编号: \n");
    scanf("%s",num);
    while(p1)
    {
        if(strcmp(num,p1->id)==0)
        {
            flag=1;
            break;
        }
        p1=p1->next;
    }
    if(flag==1)
    {
        while(p)
        {
            if(strcmp(num,p->id)==0)
            {
                printf("请输入商品编号: \n");
                scanf("%s",p->id);
                printf("请输入商品品名: \n");
                scanf("%s",p->name);
                printf("请输入商品进价: \n");
                scanf("%lf",&p->purchase_price);
                printf("请输入商品售价: \n");
                scanf("%lf",&p->selling_price);
                printf("请输入商品进货量: \n");
                scanf("%d",&p->purchase_quantity);
                printf("请输入商品销售量: \n");
                scanf("%d",&p->sales_volume);
                p->amount=(p->sales_volume)*(p->selling_price);
                printf("该商品销售额为: %lf\n",p->amount);
                p->remaining_quantity=(p->purchase_quantity)-(p->sales_volume);
                printf("该商品剩余数为: %d\n",p->remaining_quantity);
                p->gross_profit=(p->amount)-(p->purchase_price)*(p->sales_volume);
                printf("该商品毛利为: %lf\n",p->gross_profit);
                printf("\n");
                printf("修改成功!\n");
            }
            p=p->next;
        }
    }
    else
        printf("该商品不存在!\n");
}


/*删除原有商品*/
int del(struct goods **head)
{
    int flag=0;
    char num[MAXSIZE];
    printf("请输入要删除的商品编号: \n");
    scanf("%s",num);
    struct goods *p=*head,*p1=*head;
    while(p1)
    {
        if(strcmp(num,p1->id)==0)
        {
            flag=1;
            break;
        }
        p1=p1->next;
    }
    if(flag==1)
    {
        if(strcmp(num,p->id)==0)
        {
            *head=p->next;
            printf("删除成功!\n");
        }
        else
        {
            while(p)
            {
                if(p->next&&strcmp(num,p->next->id)==0)
                {
                    p->next=p->next->next;
                    printf("删除成功!\n");
                }
                p=p->next;
            }
        }
    }
    else
        printf("该商品不存在!\n");
}


/*原有商品的新进货量*/
int purchase(struct goods **head)
{
    int flag=0,num;
    struct goods *p=*head,*p1=*head,*p2;
    p2=(struct goods *)malloc(sizeof(struct goods));
    p2->next=NULL;
    printf("请输入商品编号: \n");
    scanf("%s",p2->id);
    while(p)
    {
        if(strcmp(p2->id,p->id)==0)
        {
            flag=1;
            break;
        }
        p=p->next;
    }
    if(flag==1)
    {
        printf("请输入商品新进货量: \n");
        scanf("%d",&num);
        while(p1)
        {
            if(strcmp(p1->id,p2->id)==0)
            {
                p1->purchase_quantity+=num;
                p1->remaining_quantity+=num;
            }
            p1=p1->next;
        }
    }
    else
        printf("该商品不存在!");
}


/*原有商品的新销售量*/
int sales(struct goods **head)
{
    int flag=0,num;
    struct goods *p=*head,*p1=*head,*p2;
    p2=(struct goods *)malloc(sizeof(struct goods));
    p2->next=NULL;
    printf("请输入商品编号: \n");
    scanf("%s",p2->id);
    while(p)
    {
        if(strcmp(p2->id,p->id)==0)
        {
            flag=1;
            break;
        }
        p=p->next;
    }
    if(flag==1)
    {
        printf("请输入商品新销售量: \n");
        scanf("%d",&num);
        while(p1)
        {
            if(strcmp(p1->id,p2->id)==0)
            {
                p1->sales_volume+=num;
                p1->remaining_quantity-=num;
            }
            p1=p1->next;
        }
    }
    else
        printf("该商品不存在!\n");
}


/*原有商品的报废量*/
int baofei(struct goods **head)
{
    int flag=0,num;
    struct goods *p=*head,*p1=*head,*p2;
    p2=(struct goods *)malloc(sizeof(struct goods));
    p2->next=NULL;
    printf("请输入商品编号: \n");
    scanf("%s",p2->id);
    while(p)
    {
        if(strcmp(p2->id,p->id)==0)
        {
            flag=1;
            break;
        }
        p=p->next;
    }
    if(flag==1)
    {
        printf("请输入商品报废量: \n");
        scanf("%d",&num);
        while(p1)
        {
            if(strcmp(p1->id,p2->id)==0)
            {
                if(p1->remaining_quantity>=num)
                    p1->remaining_quantity-=num;
                else
                {
                    printf("数据不合理!\n");
                    break;
                }
            }
            p1=p1->next;
        }
    }
    else
        printf("该商品不存在!\n");
}


/*统计销售情况*/
int sale(struct goods **head)
{
    int flag=0;
    struct goods *p=*head,*p1=*head,*p2;
    p2=(struct goods *)malloc(sizeof(struct goods));
    p2->next=NULL;
    printf("请输入商品编号: \n");
    scanf("%s",p2->id);
    while(p)
    {
        if(strcmp(p2->id,p->id)==0)
        {
            flag=1;
            break;
        }
        p=p->next;
    }
    if(flag==1)
    {
        while(p1)
        {
            if(strcmp(p1->id,p2->id)==0)
            {
                printf("该商品的销售额为: %lf\n",p1->amount);
                printf("该商品的剩余数为: %d\n",p1->remaining_quantity);
                p->gross_profit=(p->amount)-(p->purchase_price)*(p->sales_volume);
                printf("该商品的毛利为: %lf\n",p1->gross_profit);
            }
            p1=p1->next;
        }
    }
    else
        printf("该商品不存在!\n");
}


/*保存商品信息*/
void baocun(struct goods **head)
{
    struct goods *p=*head;
    FILE *fp;
    fp=fopen("data.text","w");
    if(fp!=NULL)
    {
        if(p!=NULL)
        {
            while(p!=NULL)
            {
                fprintf(fp,"商品编号: %s",p->id);
                fprintf(fp,"商品品名: %s",p->name);
                fprintf(fp,"商品进价: %lf",p->purchase_price);
                fprintf(fp,"商品售价: %lf",p->selling_price);
                fprintf(fp,"商品进货量: %d",p->purchase_quantity);
                fprintf(fp,"商品销售量: %d",p->sales_volume);
                fprintf(fp,"商品销售额: %lf",p->amount);
                fprintf(fp,"商品剩余数: %d",p->remaining_quantity);
                fprintf(fp,"商品毛利: %lf",p->gross_profit);
                fprintf(fp,"\n");
                p=p->next;
            }
            printf("保存成功!");
        }
        else
            printf("暂无商品信息!\n");
    }
    else
        printf("保存失败!");
    printf("\n\n");
    fclose(fp);
}


/*根据商品名查询*/
void namechaxun(struct goods **head)
{
    struct goods *p=*head,*p1=*head;
    int flag=0;
    char name[MAXSIZE];
    printf("请输入要查询的商品品名: \n");
    scanf("%s",name);
    while(p)
    {
        if(strcmp(p->name,name)==0)
        {
            flag=1;
            break;
        }
        p=p->next;
    }
    if(flag==1)
    {
        while(p1)
        {
            if(strcmp(p1->name,name)==0)
            {
                printf("商品编号: %s\n",p1->id);
                printf("商品品名: %s\n",p1->name);
                printf("商品进价: %lf\n",p1->purchase_price);
                printf("商品售价: %lf\n",p1->selling_price);
                printf("商品进货量: %d\n",p1->purchase_quantity);
                printf("商品销售量: %d\n",p1->sales_volume);
                printf("商品销售额: %lf\n",p1->amount);
                printf("商品剩余数: %d\n",p1->remaining_quantity);
                printf("商品毛利: %lf\n",p1->gross_profit);
            }
            p1=p1->next;
        }
    }
    else
        printf("该商品不存在!\n");
}


/*根据剩余数查询*/
void shuchaxun(struct goods **head)
{
    struct goods *p=*head;
    int s,b;
    int flag=0;
    printf("请输入查询区间: \n");
    scanf("%d%d",&s,&b);
    if(s>b)
    {
        printf("输入错误,请重新输入!\n");
        return 0;
    }
    else
    {
        while(p)
        {
            if(p->remaining_quantity>=s&&p->remaining_quantity<=b)
            {
                flag=1;
                printf("商品编号: %s\n",p->id);
                printf("商品品名: %s\n",p->name);
                printf("商品进价: %lf\n",p->purchase_price);
                printf("商品售价: %lf\n",p->selling_price);
                printf("商品进货量: %d\n",p->purchase_quantity);
                printf("商品销售量: %d\n",p->sales_volume);
                printf("商品销售额: %lf\n",p->amount);
                printf("商品剩余数: %d\n",p->remaining_quantity);
                printf("商品毛利: %lf\n",p->gross_profit);
            }
            p=p->next;
        }
        if(flag==0)
            printf("未找到任何记录!\n");
    }
}


/*按原来商品顺序显示*/
void xianshi(struct goods **head)
{
    struct goods *p=*head;
    if(p!=NULL)
    {
        while(p!=NULL)
        {
            printf("商品编号: %s\n",p->id);
            printf("商品品名: %s\n",p->name);
            printf("商品进价: %lf\n",p->purchase_price);
            printf("商品售价: %lf\n",p->selling_price);
            printf("商品进货量: %d\n",p->purchase_quantity);
            printf("商品销售量: %d\n",p->sales_volume);
            printf("商品销售额: %lf\n",p->amount);
            printf("商品剩余数: %d\n",p->remaining_quantity);
            printf("商品毛利: %lf\n",p->gross_profit);
            p=p->next;
        }
    }
    else
        printf("暂无商品信息!\n");
}


/*按销售额由高到低的顺序显示*/
void amount(struct goods **head)
{
    int i,count=0,num;
    struct goods *p,*q,*tail,*p1=*head,*p2;
    p=*head;
    p2=(struct goods *)malloc(sizeof(struct goods));
    p2->next=p;
    while(p!=NULL)
    {
        count++;
        p=p->next;
    }
    for(i=0;i<count-1;i++)
    {
        num=count-i-1;
        q=p2->next;
        p=q->next;
        tail=p2;
        while(num--)
        {
            if(q->amount<p->amount)
            {
                q->next=p->next;
                p->next=q;
                tail->next=p;
            }
            tail=tail->next;
            q=tail->next;
            p=q->next;
        }
    }
    *head=p2->next;
    p=p2->next;
    if(p!=NULL)
    {
        while(p!=NULL)
        {
            printf("商品编号: %s\n",p->id);
            printf("商品品名: %s\n",p->name);
            printf("商品进价: %lf\n",p->purchase_price);
            printf("商品售价: %lf\n",p->selling_price);
            printf("商品进货量: %d\n",p->purchase_quantity);
            printf("商品销售量: %d\n",p->sales_volume);
            printf("商品销售额: %lf\n",p->amount);
            printf("商品剩余数: %d\n",p->remaining_quantity);
            printf("商品毛利: %lf\n",p->gross_profit);
            p=p->next;
        }
    }
    else
        printf("暂无商品信息!\n");
}


/*按毛利由高到低的顺序显示*/
void gross_profit(struct goods **head)
{
    int i,count=0,num;
    struct goods *p,*q,*tail,*p1=*head,*p2;
    p=*head;
    p2=(struct goods *)malloc(sizeof(struct goods));
    p2->next=p;
    while(p!=NULL)
    {
        count++;
        p=p->next;
    }
    for(i=0;i<count-1;i++)
    {
        num=count-i-1;
        q=p2->next;
        p=q->next;
        tail=p2;
        while(num--)
        {
            if(q->gross_profit<p->gross_profit)
            {
                q->next=p->next;
                p->next=q;
                tail->next=p;
            }
            tail=tail->next;
            q=tail->next;
            p=q->next;
        }
    }
    *head=p2->next;
    p=p2->next;
    if(p!=NULL)
    {
        while(p!=NULL)
        {
            printf("商品编号: %s\n",p->id);
            printf("商品品名: %s\n",p->name);
            printf("商品进价: %lf\n",p->purchase_price);
            printf("商品售价: %lf\n",p->selling_price);
            printf("商品进货量: %d\n",p->purchase_quantity);
            printf("商品销售量: %d\n",p->sales_volume);
            printf("商品销售额: %lf\n",p->amount);
            printf("商品剩余数: %d\n",p->remaining_quantity);
            printf("商品毛利: %lf\n",p->gross_profit);
            p=p->next;
        }
    }
    else
        printf("暂无商品信息!\n");
}


/*添加新商品*/
void add(struct goods **head)
{
    struct goods *p,*tail=NULL;
    *head=NULL;
    int i,num;
    printf("请输入新添加的商品个数: \n");
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
        p=(struct goods *)malloc(sizeof(struct goods));
        p->next=NULL;
        printf("请输入商品编号: \n");
        scanf("%s",p->id);
        printf("请输入商品品名: \n");
        scanf("%s",p->name);
        printf("请输入商品进价: \n");
        scanf("%lf",&p->purchase_price);
        printf("请输入商品售价: \n");
        scanf("%lf",&p->selling_price);
        printf("请输入商品进货量: \n");
        scanf("%d",&p->purchase_quantity);
        printf("请输入商品销售量: \n");
        scanf("%d",&p->sales_volume);
        p->amount=(p->sales_volume)*(p->selling_price);
        printf("该商品销售额为: %lf\n",p->amount);
        p->gross_profit=(p->amount)-(p->purchase_price)*(p->sales_volume);
        printf("该商品毛利为: %lf\n",p->gross_profit);
        p->remaining_quantity=(p->purchase_quantity)-(p->sales_volume);
        printf("该商品剩余数为: %d\n",p->remaining_quantity);
        if(*head==NULL)
            *head=p;
        else
            tail->next=p;
        tail=p;
    }
}

```

  • 写回答

1条回答 默认 最新

  • AI迅剑 2023-04-06 20:22
    关注

    看我的主页,我写了这个问题的代码

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月7日
  • 已采纳回答 4月7日
  • 创建了问题 4月6日

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗