货物管理系统

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<process.h>
#include<time.h>
#define Null 0

struct goods*input(void);
struct goods*open(void);
void add(struct goods*head);
struct goods*delete(struct goods*head);
struct goods*amend(struct goods*head);
void print(struct goods*head);
void find(struct goods*head);
void statistics(struct goods*head);
void list1(struct goods*head);
struct goods*search(struct goods*head);
struct goods*incr1(struct goods*head);
void list2(struct goods*head);
struct goods*incr2(struct goods*head);
struct goods*sort1(struct goods*head);
struct goods*sort2(struct goods*head);
struct goods*sort3(struct goods*head);
struct goods*sort4(struct goods*head);

struct goods   /*定义货物数据结构体*/
{
    char number[20];               /*编号*/
    char name[20];            /*名称*/
    float Pprice;             /*进货价*/
    float Sprice;             /*销售价*/
    char ori[30];             /*产地*/
    struct
    {
        int year;
        int month;
        int day;

    }Pdate;                    /*生产日期*/
    int Slife;                /*保质期*/
    int tim;                  /*过期时间*/
    int inv;                  /*库存量*/
    int left;                 /*剩余量*/
    int Bsold;                /*已销售量*/
    float income;             /*收益*/
    struct goods*next;
};
int main(void)
{
    struct goods*head;
    char choose='',yes_no='';
    do{
        system("cls");
    printf("|--------------------------------|
");
    printf("|               欢迎来到商店货物管理系统                     |
");
    printf("|              请输入菜单选项编号(0~9)                   |
");
    printf("|--------------------------------|
");
    printf("|     A---输入货物信息                                         |
");
    printf("|     B---从文件读取货物信息                                   |
");
    printf("|     C---添加一条货物信息                                     |
");
    printf("|     D---删除一条货物信息                                     |
");
    printf("|     E---修改一条货物信息                                     |
");
    printf("|     F---显示所有货物信息                                     |
");
    printf("|     G---显示差10天过期的货物信息                             |
");
    printf("|     H---按照货物编号查找货物信息                             |
");
    printf("|     I---统计全仓库各种货物库存总数、已销售数量和剩余总数     |
");
    printf("|     J---增加货物促销信息                                     |
");
    printf("|     K---增加货物补货信息                                     |
");
    printf("|     L---按照货物编号排序                                     |
");
    printf("|     M---按照货物名称排序                                     |
");
    printf("|     N---按照货物剩余总数排序                                 |
");
    printf("|     P---按照保质期截至日前排序                               |
");
    printf("|     0---退出                                                 |
");
    printf("|--------------------------------|
");
    printf("                                                        
");
    choose=getche();
    printf("
");
    switch(choose)
    {
        case'A':{head=input();list1(head);break;}
        case'B':{head=open();list1(head);break;}
        case'C':{add(head);list1(head);break;}
        case'D':{head=delete(head);list1(head);break;}
        case'E':{head=amend(head);list1(head);break;}
        case'F':{print(head);break;}
        case'G':{head=search(head);list2(head);break;}
        case'H':{find(head);break;}
        case'I':{statistics(head);break;}
        case'J':{head=incr1(head);list2(head);break;}
        case'K':{head=incr2(head);list2(head);break;}
        case'L':{head=sort1(head);print(head);list2(head);break;}
        case'M':{head=sort2(head);print(head);list2(head);break;}
        case'N':{head=sort3(head);print(head);list2(head);break;}
        case'P':{head=sort4(head);print(head);list2(head);break;}
        case'0':{break;}
        default:printf("              %c为非法选项!
",choose);
    }
    if(choose=='0')break;
    printf("
                 要继续选择吗(Y/N)?
");
    do
    {
        yes_no=getch();
    }
    while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
   }
   while(yes_no=='Y'||yes_no=='y');

}
/*
函数功能:输入货物信息
函数入口参数:空
函数返回值:结构体指针
*/
struct goods*input(void)
{
    struct goods *head,*p,*s;
    int i,n;
    printf("请问有多少个货物信息需要输入?
");
    scanf("%d",&n);
    getchar();                 /*吸收输入n时的回车*/


    for(i=1;i<=n;i++)
    {
        if(i==1)
       {
           printf("==============请输入%d个货物信息===============
",i);
           p=(struct goods*)malloc(sizeof(struct goods));
           printf("编号:");
           scanf("%s",p->number);
           printf("名称:");
           scanf("%s",p->name);
           printf("进货价:");
           scanf("%f",&p->Pprice);
           printf("销售价:");
           scanf("%f",&p->Sprice);
           printf("产地:");
           scanf("%s",p->ori);
           printf("生产日期(年 月 日):");
           scanf("%d %d %d",&p->Pdate.year,&p->Pdate.month,&p->Pdate.day);
           printf("保质期(月):");
           scanf("%d",&p->Slife);
           printf("库存量:");
           scanf("%d",&p->inv);
           printf("已销售数量:");
           scanf("%d",&p->Bsold);
           p->income=(p->inv-p->Bsold)*(p->Sprice-p->Pprice);
           p->left=p->inv-p->Bsold;
           head=p;
       }
       else
       {
           printf("==============请输入%d个货物信息===============
",i);
           s=(struct goods*)malloc(sizeof(struct goods));
           printf("编号:");
           scanf("%s",s->number);
           printf("名称:");
           scanf("%s",s->name);
           printf("进货价:");
           scanf("%f",&s->Pprice);
           printf("销售价:");
           scanf("%f",&s->Sprice);
           printf("产地:");
           scanf("%s",s->ori);
           printf("生产日期(年 月 日):");
           scanf("%d %d %d",&s->Pdate.year,&s->Pdate.month,&s->Pdate.day);
           printf("保质期(月):");
           scanf("%d",&s->Slife);
           printf("库存量:");
           scanf("%d",&s->inv);
           printf("已销售数量:");
           scanf("%d",&s->Bsold);
           s->income=(s->inv-s->Bsold)*(s->Sprice-s->Pprice);
           s->left=s->inv-s->Bsold;
           p->next=s;
           p=s;
       }

    }
    s->next=Null;
    return head;
}
/*
函数功能:从文件读取货物信息
函数入口参数:空
函数返回值:结构体指针
*/
struct goods*open(void)
{
    struct goods *head,*p,*s;
    int i,n;

    printf("将信息按:'编号 名称 进货价 销售价 产地 生产日期 保质期库存量 已销售数量'输入
");
    printf("
");
    printf("请问有多少个货物信息需要输入?
");
    scanf("%d",&n);
    getchar();                 /*吸收输入n时的回车*/

    char ad[]="D:\goods.txt";
    FILE * fp;
    while((fp=fopen(ad,"r+"))==NULL)
    {
        printf("没有找到输入的文件请重新输入文件名及其所在地址:
");
        gets(ad);
    }

    for(i=1;i<=n;i++)
    {
        if(i==1)
       {
           p=(struct goods*)malloc(sizeof(struct goods));
           fscanf(fp,"%s %s %f %f %s %d %d %d %d %d %d",p->number,p->name,&p->Pprice,&p->Sprice,p->ori,&p->Pdate.year,&p->Pdate.month,&p->Pdate.day,&p->Slife,&p->inv,&p->Bsold);
           p->income=(p->inv-p->Bsold)*(p->Sprice-p->Pprice);
           p->left=p->inv-p->Bsold;
           head=p;
       }
       else
       {
           s=(struct goods*)malloc(sizeof(struct goods));
           fscanf(fp,"%s %s %f %f %s %d %d %d %d %d %d",s->number,s->name,&s->Pprice,&s->Sprice,s->ori,&s->Pdate.year,&s->Pdate.month,&s->Pdate.day,&s->Slife,&s->inv,&s->Bsold);
           s->income=(s->inv-s->Bsold)*(s->Sprice-s->Pprice);
           s->left=s->inv-s->Bsold;
           p->next=s;
           p=s;
       }
    }
    s->next=Null;
    fclose(fp);
    return head;
}
/*
函数功能:增加一条货物信息
函数入口参数:结构体指针
函数返回值:空
*/
void add(struct goods*head)
{
    struct goods*p,*pi,*pl;
    char num[20];
    printf("请问要在哪个货物后面添加记录(输入货物编号):
");
    scanf("%s",num);
    pi=(struct goods *)malloc(sizeof(struct goods));
    p=pl=head;
    while((strcmp(pl->number,num)!=0)&&(pl->next!=Null))
    {
        pl=pl->next;
    }
    if(strcmp(pl->number,num)==0)
    {
        printf("========请输入新货物的信息===========
");
        printf("编号:");
        scanf("%s",pi->number);
        printf("名称:");
        scanf("%s",pi->name);
        printf("进货价:");
        scanf("%f",&pi->Pprice);
        printf("销售价:");
        scanf("%f",&pi->Sprice);
        printf("产地:");
        scanf("%s",pi->ori);
        printf("生产日期(年 月 日):");
        scanf("%d %d %d",&pi->Pdate.year,&pi->Pdate.month,&pi->Pdate.day);
        printf("保质期(月):");
        scanf("%d",&pi->Slife);
        printf("库存量:");
        scanf("%d",&pi->inv);
        printf("已销售数量:");
        scanf("%d",&pi->Bsold);
        pi->income=(pi->inv-pi->Bsold)*(pi->Sprice-pi->Pprice);
        pi->left=pi->inv-pi->Bsold;
        if(head=Null)
        {
            head=pi;
            pi->next=Null;
        }
        else
        {
            while((strcmp(p->number,num)!=0)&&(p->next!=Null))
                  {
                      p=p->next;
                  }
           if(p->next!=Null)
           {
               pi->next=p->next;
               p->next=pi;
           }
           else
           {
               p->next=pi;
               pi->next=Null;
           }
        }
    }
    else
        printf("没有找到相应的货物信息记录,请重试!
");

}
/*
函数功能:删除一条货物信息
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*delete(struct goods*head)
{
    struct goods*p,*q;
    char num[20];
    p=head;

    printf("请输入要删除货物的编号:
");
    scanf("%s",num);
    if(head==Null)
    {
        printf("没有要删除的货物信息!
");
        return head;
    }
    while((strcmp(p->number,num)!=0)&&(p->next!=Null))
    {
        q=p;
        p=p->next;
    }
    if(strcmp(p->number,num)==0)
    {
        if(p==head)
            head=p->next;
        else
            q->next=p->next;
        free(p);
    }
    else
        printf("没有找到相应货物信息,请重试!
");
    return head;
}
/*
函数功能:修改一条货物信息
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*amend(struct goods*head)
{
    struct goods*p,*q;
    int flag=0;
    char num[20];
    p=head;

    printf("请输入要修改货物的编号:
");
    scanf("%s",num);
    if(head==Null)
    {
        printf("没有要修改的货物信息!
");
        return head;
    }
    while(p!=Null)
    {
        if(strcmp(p->number,num)==0)
        {
           flag=1;
           printf("============请输入新的货物信息==========
");
           printf("编号:");
           scanf("%s",p->number);
           printf("名称:");
           scanf("%s",p->name);
           printf("进货价:");
           scanf("%f",&p->Pprice);
           printf("销售价:");
           scanf("%f",&p->Sprice);
           printf("产地:");
           scanf("%s",p->ori);
           printf("生产日期(年 月 日):");
           scanf("%d %d %d",&p->Pdate.year,&p->Pdate.month,&p->Pdate.day);
           printf("保质期(月):");
           scanf("%d",&p->Slife);
           printf("库存量:");
           scanf("%d",&p->inv);
           printf("已销售数量:");
           scanf("%d",&p->Bsold);
          p->income=(p->inv-p->Bsold)*(p->Sprice-p->Pprice);
          p->left=p->inv-p->Bsold;
        }
        p=p->next;
    }
    if(flag==0)
        printf("没有找到要修改的货物信息,请重试!
");
    return head;
}
/*
函数功能:查看所有货物信息记录
函数入口参数:结构体指针
函数返回值:无
*/
void print(struct goods*head)
{
    struct goods*p;
    p=head;
    printf("==========================货物信息========================================
");
    printf("编号 名称 进货价 销售价 产地 生产日期 保质期 库存量 已销售数量 剩余量 收益
");
    while(p!=Null)
    {
        printf("%s %s %.2f %.2f %s %d %d %d %d %d %d %d %.2f
",p->number,p->name,p->Pprice,p->Sprice,p->ori,p->Pdate.year,p->Pdate.month,p->Pdate.day,p->Slife,p->inv,p->Bsold,p->left,p->income);
        p=p->next;
    }
}
/*
函数功能:按编号查找一条货物信息
函数入口参数:结构体指针
函数返回值:无
*/
void find(struct goods*head)
{
    struct goods*p;
    char num[20];
    int flag=0;
    p=head;

    printf("请输入要查找货物的编号:
");
    scanf("%s",num);
    while(p!=Null)
    {
        if(strcmp(p->number,num)==0)
        {
            flag=1;
            printf("==============================货物信息====================================
");
            printf("
");
            printf("编号 名称 进货价 销售价 产地 生产日期 保质期 库存量 已销售数量 剩余量 收益
");
            printf("%s %s %.2f %.2f %s %d %d %d %d %d %d %d %.2f",p->number,p->name,p->Pprice,p->Sprice,p->ori,p->Pdate.year,p->Pdate.month,p->Pdate.day,p->Slife,p->inv,p->Bsold,p->left,p->income);
        }
         p=p->next;
    }
    if(flag==0)
    printf("没有找到该编号的货物信息,请重试!
");
}

/*
函数功能:统计全仓库各种货物库存总数、已销售数量和剩余总数
函数入口参数:结构体指针
函数返回值:无
*/
void statistics(struct goods*head)
{
    struct goods*p;
    int sum1=0,sum2=0,sum3=0;
    p=head;
    printf("======全仓库各种货物库统计======
");
    printf("
");
    printf("库存总数  已销售数量  剩余总数
");
    while(p!=Null)
    {
        sum1=sum1+p->inv;
        sum2=sum2+p->Bsold;
        sum3=sum3+p->left;
        p=p->next;
    }
    printf("%d %d %d
",sum1,sum2,sum3);
}
/*
函数功能:显示差10天过期的货物信息
函数入口参数:结构体指针
函数返回值:无
*/
struct goods*search(struct goods*head)
{
    int flag=0;
    struct goods*p,*q;
    p=head,q=head;
    int y,m,d;
    struct tm *t;
    time_t tt;
    time(&tt);
    t=localtime(&tt);
    printf("当前时间为%d年%d月%d日
",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
    y=t->tm_year+1900;
    m=t->tm_mon+1;
    d=t->tm_mday;
    while(p!=Null)
    {
        p->tim=(p->Pdate.year*365+p->Pdate.month*30+p->Pdate.day+p->Slife*30)-(y*365+m*30+d);
        if(p->tim==10)
            flag=1;
        p=p->next;
    }
    if(flag==0)
        printf("货物中没有找到指定差10天过期的货物信息");
    else
    {
        printf("===============================货物信息===========================================
");
        printf("编号 名称 进货价 销售价 产地 生产日期 保质期 过期日期 库存量 已销售数量 剩余量 收益
");
        while(q!=Null)
        {
           if(q->tim==10)
           {
               printf("%s %s %.2f %.2f %s %d %d %d %d %d %d %d %d %.2f
",q->number,q->name,q->Pprice,q->Sprice,q->ori,q->Pdate.year,q->Pdate.month,q->Pdate.day,q->Slife,q->tim,q->inv,q->Bsold,q->left,q->income);
               }
        q=q->next;
        }
        }

    return head;

}
/*
函数功能:增加促销信息
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*incr1(struct goods*head)
{
    int flag=0;
    struct goods*p;
    p=head;
    while(p!=Null)
    {
        if(p->tim==10)
           {
               flag=1;
               p->Sprice=p->Sprice/2;
           }
        p=p->next;
    }
    if(flag==0)
        printf("没有找到指定差10天过期的物品,请重试!
");
    else
        printf("已将指定差10天过期的物品以半价出售!");
    return head;

};

/*
函数功能:增加补货信息
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*incr2(struct goods*head)
{

    int flag=0,add;
    struct goods*q;
    q=head;
    while(q!=Null)
    {
        if(q->left<100)
           {
                printf("======================货物信息=============================
");
                printf("编号 名称 进货价 销售价 产地 库存量 已销售数量 剩余量 收益
");
                printf("%s %s %.2f %.2f %s %d %d %d %.2f
",q->number,q->name,q->Pprice,q->Sprice,q->ori,q->inv,q->Bsold,q->left,q->income);
                printf("请输入该货物需要补货的量
");
                scanf("%d",&add);
                q->inv=q->inv+add;
                q->income=(q->inv-q->Bsold)*(q->Sprice-q->Pprice);
                q->left=q->inv-q->Bsold;
                flag=1;
                }
        q=q->next;
    }
    if(flag==0)
         printf("没有找到剩余量少于100的货物信息
");
    return head;

};
/*
函数功能:按照货物编号
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*sort1(struct goods*head)
{
    struct goods*p,*p1,*p2,*p3;
    struct goods h, t;
    if (head == Null)
        return Null;
    h.next=head;
    p=&h; //使用头结点,避免特殊情况的处理,因为代价太高
    while (p->next!=Null)
    {
        p=p->next;
    }
    p=p->next=&t; //!!!链表的尾节点可能被交换到链表中间去,这里使用了一个固定的节点
    while (p!=h.next)
    {
        p3=&h;
        p1=p3->next;
        p2=p1->next;
        while (p2!=p) //p指向的是已就序的节点
        {
            if (strcmp(p1->number,p2->number)>0)
            {//避免在双层循环内进行特殊情况的判断,因为这里的时间复杂度是O(n^2)
                p1->next=p2->next;
                p2->next=p1;
                p3->next=p2;

                p3=p2; //由于加了表头和表尾,整个交换过程更简洁
                p2=p1->next;

            } else {
                p3=p1;
                p1=p2;
                p2=p2->next;
            }
        }
        p=p1;
    }
    while (p->next!=&t)
    {
        p=p->next;  //虽然这里复杂度是O(n),但是远比在双层循环内的特殊处理要高效的多
    }
    p->next=Null;
    return h.next;

};
/*
函数功能:按照货物名称
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*sort2(struct goods*head)
{
    struct goods*p,*p1,*p2,*p3;
    struct goods h, t;
    if (head == Null)
        return Null;
    h.next=head;
    p=&h; //使用头结点,避免特殊情况的处理,因为代价太高
    while (p->next!=Null)
    {
        p=p->next;
    }
    p=p->next=&t; //!!!链表的尾节点可能被交换到链表中间去,这里使用了一个固定的节点
    while (p!=h.next)
    {
        p3=&h;
        p1=p3->next;
        p2=p1->next;
        while (p2!=p) //p指向的是已就序的节点
        {
            if (strcmp(p1->name,p2->name)>0)
            {//避免在双层循环内进行特殊情况的判断,因为这里的时间复杂度是O(n^2)
                p1->next=p2->next;
                p2->next=p1;
                p3->next=p2;

                p3=p2; //由于加了表头和表尾,整个交换过程更简洁
                p2=p1->next;

            } else {
                p3=p1;
                p1=p2;
                p2=p2->next;
            }
        }
        p=p1;
    }
    while (p->next!=&t)
    {
        p=p->next;  //虽然这里复杂度是O(n),但是远比在双层循环内的特殊处理要高效的多
    }
    p->next=Null;
    return h.next;

};
/*
函数功能:按照货物剩余总数
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*sort3(struct goods*head)
{
    struct goods*p,*p1,*p2,*p3;
    struct goods h, t;
    if (head == Null)
        return Null;
    h.next=head;
    p=&h; //使用头结点,避免特殊情况的处理,因为代价太高
    while (p->next!=Null)
    {
        p=p->next;
    }
    p=p->next=&t; //!!!链表的尾节点可能被交换到链表中间去,这里使用了一个固定的节点
    while (p!=h.next)
    {
        p3=&h;
        p1=p3->next;
        p2=p1->next;
        while (p2!=p) //p指向的是已就序的节点
        {
            if ((p1->left)>(p2->left))
            {//避免在双层循环内进行特殊情况的判断,因为这里的时间复杂度是O(n^2)
                p1->next=p2->next;
                p2->next=p1;
                p3->next=p2;

                p3=p2; //由于加了表头和表尾,整个交换过程更简洁
                p2=p1->next;

            } else {
                p3=p1;
                p1=p2;
                p2=p2->next;
            }
        }
        p=p1;
    }
    while (p->next!=&t)
    {
        p=p->next;  //虽然这里复杂度是O(n),但是远比在双层循环内的特殊处理要高效的多
    }
    p->next=Null;
    return h.next;

};
/*
函数功能:按照保质期截至日前排序
函数入口参数:结构体指针
函数返回值:结构体指针
*/
struct goods*sort4(struct goods*head)
{
    struct goods*p,*p1,*p2,*p3;
    struct goods h, t;
    if (head == Null)
        return Null;
    h.next=head;
    p=&h; //使用头结点,避免特殊情况的处理,因为代价太高
    while (p->next!=Null)
    {
        p=p->next;
    }
    p=p->next=&t; //!!!链表的尾节点可能被交换到链表中间去,这里使用了一个固定的节点
    while (p!=h.next)
    {
        p3=&h;
        p1=p3->next;
        p2=p1->next;
        while (p2!=p) //p指向的是已就序的节点
        {
            if ((p1->tim)>(p2->tim))
            {//避免在双层循环内进行特殊情况的判断,因为这里的时间复杂度是O(n^2)
                p1->next=p2->next;
                p2->next=p1;
                p3->next=p2;

                p3=p2; //由于加了表头和表尾,整个交换过程更简洁
                p2=p1->next;

            } else {
                p3=p1;
                p1=p2;
                p2=p2->next;
            }
        }
        p=p1;
    }
    while (p->next!=&t)
    {
        p=p->next;  //虽然这里复杂度是O(n),但是远比在双层循环内的特殊处理要高效的多
    }
    p->next=Null;
    return h.next;
};


/*
函数功能:将数据从缓冲区存储到硬盘文件中
函数入口参数:结构体指针
函数返回值:无
*/
void list1(struct goods*head)
{
    char ad[]="D:\goods.txt";;
    FILE * fp;
    struct goods*p;

    p=head;
    fp=fopen(ad,"w+");
    fprintf(fp,"==============================货物信息=====================================
");
    fprintf(fp,"编号 名称 进货价 销售价 产地 生产日期 保质期 库存量 已销售数量 剩余量 收益
");
    while(p!=Null)
    {
        fprintf(fp,"%s %s %.2f %.2f %s %d %d %d %d %d %d %d %.2f
",p->number,p->name,p->Pprice,p->Sprice,p->ori,p->Pdate.year,p->Pdate.month,p->Pdate.day,p->Slife,p->inv,p->Bsold,p->left,p->income);
        p=p->next;
    }
    fclose(fp);
}
/*
函数功能:将数据从缓冲区存储到硬盘文件中
函数入口参数:结构体指针
函数返回值:无
*/
void list2(struct goods*head)
{
    char ad[]="D:\goods.txt";
    FILE * fp;
    struct goods*p;

    p=head;
    fp=fopen(ad,"w+");
    fprintf(fp,"===============================货物信息============================================
");
    fprintf(fp,"编号 名称 进货价 销售价 产地 生产日期 保质期 过期日期 库存量 已销售数量 剩余量 收益
");
    while(p!=Null)
    {
        fprintf(fp,"%s %s %.2f %.2f %s %d %d %d %d %d %d %d %d %.2f
",p->number,p->name,p->Pprice,p->Sprice,p->ori,p->Pdate.year,p->Pdate.month,p->Pdate.day,p->Slife,p->tim,p->inv,p->Bsold,p->left,p->income);
        p=p->next;
    }
    fclose(fp);
}
原文地址:https://www.cnblogs.com/qisong178878915/p/3405809.html