使用内核链表实现简单的超市管理系统。

代码实现:有bug,能基本实现功能,需要先存入商品,在执行其他操作。有部分注释,本人初学者,有错误请指教。

/*超市管理系统*/

/*
管理员登录密码:123456
资金管理登录密码:654321
基本能实现:
商品的添加;商品进价售价数量的修改;
满足客户的多次购物;满足客户对商品信息的查看
满足管理员对金额管理,显示盈利状况。
*/

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

static int numberr = 0;
//商品信息结构体
struct goods
{
    int date;
    int number;    //商品编号
    char goodsname[50];    //商品名称
    int stock;    //库存量
    int stocknum;    //进货量
    double purchase;    //进价
    double prace;    //售价
    double money;//进价总金额
    struct list_head slist;    //内核链表使用
};

//函数声明
struct goods *new_node(int date,char *goodsname,int stock,double purchase,double prace);
struct goods *find_node(struct goods *head,int data);
void show_goods(struct goods *head);
void add(struct goods *head);
void revise(struct goods *head);
void cart(struct goods *head);
void shopping(struct goods *head);
struct goods *money(struct goods *head);
void display_fund(struct goods *head);
void display_gain(struct goods *head);
void adm();

//建立新节点,并传入参数(增加新商品,传入商品参数)
struct goods *new_node(int date,char *goodsname,int stock,double purchase,double prace)
{
    numberr++;
    struct goods *new = NULL;

    new = malloc(sizeof(struct goods));
    memset(new, 0, sizeof(struct goods));

    new->date = date;
    new->number = numberr;
    strcpy(new->goodsname,goodsname);
    new->stock = stock;
    new->stocknum = new->stock;
    new->purchase = purchase;
    new->prace = prace;
    new->money = stock*purchase;

扫描二维码关注公众号,回复: 2557961 查看本文章

    if(new)
    {
        INIT_LIST_HEAD(&new->slist);
    }

    return new;
}

//找到新节点要插入的地方,并且插入。形成链表
struct goods *find_node(struct goods *head,int data)
{
    struct goods *pos = NULL;
    list_for_each_entry(pos, &head->slist,slist)
    {
        if(pos->number > data)
        {
            return pos;
        }
    }
    return pos;
}

//管理员界面的 商品信息全部显示
void show_goods(struct goods *head)
{
    struct goods *pos = head;

    if(list_empty(&pos->slist))
    {
        printf("库存中没有商品,请先进货。\n");
        printf("\n");
    }

    else
    {
        list_for_each_entry(pos,&head->slist,slist)
        {
            printf("进货日期:%d\n",pos->date);
            printf("编号:  %d\n",pos->number);
            printf("商品名称:  %s\n",pos->goodsname);
            printf("库存:  %d\n",pos->stock);
            printf("进价:  %.2lf元\n",pos->purchase);
            printf("售价:  %.2lf元\n",pos->prace);
            printf("\n");
        }
    }
    
}


/*管理员模式下 增加商品*/
void add(struct goods *head)
{
    int date;
    int n;
    char goodsname[50];
    int stock;
    double purchase;
    double prace;

    while(1)
    {
        
        printf("请输入进货日期:\n");
        scanf("%d",&date);
        printf("请输入商品名称:\n");
        scanf("%s",goodsname);
        printf("请输入商品进货数量:\n");
        scanf("%d",&stock);
        printf("请输入商品进价:\n");
        scanf("%lf",&purchase);
        printf("请输入商品售价:\n");
        scanf("%lf",&prace);

        struct goods *new = new_node(date,goodsname,stock,purchase,prace);

        struct goods *pos = find_node(head,numberr);

        list_add_tail(&new->slist,&pos->slist);


        printf("你好:管理员大大。\n你是确定继续新增(1):\n还是返回上一层呢(0):\n");
        scanf("%d",&n);

        if(n == 1)
        {
            continue;
        }

        break;
    }
}


//管理员模式下的 商品信息修改。
void revise(struct goods *head)
{
    int number = 0;
    int n = 0;
    int sto ;
    double purc;
    double pra;

    printf("请输入你需要修改的商品的编号:\n");
    scanf("%d",&number);

    printf("请选择你所需要修改的商品信息:\n库存信息(0)\n进价信息(1)\n售价信息(2)\n");
    scanf("%d",&n);

int flag = 0;

    struct goods *pos = head;
    list_for_each_entry(pos, &head->slist,slist)
    {
        if(pos->number = number)
        {
        
            switch(n)
            {
                case 0:
                printf("请输入新的库存量:\n");
                scanf("%d",&sto);
                pos->stock = sto;

                flag = 1;
                break;
        
                case 1:
                printf("请输入新的进价:\n");
                scanf("%lf",&purc);
                pos->purchase = purc;

                flag = 1;
                break;
        
                case 2:
                printf("请输入新的售价:\n");
                scanf("%lf",&pra);
                pos->prace = pra;

                flag = 1;
                break;
            }
        }

        if(flag)
            break;

    }
}


//顾客查看商品。
void cart(struct goods *head)
{
    printf("===========================你好,欢迎进入购物车================================\n");
    printf("\n");

    int choice;
    struct goods *pos = head;

    if(list_empty(&pos->slist))
    {
        printf("库存中没有任何商品,请联系管理员进货。\n");
        printf("\n");
    }

    else
    {
        printf("你好,超市中拥有以下商品:\n");
        list_for_each_entry(pos,&head->slist,slist)
        {
            if(pos->stock == 0)
            {
                printf("%s 已经售罄。\n",pos->goodsname);
            }
            else
            {
                printf("编号为(%d)\n商品名称:%s:剩余 %d 件  单价%0.2lf元\n\n",pos->number,pos->goodsname,pos->stock,pos->prace);
            }
    
        }
    
        while(1)
        {
            printf("你好:请问你是需要继续购物(1),退出(0):\n");
            scanf("%d",&choice);
    
            if(choice == 1)
            {
                shopping(head);
            }
            break;
        }
    }
    
}


//顾客进行购物
void shopping(struct goods *head)
{
    double mone = 0;

    while(1)
    {

        int num = 0;
        double tem = 0;
    
        printf("请输入商品编号:\n");
        scanf("%d",&num);
    
        struct goods *pos = head;
    
        list_for_each_entry(pos,&head->slist,slist)
        {
            if(pos->number == num)
                {
                    int amount = 0;
    
                    printf("请输入你所需要购买的数量:\n");
                    scanf("%d",&amount);
    
                    if(pos->stock >= amount)
                    {    
                        
                        tem = amount * (pos->prace) ;

                        pos->stock = pos->stock - amount;
                    }
    
                    else
                    {
                        printf("抱歉,你所需要的商品数量不足。\n");
                    }

                }

        }

        printf("你本次购买商品花费 %0.2lf 元。\n",tem);

        mone = mone + tem;

        int choice;

        printf("请问你是需要继续购买(1)    还是买完离开呢?(0)\n");
        scanf("%d",&choice);

        if(choice == 0)
        {
            printf("你本次总共花费%0.2lf元\n",mone);
            break;
        }
        
    }

}


//金额管理系统,显示所有金额信息    /*在顾客每次购买东西后盈利金额计算*/
struct goods *money(struct goods *head)
{
    int password = 654321;
    int sign;

    printf("请输入密码以进入资金管理系统:\n");
    scanf("%d",&sign);

    if( password == sign )
    {
        while(1)
        {
            int choice;

            printf("请选择你需要进入的部分:剩余本金显示(1)盈利显示(2)退出(0)\n");
            scanf("%d",&choice);
    
            switch(choice)
            {
                case 1:
                display_fund(head);break;
    
                case 2:
                display_gain(head);break;

                default:
                break;
            }

            while( choice == 0);
            break;
        }
        
    }

}

void display_fund(struct goods *head)
{
    double corpus = 0;
    double fund = 0;
    double total = 0;
    struct goods *pos = head;

    printf("请输入你的资金总额:\n");
    scanf("%lf",&corpus);

    list_for_each_entry(pos,&head->slist,slist)
    {
        fund = (pos->stocknum)*(pos->purchase);

        total += fund;
    }

    printf("你进货一共花费:%0.2lf元\n",total);
    printf("你剩余本金为:%0.2lf元\n",corpus - total);


}

void display_gain(struct goods *head)
{
    double fund = 0;

    struct goods *pos = head;

    list_for_each_entry(pos,&head->slist,slist)
    {
        double univalent = 0;
        double totap = 0;

        if((pos->stocknum) > (pos->stock))
        {
            univalent = (pos->prace) - (pos->purchase);
            totap = univalent * ((pos->stocknum) - (pos->stock));

        }
        printf("该商品总共盈利%0.2lf元\n",totap);

        fund += totap;
    }

    printf("当前商品总盈利为%0.2lf元。\n",fund);

}


//选择用户身份:顾客或者管理员
void adm()
{
    int choice;
    int n;
    int cipher = 123456;
    int password;


    struct goods *head = NULL;
    head = malloc(sizeof(struct goods));

    if(head)
        {
            INIT_LIST_HEAD(&head->slist);
        }

    while(1)
    {
        printf("你好:请问你是需要顾客服务 1 :\n管理界面 0 :\n");
        scanf("%d",&n);

        if(n == 0)
        {
            printf("请输入超市管理系统密码:\n");
            scanf("%d",&password);

            if(cipher == password)
            {
                printf("============================你好:管理员大大=============================\n");

                printf("请问你是需要那些服务:\n(0)查看商品全部信息:\n(1)增加商品:\n(2)修改商品信息:\n(3)金额管理:\n");
                scanf("%d",&choice);

                switch(choice)
                {
                    case 0:
                    show_goods(head);break;
    
                    case 1:
                    add(head);break;
    
                    case 2:
                    revise(head);break;
    
                    case 3:
                    money(head);break;
    
                    default:
                    break;
                }    
            }

            else
            {
                printf("你输入的密码有误,请重新输入。\n");
            }

        continue;
        }

        else
        {
            printf("\n");
            cart(head);
        }

    }

}


int main(void)
{
    adm();

    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/lovelijiapu/article/details/81319271
今日推荐