#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 10
typedef struct //定义商品的结构体
{
char name[10];
int num;
float price;
int ID;
}Store;
Store store[M];
void shuru() //定义输入商品的函数
{
int i;
int isgo;
int n;
for (i = 0; i < M; i++)
{
store[i].ID = i + 1;//自定义商品的ID
printf("输入商品的名字\n");
scanf_s("%s", &store[i].name);
printf("输入商品的价格\n");
scanf_s("%f", &store[i].price);
printf("还需要输入商品吗?\n");
printf("1.YES,2.NO\n");
scanf_s("%d", &isgo);
if (isgo != 2)//判断是否继续输入商品
{
}
else//不输入商品直接跳出,展示所有商品的信息
{
for (n = 0; n <= i; n++)
{
printf("商品ID\t商品名字\t商品价格\n");
printf("%d\t,%s\t,%f\n", store[n].ID, store[n].name, store[n].price);
}
i = 10;
}
}
}
void shopp() //购物函数
{
int isgo = 1;
int i;
int n;
int number;
int shoppid;
float pricenum;
float pricesum = 0.0;
float priceguke;
printf("库存的商品如下\n");
for (n = 0; n < M; n++)
{
printf("商品ID\t商品名字\t商品价格\n");
printf("%d\t,%s\t,%f\n", store[n].ID, store[n].name, store[n].price);
if (store[n].price == 0)//这里是为了不把没定义的商品不显示出来
{
n = M;
}
}
while (isgo)//购物
{
printf("按下你需要的商品ID和数量\n");
scanf_s("%d,%d", &shoppid, &number);
pricenum = (store[shoppid - 1].price) * number;
pricesum = pricesum + pricenum;
printf("你购买的商品%s是%d个,价格是%f,总金额是%f\n", store[shoppid - 1].name, number, pricenum, pricesum);
printf("是否继续购物\n");
printf("0.退出,1.继续\n");
scanf_s("%d", &isgo);
}
printf("输入顾客支付的金钱\n");//结算
scanf_s("%f", &priceguke);
if (priceguke >= pricesum)
{
printf("收到顾客%f元,找回顾客%f元\n", priceguke, priceguke - pricesum);
}
else
{
printf("收到的钱小于总金额\n") }
printf("谢谢光临\n");
}
int main()
{
int xitong = 1;
while (xitong != 3)
{
printf("\t\t★******★**********★*********★******★ \n");
printf("\t\t★ ★ \n");
printf("\t\t★ ★超市管理系统★ ★ \n");
printf("\t\t★ ★ \n");
printf("\t\t★*************************************★ \n");
printf("\t\t★**********★请选择操作[1-3]**********★ \n");
printf("\t\t★*************************************★ \n");
printf("\t\t★ ★ \n");
printf("\t\t★ ①输入商品信息 ★ \n");
printf("\t\t★ ②购买商品 ★ \n");
printf("\t\t★ ③退出系统 ★ \n");
printf("\t\t★ ★ \n");
printf("\t\t★******★**********★*********★******★ \n");
printf("请输入您的服务:");
scanf_s("%d", &xitong);
switch (xitong)
{
case 1:shuru();
break;
case 2:shopp();
break;
case 3:printf("欢迎再次使用随便买超市系统\n");
}
}
}