栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于C语言的简易超市管理系统

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于C语言的简易超市管理系统

#include
#include
int menu();
void JianliKucun();
void XianshiKucun();
void xianshi(struct node *head);
struct node *add();
void calculate(struct node *head);
struct mar //该结构体为存储货物信息
{
    int ID;         //货号
    char brand[20]; //品名
    double price;   //售价
};
struct node //该结构体为存储购物车中的信息
{
    struct mar buy;    //货物信息
    int number;        //购物的数量
    struct node *next; //指向下一个购物节点
};
struct node *head = NULL;
struct mar goods[5]; //结构体数组
int main()
{
    printf("*********************************************************n");
    printf("                欢迎进入超市信息管理系统n");
    printf("*********************************************************n");
    int find = 0;
    while (1)
    {
        switch (menu())
        { // menu()函数为输入选项

        case 1:
            JianliKucun();
            break; //选项1 手动建立库存信息

        case 2:
            XianshiKucun();
            break; //选项2 显示库存信息函数

        case 3:
            xianshi(head);
            break; //选项3 显示购物车
        case 4:
            head = add();
            break; // 添加货品进入购物车
        case 5:
            calculate(head);
            break; //选项4 计算所购商品价格并修改保存

        case 6:
            system("cls");
            printf("感谢您的使用,再见!n");
            exit(0);
        }
    }
    return 0;
}
void calculate(struct node *head)
{
    double sum;
    xianshi(head);
    struct node *ptr;
    for (ptr = head; ptr != NULL; ptr = ptr->next)
    {
        sum += ptr->buy.price;
    }
    printf("货物的总价为%lf", sum);
}
void JianliKucun()
{
    int i = 0;
    printf("请依次输入货号、品名、售价: (输入-1以结束)");
    while (1)
    {
        scanf("%d",&goods[i].ID);
        if (goods[i].ID == -1)
        {
            break;
        }
        scanf("%s%lf",  goods[i].brand, &goods[i].price);
        i++;
    }
}
void XianshiKucun()
{
    printf("货号    品名    售价nnnn");
    for (int i = 0; i < 3; i++)
    {
        printf("%d   %s   %.2fnnn", goods[i].ID, goods[i].brand, goods[i].price);
    }
}

struct node *add()
{
    int x;
    struct node *head = NULL, *tail = NULL, *p;
    printf("请输入要添加商品的货号:(输入-1以结束)n");
    scanf("%d", &x);
    for (int i = 0; i < 3; i++)
    {
        if (x == -1)
        {    
            break;
         } 
        if (x == goods[i].ID)
        {
            p = (struct node *)malloc(sizeof(struct node));
            p->buy = goods[i];
            printf("添加成功");
            p->next = NULL;
            if (head == NULL)
            {
                head = p;
            }
            else
            {
                tail->next = p;
            }
            tail = p;
            scanf("%d", &x);
        }
    }
    return head;
}
void xianshi(struct node *head)
{
    struct node *ptr;
    if (head == NULL)
    {
        printf("尚未添加货品");
    }
    else
    {
        printf("货号    品名    售价nnnn");
        for (ptr = head; ptr != NULL; ptr = ptr->next)
        {
            printf("%d    %s    %.2fnnn", ptr->buy.ID, ptr->buy.brand, ptr->buy.price);
        }
    }
}
int menu()
{          //打印主菜单函数
    int n; //选项为整数
    while (1)
    {
        printf("nn请选择下面的数字进行操作:n");
        printf("--------------------------------------------n");
        printf("1. 手动建立库存信息n");
        printf("2. 显示所有商品信息n");
        printf("3. 显示购物车中的所有商品n");
        printf("4. 添加货品进入购物车n");
        printf("5. 结算n");
        printf("6. 退出程序n");
        printf("--------------------------------------------n");
        printf("请选择对应数字1~6n");
        scanf("%d", &n);
        if (n >= 1 && n <= 6)
        {
            return n; //返回输入的选项
        }
        else
        {
            printf("输入数字有误,请重新输入");
            return 0;
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965587.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号