| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1114 人关注过本帖
标题:我写了个商品管理系统,发现添加商品时总出错,找了很久都没找到错哪了,大 ...
只看楼主 加入收藏
蝶舞123
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-6-13
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
我写了个商品管理系统,发现添加商品时总出错,找了很久都没找到错哪了,大神帮忙看看,给个解决方法,谢谢!
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
struct goods{
    char name[20];/*品名*/
    int num,inqty,outqty,remainder;/*编号、进货量、销售量、剩余数*/
    float inmoney,outmoney,sale,profit;/*进价、售价、销售额、毛利*/
    struct goods *next;
};
struct goods * Readfromfile(struct goods *head);/*读取文件信息*/
struct goods * writetofile(struct goods *head);/*将信息写入文件*/
void Show1(struct goods *head);/*显示部分商品信息*/
struct goods * Create(struct goods *head);
struct goods * Insert(struct goods *head,struct goods *p);
struct goods * Delete(struct goods *head,char name[20]); /*删除商品信息*/
struct goods * Update(struct goods *head);  /*修改商品信息*/
struct goods * Account(struct goods *head);/*统计商品信息*/
struct goods * Searchforname(struct goods *head); /*按品名查询商品信息*/
struct goods * Searchforremainder(struct goods *head);/*按剩余数查询商品信息*/
struct goods * Print(struct goods *head);/*按原来顺序显示*/
struct goods * Printsale(struct goods *head);/*按销售额高低显示*/
struct goods * Printprofit(struct goods *head);/*按毛利高低显示*/
FILE *fp;/*定义一个文件指针*/
int k=0;

int main(void)
{
    struct goods *head;
    int choice,num,num1,num2,num3,inqty,outqty,remainder;
    char name[20];
    float inmoney,outmoney,sale,profit;
    inmoney=outmoney=sale=profit=0.00;
    inqty=outqty=num=remainder=0;
    int size=sizeof(struct goods);
    head=Readfromfile(head);
    do{
    system("color 75");
    printf("\t\t*********************************\n"
           "\t\t*        1——创建商品档案       *\n"
           "\t\t*        2——编辑商品信息       *\n"
           "\t\t*        3——统计销售情况       *\n"
           "\t\t*        4——查询商品信息       *\n"
           "\t\t*        5——显示商品信息       *\n"
           "\t\t*        0——退出系统           *\n"
           "\t\t*********************************\n"
           "\t\t         请输入选项编号(0~5):");
    scanf("%d",&choice);
    if(choice>5||choice<0){
        printf("\t\t不存在该选项编号\n\t\t请重新输入选项编号:");
        scanf("%d",&choice);
    }
    switch(choice){
        case 1:
            if(k==0)
            {
                k=1;
                printf("\t\t商品档案已建立!\n\t\t请继续!\n");
            }
            else
                printf("\t\t商品档案已存在,无需重复建立!\n");
            break;
        case 2:
            if(k==0){
                printf("\t\t操作失败!请先建立商品档案!\n");
                break;
            }
            else{
                 do{
                     system("cls");
                     printf("\t\t*********************************\n"
                            "\t\t*      1——添加新商品           *\n"
                            "\t\t*      2——删除原有商品         *\n"
                            "\t\t*      3——修改原有商品信息     *\n"
                            "\t\t*      4——返回                 *\n"
                            "\t\t*      0——退出系统             *\n"
                            "\t\t*********************************\n"
                            "\t\t      请输入选项编号(0~4):");
                     scanf("%d",&num1);
                     if(num1>4||num1<0){
                        printf("\t\t不存在该选项编号\n\t\t请重新输入选项编号:");
                        scanf("%d",&num1);
                     }
                     switch(num1){
                        case 1:
                             if(k==0)
                             {
                                printf("\t\t操作失败!请先建立商品档案!\n");
                                break;
                             }
                            else{
                                head=Create(head);
                                break;
                            }
                        case 2:
                            if(k==0)
                            {
                                printf("\t\t操作失败!请先建立商品档案!\n");
                                break;
                            }
                            else{
                                system("cls");
                                Show1(head);
                                printf("\t\t请输入要删除的商品的名称:");
                                scanf("%s",name);
                                head=Delete(head,name);
                                //writetofile(head);
                                break;
                            }
                        case 3:
                            if(k==0)
                            {
                                 printf("\t\t操作失败!请先建立商品档案!\n");
                                break;
                            }
                            else{
                                system("cls");
                                head=Update(head);
                                //writetofile(head);
                                break;
                            }

                        case 4:break;
                        case 0:exit(0);break;
                      }
                    }while(num1!=4);
                    break;
                  }
        case 3:
            if(k==0)
            {
                printf("\t\t操作失败!请先建立商品档案!\n");
                break;
            }
            else
              system("cls");
              Account(head);
            break;
        case 4:
            if(k==0)
               printf("\t\t操作失败!请先建立商品档案!\n");
            else{
                do{
                     system("cls");
                     printf("\t\t**********************************\n"
                            "\t\t*        1——按品名查询          *\n"
                            "\t\t*        2——按剩余数查询        *\n"
                            "\t\t*        3——返回                *\n"
                            "\t\t*        4——退出系统            *\n"
                            "\t\t**********************************\n"
                            "\t\t         请输入选项编号(0~3):");
                     scanf("%d",&num2);
                     if(num2>3||num2<0){
                         printf("\t\t不存在该选项编号\n\t\t请重新输入选项编号:");
                         scanf("%d",&num2);
                     }
                     switch(num2){
                         case 1:
                             head=Searchforname(head);
                             break;
                         case 2:
                             system("cls");
                             head=Searchforremainder(head);
                             break;
                         case 3:break;
                         case 0:
                             exit(0);
                             break;
                     }
                  }while(num2!=3);
            }
                  break;
        case 5:
            if(k==0)
                printf("\t\t操作失败!\t\t请先建立商品档案!\n");
            else{
                do{
                    system("cls");
                    printf("\t\t************************************\n"
                           "\t\t*        1——按原来顺序显示        *\n"
                           "\t\t*        2——按销售额高低显示      *\n"
                           "\t\t*        3——按毛利高低显示        *\n"
                           "\t\t*        4——返回                  *\n"
                           "\t\t*        0——退出系统              *\n"
                           "\t\t************************************\n"
                           "\t\t         请输入选项编号(0~4):");
                    scanf("%d",&num3);
                    if(num3>4||num3<0){
                       printf("\t\t不存在该选项编号\n\t\t请重新输入选项编号:");
                       scanf("%d",&num3);
                    }
                    switch(num3){
                       case 1:Print;break;
                              printf("添加成功!\n");
                       case 2:Printsale;break;
                       case 3:Printprofit;break;
                       case 4:break;
                       case 0:exit(0);break;
                    }
                }while(num3!=4);
            }
            break;
        case 0:break;
        }
    }while(choice!=0);
    printf("\t\t感谢使用本软件!已正常退出,按任意键结束。\n");
    writetofile(head);/*将信息输入文件*/
    return 0;
}
/*读取文件信息*/
struct goods * Readfromfile(struct goods * head)
{
    char name[20];
    int num,inqty,outqty,remainder;
    float inmoney,outmoney,sale,profit;
    head=NULL;
    head=(struct goods *)malloc(sizeof(struct goods));
    fp=fopen("商店商品档案.txt","r");
    if(fp==NULL)
    {
        printf("文件打开失败!\n");
        exit(0);
    }
    while(fscanf(fp,"%d%s%f%f%d%d%d%f%f",&num,name,&inmoney,&outmoney,&inqty,&outqty)!=EOF)
    {
        struct goods *p1=(struct goods *)malloc(sizeof(struct goods));
        p1->num=num;
        strcpy(p1->name,name);
        p1->inmoney=inmoney;
        p1->outmoney=outmoney;
        p1->inqty=inqty;
        p1->outqty=outqty;
        p1->remainder=inqty-outqty;
        p1->sale=outqty*outmoney;
        p1->profit=outqty*(outmoney-inmoney);
        p1->next=NULL;
        k=1;
        Insert(head,p1);
    }
    fclose(fp);
    return head;
}
/*将信息输入文件*/
struct goods *writetofile(struct goods *head)
{
    fp=fopen("商店商品档案.txt","a");
    struct goods *p;
    p=head->next;
    while(p!=NULL)
    {
        fprintf(fp,"%d\t",p->num);
        fprintf(fp,"%s\t",p->name);
        fprintf(fp,"%f\t",p->inmoney);
        fprintf(fp,"%f\t",p->outmoney);
        fprintf(fp,"%f\t",p->inqty);
        fprintf(fp,"%f\t",p->outqty);
        fprintf(fp,"%f\t",p->remainder);
        fprintf(fp,"%f\t",p->sale);
        fprintf(fp,"%f\n",p->profit);
        p=p->next;
    }fclose(fp);
    return head;
}
/*显示部分商品信息*/
void Show1(struct goods *head)
{
    struct goods *p;
    int num;
    if(head==NULL)
        printf("档案为空!\n");
    else{
        printf("\t\t\t商品信息如下:\n");
        printf("编号\t品名\t\t进价\t售价\t进货量\t售货量\n");
        for(p=head;p!=NULL;p=p->next)
        {
            printf("%d\t%s\t\t%f\t%f\t%f\t%f\n",p->num,p->name,p->inmoney,p->outmoney,p->inqty,p->outqty);
        }
    }
}
struct goods * Create(struct goods *head)
{
    struct goods *p;
    int num,inqty,outqty,remainder;
    char name[20];
    float inmoney,outmoney,sale,profit;
    while(1){
       printf("请输入商品编号:\n");
       scanf("%d",&num);
       printf("请输入商品名称:\n");
       scanf("%s",name);
       printf("请输入商品进价:\n");
       scanf("%lf",&inmoney);
       printf("请输入商品售价:\n");
       scanf("%lf",&outmoney);
       printf("请输入商品进货量:\n");
       scanf("%lf",&inqty);
       printf("请输入商品销售量:\n");
       scanf("%lf",&outqty);
       printf("\t\t************************************\n");
       if(num>0){
          p=(struct goods *)malloc(sizeof(struct goods));
          p->num=num;
          strcpy(p->name,name);
          p->inmoney=inmoney;
          p->outmoney=outmoney;
          p->inqty=inqty;
          p->outqty=outqty;
          p->remainder=p->inqty-p->outqty;
          p->sale=p->outmoney*p->outqty;
          p->profit=p->sale-(p->outqty*p->inmoney);
          p->next=NULL;
          Insert(head,p);
          writetofile(head);
       }
       else
          break;
    }
   printf("添加成功!\n");
   return head;
}
struct goods *Insert(struct goods *head,struct goods *p1)
{
    struct goods *ptr,*ptr1,*ptr2;
    ptr2=head;
    ptr=p1;
    if(head==NULL)
    {
        head=ptr;
        head->next=NULL;
    }
    else{
        while(ptr2->next!=NULL)
        {
            ptr1=ptr2;
            ptr2=ptr2->next;
        }
            ptr2->next=ptr;
            ptr->next=NULL;
        }
    return head;
}
/*删除商品信息*/
struct goods * Delete(struct goods *head,char name[20])
{
    struct goods *p1,*p2;
    while(head!=NULL&&head->name==name)
    {
        p2=head;
        head=head->next;
        free(p2);
        printf("\t\t删除成功!\n");
        printf("\t\t请重新添加商品!\n");
    }
    p1=head;
    p2=head->next;
    while(p2!=NULL)
    {
        if(strcmp(p2->name,name)==0)
        {
            p1->next=p2->next;
            free(p2);
            printf("\t\t删除成功!\n");
        }
        else
            p1=p2;
        p2=p1->next;
    }
    return head;
}
/*修改商品信息*/
struct goods * Update(struct goods *head)
{
    struct goods *p,*p1;
    int num1,j=0,m,inqty1,outqty1;
    double inmoney1,outmoney1;
    char ch1,name[20],name1[20];
    if(head==NULL)
        printf("\t\t档案为空!\n");
    else{
        printf("\t\t商品信息如下:\n");
        printf("编号\t品名\t\t进价\t售价\t进货量\t售货量\n");
        for(p=head;p!=NULL;p=p->next)
        {
            printf("%d\t%s\t\t%.2lf\t%.2lf\t%d\t%d\n",p->num,p->name,p->inmoney,p->outmoney,p->inqty,p->outqty);
        }
        printf("\t\t请输入要修改的商品名称:");
        scanf("%s",name);
        p=head;
        while((p!=NULL)||(j=1))
        {
            p=p->next;
            if(strcmp(p->name,name)==0)
            {
                printf("\t\t要修改的商品信息如下:\n");
                printf("编号:%d 品名:%s 进价:%.2lf 售价:%.2lf 进货量:%d 售货量:%d\n",p->num,p->name,p->inmoney,p->outmoney,p->inqty,p->outqty);
                j=1;
            }
        }
        if(j==0)
            printf("不存在该商品!\n");
        if(j==1)
        {
            do{
            printf("\t\t**********************************\n"
                   "\t\t*        A.修改商品编号          *\n"
                   "\t\t*        B.修改商品名称          *\n"
                   "\t\t*        C.修改商品进价          *\n"
                   "\t\t*        D.修改商品售价          *\n"
                   "\t\t*        E.修改商品进货量        *\n"
                   "\t\t*        F.修改商品销售量        *\n"
                   "\t\t*        G.修改完毕,退出!      *\n"
                   "\t\t**********************************\n"
                   "\t\t   请输入要修改的商品信息选项:");
            scanf("%c",&ch1);
            switch(ch1){
                case 'A':
                     printf("\t\t请输入新的商品编号:");
                     scanf("%d",num1);
                     p->num=num1;
                     printf("\t\t修改成功!\n");
                     break;
                case 'B':
                     printf("\t\t**********************************\n"
                            "\t\t*        1.删除商品名称         *\n"
                            "\t\t*        2.更改商品名称         *\n"
                            "\t\t**********************************\n"
                            "\t\t       请输入你要执行的操作:");
                     scanf("%d",&m);
                     if(m==1)
                          strcpy(p->name,"未知");
                     else{
                          printf("\t\t请输入新的商品名称:");
                          scanf("%s",name1);
                          strcpy(p->name,name1);
                     }
                     printf("\t\t修改成功!\n");
                     break;
                case 'C':
                    printf("\t\t**********************************\n"
                           "\t\t*        1.删除商品进价         *\n"
                           "\t\t*        2.更改商品进价         *\n"
                           "\t\t**********************************\n"
                           "\t\t      请输入你要执行的操作:");
                    scanf("%d",&m);
                    if(m==1)
                    {
                        p->inmoney=0.00;
                        p->profit=p->sale;
                    }
                    else
                    {
                        printf("\t\t请输入新的商品进价:");
                        scanf("%.2lf",&inmoney1);
                        p->inmoney=inmoney1;
                        p->profit=p->sale-inmoney1*p->outqty;
                    }
                    printf("\t\t修改成功!\n");
                    break;
                case 'D':
                    printf("\t\t*********************************\n"
                           "\t\t*       1.删除商品售价         *\n"
                           "\t\t*       2.更改商品售价         *\n"
                           "\t\t*********************************\n"
                           "\t\t     请输入你要执行的操作:");
                    scanf("%d",&m);
                    if(m==1)
                    {
                        p->outmoney=p->sale=0.00;
                        p->profit=-(p->inmoney*p->outqty);
                    }
                    else{
                        printf("\t\t请输入新的商品售价:");
                        scanf("%.2lf",&outmoney1);
                        p->outmoney=outmoney1;
                        p->sale=p->outqty*outmoney1;
                        p->profit=p->sale-p->inmoney*p->outqty;
                     }
                    printf("\t\t修改成功!\n");
                    break;
                case 'E':
                    printf("\t\t*********************************\n"
                           "\t\t*       1.删除商品进货量       *\n"
                           "\t\t*       2.更改商品进货量       *\n"
                           "\t\t*********************************\n"
                           "\t\t     请输入你要执行的操作:");
                    scanf("%d",&m);
                    if(m==1)
                    {
                        p->inqty=p->outqty=p->profit=p->sale=p->remainder=0;
                    }
                    else{
                    printf("\t\t请输入新的商品进货量:");
                    scanf("%d",&inqty1);
                    p->inqty=inqty1;
                    p->remainder=inqty1-p->outqty;
                    }
                    printf("\t\t修改成功!\n");
                    break;
                case 'F':
                    printf("\t\t*********************************\n"
                           "\t\t*       1.删除商品销售量       *\n"
                           "\t\t*       2.更改商品销售量       *\n"
                           "\t\t*********************************\n"
                           "\t\t      请输入你要执行的操作:");
                    scanf("%d",&m);
                    if(m==1)
                    {
                        p->outqty=p->sale=p->profit=0;
                        p->remainder=p->inqty;
                    }
                    else{
                    printf("\t\t请输入新的商品销售量:");
                    scanf("%d",&outqty1);
                    p->outqty=outqty1;
                    p->remainder=p->inqty-p->outqty;
                    p->sale=p->outqty*p->outqty;
                    p->profit=p->sale-p->outqty*p->outmoney;
                    }
                    printf("\t\t修改成功!\n");
                    break;
                case 'G':break;
                default:
                    printf("\t\t不存在该选项!\n\t\t请重新选择输入!\n");
                    break;
            }
          } while(ch1!='G');
        }
       }
  return head;
}
/*统计商品信息*/
struct goods * Account(struct goods *head)
{
    struct goods *p;
    int num=1;
    if(head==NULL)
        printf("\t\t档案为空!\n");
    else{
        printf("\t\t商品销售情况如下:\n");
        printf("编号\t品名\t\t剩余数\t销售额\t利润\n");
        for(p=head;p!=NULL;p=p->next)
        {
            printf("%d\t%s\t\t%d\t%.2lf\t%.2lf\n",p->num,p->name,p->remainder,p->sale,p->profit);
        }
    }
    return head;
}
/*按品名查询商品信息*/
struct goods * Searchforname(struct goods *head)
{
    struct goods *p;
    char name[20];
    int n=0;
    printf("\t\t请输入要查询的商品名称:");
    scanf("%s",name);
    for(p=head;p!=NULL;p=p->next)
    {
        if(p->name==name)
        {
             printf("编号:%d 品名:%s 进价:%.2lf 售价:%.2lf\n进货量:%d 售货量:%d 剩余量:%d 销售额:%.2lf 毛利:%.2lf\n",
                             p->num,p->name,p->inmoney,p->outmoney,p->inqty,p->outqty,p->remainder,p->sale,p->profit);
             n=1;
        }
    }
    if(n==1)
        printf("\t\t查询结束!\n");
    else
        printf("\t\t不存在该商品!\n");
    return head;
}
/*按剩余数查询商品信息*/
struct goods * Searchforremainder(struct goods *head)
{
    struct goods *p;
    int n;
    printf("\t\t**********************************\n"
           "\t\t*  1.剩余数小于5                *\n"
           "\t\t*  2.剩余数大于等于5且小于等于2 *\n"
           "\t\t*  3.剩余数大于20               *\n"
           "\t\t**********************************\n"
           "\t\t   请选择查询编号:");
    scanf("%d",&n);
    if(n==1)
    {
        for(p=head;p!=NULL;p=p->next)
        {
            if(p->remainder<5)
             printf("编号:%d 品名:%s 进价:%.2lf 售价:%.2lf\n进货量:%d 售货量:%d 剩余量:%d 销售额:%.2lf 毛利:%.2lf\n",
                             p->num,p->name,p->inmoney,p->outmoney,p->inqty,p->outqty,p->remainder,p->sale,p->profit);
        }
    }
    else if(n==2)
    {
         for(p=head;p!=NULL;p=p->next)
        {
            if((p->remainder>5)&&(p->remainder<20))
             printf("编号:%d 品名:%s 进价:%.2lf 售价:%.2lf\n进货量:%d 售货量:%d 剩余量:%d 销售额:%.2lf 毛利:%.2lf\n",
                             p->num,p->name,p->inmoney,p->outmoney,p->inqty,p->outqty,p->remainder,p->sale,p->profit);
        }
    }
        else if(n==3)
             {
                  for(p=head;p!=NULL;p=p->next)
                  {
                           if(p->remainder>20)
                                   printf("编号:%d 品名:%s 进价:%.2lf 售价:%.2lf\n进货量:%d 售货量:%d 剩余量:%d 销售额:%.2lf 毛利:%.2lf\n",
                                          p->num,p->name,p->inmoney,p->outmoney,p->inqty,p->outqty,p->remainder,p->sale,p->profit);
                  }

             }
             else
                 printf("\t\t查询编号错误!\n");
     return head;
}
/*按原来顺序显示*/
struct goods * Print(struct goods *head)
{

}
/*按销售额高低显示*/
struct goods * Printsale(struct goods *head)
{

}
/*按毛利高低显示
struct goods * Printprofit(struct goods *head)
{

}*/
搜索更多相关主题的帖子: 管理系统 include Create profit 销售额 管理系统 include Create profit 销售额 
2017-06-13 12:19
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:20 
是入口处head没有初始化吗?
程序代码:
struct goods * Create(struct goods *head)
{
    struct goods *p;
    int num,inqty,outqty,remainder;
    char name[20];
    float inmoney,outmoney,sale,profit;
    while(1){
       printf("请输入商品编号:\n");
       scanf("%d",&num);//<--输入完立刻判断,避免以下的无效输入。
       printf("请输入商品名称:\n");
       scanf("%s",name);
       printf("请输入商品进价:\n");
       scanf("%lf",&inmoney);
       printf("请输入商品售价:\n");
       scanf("%lf",&outmoney);
       printf("请输入商品进货量:\n");
       scanf("%lf",&inqty);
       printf("请输入商品销售量:\n");
       scanf("%lf",&outqty);
       printf("\t\t************************************\n");
       if(num>0){//可前置
          p=(struct goods *)malloc(sizeof(struct goods));
          p->num=num;
          strcpy(p->name,name);

剑栈风樯各苦辛,别时冰雪到时春
2017-06-13 12:45
快速回复:我写了个商品管理系统,发现添加商品时总出错,找了很久都没找到错哪了 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025510 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved