商品管理系统

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
int main()
{


    welcome();
    menu();
    system("cls");
    return 0;
}
struct produce
{
    int id;
    char name[20];
    char spece[20];
    int count;
    float price;
};
void welcome()
{
    printf(" _________________   ____ ______________\n");
    printf("/   _____/\\______ \\ |    |   \\__    ___/\n") ;
    printf("\\_____  \\  |    |  \\|    |   / |    | \n");
    printf("/        \\ |    `   \\    |  /  |    |  \n");
    printf("/_______  //_______  /______/   |____|  \n");
    printf("        \\/         \\/ \n");
    printf("*****************************************\n");
    printf("*               欢迎使用                *\n");
    printf("*             库存管理系统              *\n");
    printf("*             按任意键继续              *\n");
    printf("*****************************************\n");
    system("pause");
    system("cls");
}
void menu()
{
    system("cls");
    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("*****9【清空屏幕信息】******\n");
    printf("****请选择您要选择的菜单****\n");
    int key;
    while(scanf("%d",&key)!=EOF)
    {
        getchar();//输入回车
        if(key==1)
        {
            fileinit();
        }
        else if(key==2)
        {
            enter();
        }
        else if(key==3)
        {
            delete();
        }
        else if(key==4)
        {
            update();
        }
        else if(key==5)
        {
            find();
        }
        else if(key==6)
        {
            checkall();
        }
        else if(key==7)
        {
            color();
        }
        else if(key==8)
        {
            checkout();
        }
        else if(key==9)
            clear();
        system("cls");
        menu();
    }
    system("cls");
}
void delproduce(int id)
{
    system("cls");
    FILE *file;
    file=fopen("produce.data","r");
    struct produce arrey[100],p;
    int j=0,size,i;
    while(!feof(file)&&(size=fread(&p,sizeof(struct produce),1,file))==1)
    {
        if(p.id!=id)
        {
            arrey[j++]=p;
        }
    }
    fclose(file);
    file=fopen("produce.data","w");
    fclose(file);
    file=fopen("produce.data","a");
    for(i=0; i<j; i++)
        fwrite(&arrey[i],sizeof(struct produce),1,file);
    fclose(file);
    printf("******************************************\n");
    printf("*******商品删除成功,是否继续删除?*******\n");
    printf("*******按(Y)y继续删除 按(N)n回到主菜单****\n");
    printf("******************************************\n");
    char key;
    scanf("%c",&key);
    getchar();
    if(key=='y'||key=='Y')
    {
        delete();
    }
    else if(key=='N'||key=='n')
    {
        menu();
    }
    system("cls");
}
void delete()
{

    system("cls");
    struct produce p;
    int size,ok=0,id;
    printf("******************************************\n");
    printf("***********请输入待删除商品的ID***********\n");
    printf("******************************************\n");
    scanf("%d",&id);
    getchar();
    FILE *file;
    file=fopen("produce.data","r");
    while(!feof(file)&&(size=fread(&p,sizeof(struct produce),1,file))==1)
    {
        if(p.id==id)
        {
            ok=1;
            break;
        }
    }
    fclose(file);
    if(ok==1)
    {
        printf("*******************************************\n");
        printf("*******请问您确定要删除下列商品吗?********\n");
        printf("|ID      |名称     |种类     |数量     |价格\n");
        printf("|%d      |%s       |%s       |%d       |%f\n",p.id,p.name,p.spece,p.count,p.price);
        printf("*******************************************\n");
        printf("*******按(Y)y继续删除 按(N)n回到主菜单*****\n");
        char key;
        scanf("%c",&key);
        getchar();
        if(key=='y'||key=='Y')
        {
            delproduce(id);
            system("cls");
        }
    }
    else
    {
        printf("*******************************************\n");
        printf("**************未找到该商品*****************\n");
        printf("*******按(Y)y继续删除 按(N)n回到主菜单*****\n");
        char key;
        scanf("%c",&key);
        getchar();
        if(key=='y'||key=='Y')
        {
            delete();

        }
        else if(key=='N'||key=='n')
        {
            menu();
        }
        system("cls");
    }
}
void checkall()
{
    system("cls");
    FILE *file;
    file=fopen("produce.data","r");
    struct produce p;
    int size;
    printf("ID      |名称     |种类     |数量     |价格\n");
    while(!feof(file)&&(size=fread(&p,sizeof(struct produce),1,file))==1)
    {
        printf("%d       %s         %s         %d        %f\n",p.id,p.name,p.spece,p.count,p.price);
    }
    printf("按回车键返回主菜单");
    getchar();
    fclose(file);
    system("cls");
}
void updateproduce(int id)
{
    system("cls");
    FILE *file;
    file=fopen("produce.data","r");
    struct produce arrey[100],p,p1;
    int j=0,size,i;
    while(!feof(file)&&(size=fread(&p,sizeof(struct produce),1,file))==1)
    {
        if(p.id!=id)
        {
            arrey[j++]=p;
        }
        else if(p.id==id)
        {
            p1.id=p.id;
            printf("请输入name\n");
            scanf("%s",p1.name);
            printf("请输入种类\n");
            scanf("%s",p1.spece);
            printf("请输入数量\n");
            scanf("%d",&p1.count);
            printf("请输入价格\n");
            scanf("%f",&p1.price);
            getchar();
            arrey[j++]=p1;
        }
    }
    fclose(file);
    file=fopen("produce.data","w");
    fclose(file);
    file=fopen("produce.data","a");
    for(i=0; i<j; i++)
        fwrite(&arrey[i],sizeof(struct produce),1,file);
    fclose(file);
    printf("******************************************\n");
    printf("*******商品修改成功,是否继续修改?*******\n");
    printf("*******按(Y)y继续修改 按(N)n回到主菜单****\n");
    printf("******************************************\n");
    char key;
    scanf("%c",&key);
    getchar();
    if(key=='y'||key=='Y')
    {
        update();
    }
    else if(key=='N'||key=='n')
    {
        menu();
    }
    system("cls");
}
void update()
{
    system("cls");
    int size,id,ok=0;
    struct produce p;
    printf("请输入待修改的商品ID\n");
    scanf("%d",&id);
    getchar();
    FILE *file;
    file=fopen("produce.data","r");
    while(!feof(file)&&(size=fread(&p,sizeof(struct produce),1,file))==1)
    {
        if(p.id==id)
        {
            ok=1;
            break;
        }
    }
    fclose(file);
    if(ok==1)
    {
        printf("*******************************************\n");
        printf("*******请问您确定要修改下列商品吗?********\n");
        printf("|ID      |名称     |种类     |数量     |价格\n");
        printf("|%d      |%s       |%s       |%d       |%f\n",p.id,p.name,p.spece,p.count,p.price);
        printf("*******************************************\n");
        printf("*******按(Y)y继续修改 按(N)n回到主菜单*****\n");
        char key;
        scanf("%c",&key);
        getchar();
        if(key=='y'||key=='Y')
        {
            updateproduce(id);
            system("cls");
        }
    }
    else
    {
        fclose(file);
        printf("*******************************************\n");
        printf("**************未找到该商品*****************\n");
        printf("*******按(Y)y继续修改 按(N)n回到主菜单*****\n");
        char key;
        scanf("%c",&key);
        getchar();
        if(key=='y'||key=='Y')
        {
            update();
        }
        else if(key=='N'||key=='n')
        {
            menu();
        }
    }
    system("cls");
}
void find()
{
    system("cls");
    FILE *file;
    file=fopen("produce.data","r");
    struct produce p;
    int size,id,ok=0;
    printf("******************************************\n");
    printf("*************请输入待查询商品ID***********\n");
    printf("******************************************\n");
    scanf("%d",&id);
    getchar();
    while(!feof(file)&&(size=fread(&p,sizeof(struct produce),1,file))==1)
    {
        if(p.id==id)
        {
            ok=1;
            printf("|ID      |名称     |种类     |数量     |价格\n");
            printf("%d       %s         %s         %d        %f\n",p.id,p.name,p.spece,p.count,p.price);
            break;
        }
    }
    if(ok==0)
        printf("未找到商品!请检查商品ID或名字是否有误!\n");
    fclose(file);
    printf("******************************************\n");
    printf("*******商品查询结束,是否继续查询?*******\n");
    printf("*******按(Y)y继续查询 按(N)n回到主菜单****\n");
    printf("******************************************\n");
    char key;
    scanf("%c",&key);
    getchar();
    if(key=='y'||key=='Y')
    {
        find();
    }
    else if(key=='N'||key=='n')
    {
        menu();
    }
    system("cls");
}
void color()
{
    system("cls");
    int a;
    printf("1:蓝底白字 2:黑底白字 3:绿底白字  4:灰底白字  5:淡蓝底白字\n");
    printf("6:白底黑字 7:蓝底黑字  8:亮白底黑字 9:绿底黑字 10:紫底黑字\n");
    scanf("%d",&a);
    if(a==1)
        system("color 17");
    if(a==2)
        system("color 07");
    if(a==3)
        system("color 27");
    if(a==4)
        system("color 87");
    if(a==5)
        system("color 97");
    if(a==6)
        system("color 70");
    if(a==7)
        system("color 10");
    if(a==8)
        system("color F0");
    if(a==9)
        system("color 20");
    if(a==10)
        system("color 50");
    system("cls");
}
void fileinit()
{
    system("cls");
    FILE *file;
    file=fopen("produce.data","w");
    if(file==NULL)
    {
        //创建文件失败
        printf("*******************\n");
        printf("**                *\n");
        printf("** 文件初始化失败 *\n");
        printf("**                *\n");
        printf("*******************\n");
        exit(0);
    }
    printf("*******************\n");
    printf("**                *\n");
    printf("** 文件初始化成功 *\n");
    printf("**                *\n");
    printf("*******************\n");
    system("pause");
    fclose(file);
    system("cls");
}
void enter()
{
    system("cls");
    struct produce p;
    system("cls");
    printf("请输入ID\n");
    scanf("%d",&p.id);
    printf("请输入name\n");
    scanf("%s",p.name);
    printf("请输入种类\n");
    scanf("%s",p.spece);
    printf("请输入数量\n");
    scanf("%d",&p.count);
    printf("请输入价格\n");
    scanf("%f",&p.price);
    getchar();
    FILE *file;
    file=fopen("produce.data","a");
    fwrite(&p,sizeof(struct produce),1,file);
    printf("ID:%d-name:%s-种类:%s-数量:%d-价格:%f\n",p.id,p.name,p.spece,p.count,p.price);
    fclose(file);
    printf("***************************************\n");
    printf("********商品入库成功,是否继续?*******\n");
    printf("*****按(Y)y继续添加 按(N)n回到主菜单***\n");
    printf("***************************************\n");
    char key;
    scanf("%c",&key);
    getchar();
    if(key=='y'||key=='Y')
    {
        enter();
    }
    else if(key=='N'||key=='n')
    {
        menu();
    }
    system("cls");
}
void checkout()
{
    system("cls");
    printf("***************************************\n");
    printf("********请问您确定退出本系统吗?*******\n");
    printf("*****按(Y)y安全退出 按(N)n回到主菜单***\n");
    printf("***************************************\n");
    char key;
    scanf("%c",&key);
    getchar();
    if(key=='y'||key=='Y')
    {
        printf("***************************************\n");
        printf("***************************************\n");
        printf("********已安全退出 祝您生活愉快********\n");
        printf("***************************************\n");
        exit(0);
    }
    else if(key=='N'||key=='n')
    {
        menu();
    }
    system("cls");
}
void clear()
{
    system("cls");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下雨天518

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值