#include <stdio.h>
#include <stdlib.h>
int menu();
void JianliKucun();
void XianshiKucun();
void xianshi(struct node *head);
struct node *add();
void calculate(struct node *head);
struct mar //该结构体为存储货物信息
{
int ID; //货号
char brand[20]; //品名
double price; //售价
};
struct node //该结构体为存储购物车中的信息
{
struct mar buy; //货物信息
int number; //购物的数量
struct node *next; //指向下一个购物节点
};
struct node *head = NULL;
struct mar goods[5]; //结构体数组
int main()
{
printf("*********************************************************\n");
printf(" 欢迎进入超市信息管理系统\n");
printf("*********************************************************\n");
int find = 0;
while (1)
{
switch (menu())
{ // menu()函数为输入选项
case 1:
JianliKucun();
break; //选项1 手动建立库存信息
case 2:
XianshiKucun();
break; //选项2 显示库存信息函数
case 3:
xianshi(head);
break; //选项3 显示购物车
case 4:
head = add();
break; // 添加货品进入购物车
case 5:
calculate(head);
break; //选项4 计算所购商品价格并修改保存
case 6:
system("cls");
printf("感谢您的使用,再见!\n");
exit(0);
}
}
return 0;
}
void calculate(struct node *head)
{
double sum;
xianshi(head);
struct node *ptr;
for (ptr = head; ptr != NULL; ptr = ptr->next)
{
sum += ptr->buy.price;
}
printf("货物的总价为%lf", sum);
}
void JianliKucun()
{
int i = 0;
printf("请依次输入货号、品名、售价: (输入-1以结束)");
while (1)
{
scanf("%d",&goods[i].ID);
if (goods[i].ID == -1)
{
break;
}
scanf("%s%lf", goods[i].brand, &goods[i].price);
i++;
}
}
void XianshiKucun()
{
printf("货号 品名 售价\n\n\n\n");
for (int i = 0; i < 3; i++)
{
printf("%d %s %.2f\n\n\n", goods[i].ID, goods[i].brand, goods[i].price);
}
}
struct node *add()
{
int x;
struct node *head = NULL, *tail = NULL, *p;
printf("请输入要添加商品的货号:(输入-1以结束)\n");
scanf("%d", &x);
for (int i = 0; i < 3; i++)
{
if (x == -1)
{
break;
}
if (x == goods[i].ID)
{
p = (struct node *)malloc(sizeof(struct node));
p->buy = goods[i];
printf("添加成功");
p->next = NULL;
if (head == NULL)
{
head = p;
}
else
{
tail->next = p;
}
tail = p;
scanf("%d", &x);
}
}
return head;
}
void xianshi(struct node *head)
{
struct node *ptr;
if (head == NULL)
{
printf("尚未添加货品");
}
else
{
printf("货号 品名 售价\n\n\n\n");
for (ptr = head; ptr != NULL; ptr = ptr->next)
{
printf("%d %s %.2f\n\n\n", ptr->buy.ID, ptr->buy.brand, ptr->buy.price);
}
}
}
int menu()
{ //打印主菜单函数
int n; //选项为整数
while (1)
{
printf("\n\n请选择下面的数字进行操作:\n");
printf("--------------------------------------------\n");
printf("1. 手动建立库存信息\n");
printf("2. 显示所有商品信息\n");
printf("3. 显示购物车中的所有商品\n");
printf("4. 添加货品进入购物车\n");
printf("5. 结算\n");
printf("6. 退出程序\n");
printf("--------------------------------------------\n");
printf("请选择对应数字1~6\n");
scanf("%d", &n);
if (n >= 1 && n <= 6)
{
return n; //返回输入的选项
}
else
{
printf("输入数字有误,请重新输入");
return 0;
}
}
}