标题:为什么运行到删除商品时,系统就崩溃了,求助各位大神啊。。。
只看楼主
风飘飘于玲玲
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-3-17
 问题点数:0 回复次数:0 
为什么运行到删除商品时,系统就崩溃了,求助各位大神啊。。。
//MARKET.h
#ifndef _MARKET_H_
#define _MARKET_H_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NUM 5
struct item {
    char brand[10];
    char id[10];
    float inprice;
    float outprice;
    int storage;
};
struct item_node{
    struct item needed;
    int amount;
    struct item_node *next;
};
void establish();
void goodslist();
void shopping_cart();
void calculate();
void adding();
void deletet ();
void list();
struct item goods[NUM];
struct item_node *cart;
#endif


//market.c
#include "MARKET.h"
main()
{
    int a,b;
        printf("******************************\n");
    printf("欢迎进入超市信息管理系统^_^,请选择数字进行选择:\n\n");
    while(1)
{
    printf("\t1.建立商品库\n");
    printf("\t2.显示所有商品\n");
    printf("\t3.购物车\n");
    printf("\t4.结算商品\n");
    printf("\t5.退出系统-_-!\n");
    printf("*****************************\n");

    scanf("%d",&a);
    while(a<1||a>5)
    {
        printf("输入错误,请重新输入!\n");
        scanf("%d",&b);
        a=b;
    }
        switch(a)
        {
            case 1:
            establish();   
              
             continue;
        case 2:
             goodslist();
             continue;
        case 3:
             shopping_cart();
             continue;
        case 4:
              calculate();
             continue;
        case 5 :
            printf("谢谢使用!\n");
            exit(0);
        
         
        }
   
   
   
   
    }

}
   

    //establish.c
    void establish()
    {
        FILE *fp;
        int i;
        printf("请输入货物信息:\n");
        printf("-----------------------------\n");
        for (i=0;i<NUM;++i)
        {
            printf("商品%d  \n",i);
            printf("产品号:\n");
            fflush(stdin);
            gets(goods[i].id);
            printf("名字:\n");
            fflush(stdin);
            gets(goods[i].brand);
            printf("单价:\n");
            fflush(stdin);
            scanf("%f",&goods[i].outprice);
            printf("库存:\n");
            fflush(stdin);
            scanf("%d",&goods[i].storage);
            printf("进价:\n");
            fflush(stdin);
            scanf("%f",&goods[i].inprice);
        }
        if((fp=fopen("goods","w"))==0)
        {
            printf("创建文件失败!\n");
            return;
        }
        fwrite(goods,sizeof(struct item),NUM,fp);
        fclose(fp);
    }
        void goodslist()
    {
        int i;
        FILE *fp;
        fp=fopen("goods","r");//打开文件,读取商品信息
        for(i=0;(fread(goods+i,sizeof(struct item),1,fp)!=0);i++)
        {
            printf("---------------------\n");
            printf("产品号  名字   单价     库存\n");
            printf("%s     %s  %7.2f    %d\n",goods[i].id ,goods[i].brand ,goods[i].outprice ,goods[i].storage);
            printf("---------------------\n");
        }
        fclose(fp);
    }
    void shopping_cart()
    {
        int q;
            
        printf(" 您的购物车  \n");
        printf(" 请选择数字进行操作:\n");
        while(1)
   {
        printf("1.当前商品\n");
        printf("2.添加商品\n");
        printf("3.删除商品\n");
        printf("4.狠心离开\n");
   
        scanf ("%d",&q);
            
        while(q<1||q>4)
        {
        printf("输入错误,请重新输入!\n");
        scanf("%d",&q);
        }
   
     switch (q)
     {
      case 1:
         list();
         break;
     case 2:
         adding();
         break;
     case 3:
         deletet();
         break;
     case 4:
        return;
     default :
         printf("输入错误,请重新输入!\n");
         
     }
   
    }
        
        
}
void list()
{
            struct item_node *p=cart;
            if(p==NULL)
            {
                printf("空空如也!\n");
                return ;
            }
            while(p!=0)
            {
                printf("----------------\n");
                printf("产品号   名字   单价    数量\n");
                printf("%3s   %3s   %7.2f    %d\n",p->needed.id ,p->needed.brand,p->needed.outprice ,p->amount);
                  p=p->next;
            }
}
void adding()
        {
            FILE *fp;
            int i,o;
            char str[NUM];
            char choiceA,choiceB;
            struct item_node *p,*p1;
            do{
                printf("输入产品号:\n");
                fflush(stdin);
                gets(str);
                if((fp=fopen("goods","r"))==NULL)
                {
                    printf("打开文件失败,请重试!\n");
                    continue;
                }
                for(i=0;(fread(goods+i,sizeof(struct item),1,fp)!=0);i++)
            {   
                if((strcmp(goods[i].id,str)==0)&&(goods[i].storage!=0))
                {
                printf("找到此商品:\n");
                printf("产品号    名字    单价   库存\n");
                printf("%5s%10s%8.2f %d\n",goods[i].id,goods[i].brand ,goods[i].outprice ,goods[i].storage );
                printf("请输入所需数量:\n");
                scanf("%d",&o);
                if(o>goods[i].storage)
            {
                printf("库存不足!请重新选择!");
                break;
            }
                printf("是否购买?(y/n)\n");
                fflush(stdin);
                choiceA=getchar();
            if(choiceA=='Y'||choiceA=='y')
            {
                p1=(struct item_node*)malloc(sizeof(struct item_node));
                if(p1==NULL)
                {
                    printf("内存申请失败!\n");
                    exit(1);
                }
                p1->amount=o;
                p1->needed =goods[i];
               
                p1->next =NULL;
                p=cart;
                if(cart==NULL)
                cart=p1;
                else
                {
                while (p->next!=NULL)
               
                p=p->next;
                p1->next=p->next;
                p->next=p1;
                }
                }
                break;
                }
            }
            if(i==NUM)   
                printf("未找到该商品!\n");
                fclose(fp);
                printf("是否继续?(N/Y)\n");
                fflush(stdin);
                   choiceB=getchar();
            }while(choiceB=='Y'||choiceB=='y');
   
        }
        
    void deletet()
    {
        int i,m;
        char o[10],*g;
        struct item_node *p=cart;
        if(p==NULL)
        {
            printf("您还未购买货物!\n");
            return ;
        }
        while(p!=NULL)
        {
        printf("-------------\n");
        printf("产品号    名字    单价   数量\n");
        printf("%s %s%8.2f %d\n",p->needed.id ,p->needed.brand,p->needed.outprice,p->amount);
        p=p->next;
        printf("请输入需要删除的货物的产品名:\n");
        scanf("%s",o[10]);
        g=p->needed.brand;
        printf("%s",g);
        if(strcmp(g,o)==0)
        {
        printf("请输入删除的数量:\n");
        scanf("%d",&m);
        if(m>=p->amount||m<=0 )
        printf("输入错误!\n");
        else
        p->amount-=m;
        
    }
        }
    }            
        
   
    /*calculate.c*/

    void calculate()
    {
        float all=0,pay,t;
        struct item_node *p;
        int i;
        FILE *fp;
        printf("购物清单:\n");
        list();
        if ((fp=fopen("goods","r"))==0)
        {
            printf("打开文件失败!\n");
            return;
        }
        for(i=0;fread(goods+i,sizeof(struct item),1,fp)!=0;i++);
        fclose(fp);
        p=cart;
        while(p!=0)
        {
            all+=p->needed .outprice *p->amount ;
            for(i=0;strcmp(goods[i].id,p->needed .id)!=0;i++);
                goods[i].storage -=p->amount;
            p=p->next;
        
        }
        printf("总计%f",all);
            printf("输入实付金额:\n");
            scanf("%f",&pay);
            t=pay-all;
            if(t<0)
            {
                printf("金额不足!请重新支付!\n");
                return;
            }
            else
        printf("找零:%f",t);
        if((fp=fopen("goods","w"))==NULL)
        {
            printf("打开文件失败。\n");
            return;
        }
        fwrite(goods,sizeof(struct item),NUM,fp);
        fclose(fp);
    }

   
搜索更多相关主题的帖子: include storage needed amount 
2017-03-17 21:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-475198-1-1.html




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

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