m0_51483219 2022-04-11 21:45 采纳率: 50%
浏览 31
已结题

商品信息增删改查,在显示添加的商品信息模块运行结果出错,


#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


//商品信息 
struct Goods{
    char artnum[4];
    char name[10];
    float sale;
    int num;
};

//商品信息列表
struct Glist{
    struct Goods* data;
    int yl;//列表有效元素 
    int rl;//列表当前最大容量 

};

//初始化商品信息
void cs_sp(struct Glist* sp);
//检查商品信息 
void check_sp(struct Glist* sp);
//添加商品信息 
void tj_sp(struct Glist* sp);
//删除商品信息 
void sc_sp(struct Glist* sp); 
//修改商品信息
void xg_sp(struct Glist* sp); 
//查找商品信息
void cz_sp(const struct Glist* sp);
//保存商品信息
void save_sp(struct Glist* sp); 
//加载商品信息 
void jz_sp(struct Glist* sp);
//显示全部商品信息
void show_sp(struct Glist* sp) ; 


void menu()
{
    printf("****************************************\n");
    printf("------------1.添加商品信息--------------\n");
    printf("------------2.删除商品信息--------------\n");
    printf("------------3.查找商品信息--------------\n");
    printf("------------4.修改商品信息--------------\n");
    printf("------------5.显示商品信息--------------\n");
    printf("------------6.保存商品信息--------------\n");
    printf("------------0.返回上一界面--------------\n");
    printf("****************************************\n");

}

int main()
{

    int input = 0;
    //创建一个商品信息列表 
    struct Glist list;
    //初始化商品信息列表 
    cs_sp(&list);
    do
    {
        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            tj_sp(&list);
            break;
        case 2:
            sc_sp(&list);
            break;
        case 3:
            cz_sp(&list);
            break; 
        case 4:
            xg_sp(&list);
            break;
        case 5:
            show_sp(&list);
            break; 
        case 6:
            //保存信息
            save_sp(&list);
            printf("退出\n");
            break;
        default:
            printf("选择错误\n");
            break;
        }
    } while (input);
    return 0;
}

//初始化 
void cs_sp(struct Glist* sp)
{
    sp->yl = 0;
    //最多可以放3个商品的信息,空间不够,可以增容
    sp->data = (struct     Goods*)malloc(3 * sizeof(struct Goods));
    sp->rl = 3;

    //加载文件信息到商品信息中
    jz_sp(sp);
}

//加载 
void jz_sp(struct Glist* sp)
{
    //打开文件
    FILE* pf = fopen("goods.txt", "rb");
    if (NULL == pf)
    {
        perror("LoadGoods::fopen");//打印出错误信息
        return;
    }
    //读文件
    struct Goods temp = { 0 };

    while (fread(&temp, sizeof(struct Goods), 1, pf))
    {
        //空间不够的话,得增加空间
        check_sp(sp);

        sp->data[sp->yl] = temp;
        sp->yl++;
    }

    //关闭文件
    fclose(pf);
    pf = NULL;
}

//添加商品信息 
void tj_sp(struct Glist* sp)
{
    
    check_sp(sp);
    //录入新增商品的信息
//    int wy;
//    do
//    {    
//        printf("请输入商品货号:>");
//        scanf("%s", sp->data[sp->yl].artnum);
//        wy = UniqueString(sp);
//        
//        } while(strlen(sp->data[sp->yl].artnum)!=4||wy==-1);
        //唯一性判定
        
        //有效性判定 
        printf("请输入商品货号:>");
        scanf("%s", sp->data[sp->yl].artnum);
                
        printf("请输入商品名称:>");
        scanf("%s", sp->data[sp->yl].name);
        
        printf("请输入商品的售价:>");
        scanf("%f", &sp->data[sp->yl].sale);
        
        printf("请输入商品的数量:>");
        scanf("%d", &sp->data[sp->yl].num);
        
        sp->yl++;
        printf("增加成功\n");
}


//检查商品信息 
void check_sp(struct Glist* sp)
{
    if (sp->yl == sp->rl)
    {
        //增加容量
        struct Goods* gds = (struct Goods*)realloc(sp->data, (sp->rl + 2)*sizeof(struct Goods));
        if (gds != NULL)
        {
            sp->data = gds;
            sp->rl += 2;

            printf("增容成功\n");
        }
        else
        {
            perror("增容失败");
            exit(1);
        }
    }    
}

//判定唯一性
//int UniqueString(struct Glist* sp)
//{
//    int i = 0;
//    for(i=0; i<sp->yl; i++) {
//        if(strcmp(sp->data[sp->yl].artnum, sp->data[i].artnum) == 0) {
//            printf("该信息已经存在"); 
//            return -1;
//        }
//    }
//    return 1;
//} 

//按商品货号查找 
int FindGoodsByArtnum(const struct Glist* sp,char artnum[])
{
    int i = 0;
    for ( i = 0; i < sp->yl; i++)
    {
        if (strcmp(sp->data[i].artnum, artnum) == 0)
        {
            return i;
        }
    }
    return -1;
}
void sc_sp(struct Glist* sp)
{
    if (sp->yl == 0)
    {
        printf("商品列表为空,无法删除\n");
        return;
    }
    char artnum[4] = {0};
    printf("请输入要删除删除商品的货号:>");
    scanf("%s", artnum);
    //查找
    int cz = FindGoodsByArtnum(sp, artnum);
    if (cz == -1)
    {
        printf("指定的商品货号不存在\n");
    }
    else
    {
        //删除
        int j = 0;
        for ( j = cz; j < sp->yl - 1; j++)   //把要删除元素的所有后面的元素往前移动1
        {
            sp->data[j] = sp->data[j + 1];
        }
        sp->yl--;
        printf("删除成功\n");

    }
}

//查找商品信息 
void cz_sp(const struct Glist* sp)
{
    char artnum[4] = { 0 };
    printf("输入要查找商品的货号");
    scanf("%s", artnum);
    int cz = FindGoodsByArtnum(sp, artnum);
    if (-1 == cz)
    {
        printf("查无此商品\n");
    }
    else
    {
        printf("%10s\t%5s\t%5s\t%5s\n\n", "artnum", "name", "sale", "num");
        printf("%10s\t%5s\t%5.2f\t%5d\n", sp->data[cz].artnum, sp->data[cz].name, sp->data[cz].sale, sp->data[cz].num);
//        for (cz = 0; cz < sp->yl; cz++)
//        {
//            printf("%10s\t%5s\t%5.2f\t%5d\n", sp->data[cz].artnum, sp->data[cz].name, sp->data[cz].sale, sp->data[cz].num);
//
//        }
    }
}

//修改商品信息 
void xg_sp(struct Glist* sp)
{
    char artnum[4] = { 0 };
    printf("输入要修改商品的货号");
    scanf("%s", artnum);
    int cz = FindGoodsByArtnum(sp, artnum);
    if (-1 == cz)
    {
        printf("要修改的商品不存在\n");
    }
    else
    {
        printf("请输入新的商品货号:>");
        scanf("%s", sp->data[cz].artnum);
        printf("请输入商品名称:>");
        scanf("%s", sp->data[cz].name);
        printf("请输入商品售价:>");
        scanf("%f", &sp->data[cz].sale);
        printf("请输入数量:>");
        scanf("%d", &sp->data[cz].num);    
    }
}

//显示全部商品信息
void show_sp(struct Glist* sp)
{
    int i = 0;
    printf("%10s\t%5s\t%5s\t%5s\n\n", "artnum", "name", "sale", "num");
    for ( i = 0; i < sp->yl; i++)
    {
        printf("%10s\t%5s\t%5.2f\t%5d\n",sp->data[i].artnum, sp->data[i].name, sp->data[i].sale, sp->data[i].num);
    }
} 

//保存商品信息 
void save_sp(struct Glist* sp)
{
    //打开文件
    FILE* pf = fopen("goods.txt", "wb");//以二进制的形式写
    if (pf == NULL)
    {
        perror("SaveGoods::fopen");
        return;
    }
    //写数据
    for (int i = 0; i < sp->yl; i++)
    {
        
        fwrite(sp->data + i, sizeof(struct Goods), 1, pf);
    }
    //关闭文件
    fclose(pf);
    pf = NULL;
}


img

img

img

@

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2022-04-11 22:14
    关注

    struct Goods{
    char artnum[4];
    char name[10];
    float sale;
    int num;
    };
    这里的artnum数组太小了,你输入123n有四个字符,导致artnum数组没有存储字符串结束符,后续输出字符串时,会一直向后找到第一个字符串结束符,所以就找到了后面的name数组的字符串结束符,导致输出了123n方便面。将artnum数组大小改大一点就好了,比如 char artnum[10];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月20日
  • 已采纳回答 4月12日
  • 创建了问题 4月11日

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?