一:程序启动后显示主菜单,包括下面的选
项:
1.显示货物类型列表
2.增加货物类型
3.删除货物类型
4.货物入库
5. 货物出库
6.库存显示
7.退出
按下相应的按键后进入各自的子功能
二:每个子功能执行完毕后,返回并显示主菜
单
三:详细说明
货物类型列表包括货物类型总数,各货物类型
名称以及该货物类型下已有的货物总数
货物信息包括货物名称,货物价格,所属类型
等
货物类型显示:要求输入货物类型,能够显示
该类型下的货物列表
退出程序,返回操作系统。
部分截图:
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
/*
1.显示货物类型列表
2.增加货物类型
3.删除货物类型
4.货物入库
5. 货物出库
6.库存显示
7.退出
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
//货物信息
typedef struct
{
char name[20];// 货物名称
double price; // 货物价格;
char type[20];//所属类型
int nmb; //数量
}Stuf;
//菜单
void menu()
{
system("cls");
printf("1.显示货物类型列表\n");
printf("2.增加货物类型\n");
printf("3.删除货物类型\n");
printf("4.货物入库\n");
printf("5.货物出库\n");
printf("6.库存显示\n");
printf("7.退出\n");
printf("请选择:\n");
}
//1显示货物类型列表
void showStufTypeList(Stuf list[],int nmbStuf)
{
char type[100][20]={0}; //存储所有货物类型
int nmb[100]={0}; //存储所有类型货物对应的数量
int i,j, cnt = 0;
system("cls"); //清屏
//遍历链表
for(j = 0;j<nmbStuf;j++)
{
for (i = 0; i < cnt; i++)
{
if (strcmp(type[i], list[j].type) == 0)
{
nmb[i] += list[j].nmb; //数量累加
break;
}
}
if (i == cnt) //类型在数组中不存在
{
strcpy(type[cnt], list[j].type);
nmb[cnt] = list[j].nmb;
cnt++;
}
}
if (cnt == 0)
{
printf("目前暂无货物信息\n");
system("pause");
return;
}
//输出
printf("当前货物类型数量:%d\n", cnt);
printf("--------------货物类型列表------------\n");
printf("货物类型 货物数量\n");
for (i = 0; i < cnt; i++)
printf("%-8s %-d\n", type[i], nmb[i]);
system("pause");
}
//2增加货物类型
void addStufType(Stuf list[], int *nmbStuf)
{
char type[20];
int i;
char ch;
system("cls");
while (1)
{
printf("请输入需要添加的货物类型:");
scanf("%s", type);
for (i = 0; i < *nmbStuf; i++)
{
if (strcmp(list[i].type, type) == 0)
{
printf("该货物类型已经存在!");
break;
}
}
if (i == *nmbStuf)
{
strcpy(list[*nmbStuf].type, type);
list[*nmbStuf].name[0] = 0;
list[*nmbStuf].nmb = 0;
list[*nmbStuf].price = 0;
*nmbStuf += 1;
printf("货物类型添加成功!\n");
system("pause");
return;
}
else
{
printf("是否重新输入(Y/N)?:");
rewind(stdin); //vs2017版本及以后用,之前的版本用 fflush(stdin)
//fflush(stdin);
ch = getchar();
getchar();
if (ch != 'Y' && ch != 'y')
{
system("pause");
return;
}
}
}
}
//3删除货物类型
void deleteStuf(Stuf list[], int* nmbStuf)
{
char type[20];
int i,j, flag = 0, nmb = 0;
char ch;
system("cls");
printf("请输入需要删除的货物类型:");
scanf("%s", type);
for (i = 0; i < *nmbStuf; i++)
{
if (strcmp(list[i].type, type) == 0)
{
flag = 1;
nmb += list[i].nmb;
}
}
if (flag == 0)
printf("未找到该类型的货物\n");
else
{
if (nmb > 0)
{
printf("该类型货物下有库存,是否确定删除(Y/N)?:");
rewind(stdin);
ch = getchar();
getchar();
if (ch == 'Y' && ch == 'y')
flag = 1;
else
flag = 2; //输入其它默认,不删除
}
if (flag == 1)
{
//删除所有数据
for (i = 0; i < *nmbStuf; i++)
{
if (strcmp(list[i].type, type) == 0)
{
//删除
for (j = i; j < *nmbStuf - 1; j++)
list[j] = list[j + 1];
*nmbStuf -= 1;
i--; //抵消for循环的i++
}
}
printf("删除成功!\n");
}
}
system("pause");
}
//4货物入库
void stufIn(Stuf list[], int* nmbStuf)
{
char name[20], type[20];
double price;
int nmb,i;
char ch;
system("cls");
printf("请输入货物名称:");
scanf("%s", name);
printf("请输入货物价格:");
scanf("%lf", &price);
printf("请输入货物所属类型:");
scanf("%s", type);
printf("请输入货物数量:");
scanf("%d", &nmb);
//判断该货物是否已经存在
for (i = 0; i < *nmbStuf; i++)
{
if (strcmp(list[i].name, name) == 0 && strcmp(list[i].type, type) == 0)//表示货物已存在,并且名称和类型一致
{
if (list[i].price == price) //如果价格也一致,则只更新数量即可
{
list[i].nmb += nmb;
printf("货物入库成功!\n");
break;
}
else
{
printf("货物已存在,但是价格不一致,是否更新价格(Y/N)?");
rewind(stdin);
ch = getchar();
getchar();
if (ch == 'Y' || ch == 'y')
{
list[i].price = price;
}
list[i].nmb += nmb;
printf("货物入库成功!\n");
break;
}
}
else if (strcmp(list[i].type, type) == 0 && strlen(list[i].name) == 0) //只添加了类型,没有货物信息
{
strcpy(list[i].name, name);
strcpy(list[i].type, type);
list[i].price = price;
list[i].nmb = nmb;
printf("货物入库成功!\n");
break;
}
}
if (i == *nmbStuf)
{
strcpy(list[*nmbStuf].name, name);
list[*nmbStuf].price = price;
strcpy(list[*nmbStuf].type, type);
list[*nmbStuf].nmb = nmb;
*nmbStuf += 1;
printf("货物入库成功!\n");
}
system("pause");
}
//5货物出库
void stufOut(Stuf list[], int* nmbStuf)
{
int i;
char name[20], type[20];
int nmb;
system("cls");
printf("请输入要出库的货物名称:");
scanf("%s", name);
printf("请输入要出库的货物类型:");
scanf("%s", type);
printf("请输入要出库的数量:");
scanf("%d", &nmb);
for (i = 0; i < *nmbStuf; i++)
{
if (strcmp(list[i].name, name) == 0 && strcmp(list[i].type, type) == 0)
{
if (list[i].nmb >= nmb)
{
list[i].nmb -= nmb;
printf("出库成功!\n");
system("pause");
return;
}
else
{
printf("货物库存数量已不足!当前库存:%d,出库失败!\n", list[i].nmb);
system("pause");
return;
}
}
}
if (i == *nmbStuf)
{
printf("未找到该商品,出库失败!\n");
system("pause");
}
}
//6.库存显示
void showAll(Stuf list[], int nmbStuf)
{
char type[20]={0};
int i,op;
while (1)
{
system("cls");
printf("1.显示所有类型货物库存\n");
printf("2.显示特定类型货物库存\n");
printf("3.结束显示\n");
while (1)
{
scanf("%d",&op);
if (op >= 1 && op <= 3)
break;
}
switch (op)
{
case 1:
printf("\n");
printf("货物名称 所属类型 价 格 库 存\n");
for (i = 0; i < nmbStuf; i++)
{
if(strlen(list[i].name) > 0 )
printf("%8s %8s %4.2lf %4d\n", list[i].name, list[i].type, list[i].price, list[i].nmb);
}
break;
case 2:
printf("请输入需要查询的类型:");
scanf("%s", type);
printf("\n");
printf("货物名称 所属类型 价 格 库 存\n");
for (i = 0; i < nmbStuf; i++)
{
if ( strlen(list[i].name) > 0 && strcmp(list[i].type,type)==0)
printf("%8s %8s %4.2lf %4d\n", list[i].name, list[i].type, list[i].price, list[i].nmb);
}
break;
case 3:
return;
}
system("pause");
}
}
int main()
{
int op;
Stuf stuf[MAXLEN];
int nmb = 0;
while (1)
{
menu();
scanf("%d", &op);
switch (op)
{
case 1:showStufTypeList(stuf, nmb); break;
case 2:addStufType(stuf, &nmb); break;
case 3:deleteStuf(stuf, &nmb); break;
case 4:stufIn(stuf, &nmb); break;
case 5:stufOut(stuf, &nmb); break;
case 6:showAll(stuf, nmb); break;
case 7:return 0;
}
}
return 0;
}