#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;
}
@