C项目-无人售卖机
  C

C项目-无人售卖机

 次点击
12 分钟阅读

项目说明

无人售卖机项目说明1.0

本项目的主要目的是结合实际生活案例,利用C语言基础语法(包括数据类型、控制语句、数组、指针、函数等),实现下无人售卖机项目。

代码实现

#include <stdio.h>
#include <string.h>

void input_error();
void menu_merchant();
void menu_customer();
void list_goods();
void modify_goods();
void purchase_goods();
void shopcar();

//角色的数据类型
enum
{
	merchant = 1,
	customer,
};

//商家的数据类型
enum
{
	list = 1,
	modify,
	purchase,
	sales,
	quit,
};

//顾客的数据类型
enum
{
	list_c = 1,
	shopcar_c,
	quit_c,
};

//商品列表
struct goods
{
	int num;
	char name[64];
	int quantity;
	float price;
}goods[64] = {\
	{1,"苹果",10,3},\
	{2,"香蕉",10,2.5},\
	{3,"橘子",15,4},\
};

int n = 3;//商品列表数量
float sale = 0;//销售额

int main()
{
	int role;
	while(1)
	{
		printf("|************************|\n");
		printf("|*   1.商家    2.顾客   *|\n");
		printf("|************************|\n");
		scanf("%d",&role);
		if (role == merchant)
		{
			//printf("it is merchant\n");
			menu_merchant();
		}
		else if (role == customer)
		{
			//printf("it is customer\n");
			menu_customer();
		}
		else
		{
			input_error();
		}
	}
	return 0;
}

void menu_merchant()//商家菜单
{
	int m;
MENU_MERCHANT:
	printf("|************************|\n");
	printf("|*     1.查看库存       *|\n");
	printf("|*     2.修改商品       *|\n");
	printf("|*     3.采购订单       *|\n");
	printf("|*     4.查看销售额     *|\n");
	printf("|*     5.退出           *|\n");
	printf("|************************|\n");
	scanf("%d",&m);
	if (m == list)
	{
		//printf("list\n");
		list_goods();
		goto MENU_MERCHANT;
	}
	else if (m == modify)
	{
		//printf("modify\n");
		modify_goods();
		goto MENU_MERCHANT;
	}
	else if (m == purchase)
	{
		//printf("purchase\n");
		purchase_goods();
		goto MENU_MERCHANT;
	}
	else if (m == sales)
	{
		printf("销售额为%f\n",sale);
		goto MENU_MERCHANT;
	}
	else if (m == quit)
	{

	}
	else
	{
		input_error();
		goto MENU_MERCHANT;
	}
}

void menu_customer()//顾客菜单
{
	int c;
MENU_CUSTOMER:
	printf("|************************|\n");
	printf("|*       1.商品列表     *|\n");
	printf("|*       2.购物车       *|\n");
	printf("|*       3.退出         *|\n");
	printf("|************************|\n");
	scanf("%d",&c);
	if (c == list_c)
	{
		//printf("list_c\n");
		list_goods();
		goto MENU_CUSTOMER;
	}
	else if (c == shopcar_c)
	{
		//printf("shopcar_c\n");
		shopcar();
		goto MENU_CUSTOMER;
	}
	else if (c == quit_c)
	{

	}
	else
	{
		input_error();
		goto MENU_CUSTOMER;
	}
}

void list_goods()//商品列表
{
	int i;
	printf("num\tname\tquantity\tprice\n");
	for (i = 0; i < n; i++)
	{
		printf("%d\t%s\t%d\t\t%.2f\n",goods[i].num,goods[i].name,goods[i].quantity,goods[i].price);
	}
}

void modify_goods()//修改商品
{
	int num_list;
	list_goods();
	printf("input goods's num\n");
	scanf("%d",&num_list);
	if(num_list > n || num_list < 1)
	{
		input_error();
	}
	else
	{
		printf("input good's quantity and price\n");
		scanf("%d%f",&goods[num_list-1].quantity,&goods[num_list-1].price);
		list_goods();
	}

}

void purchase_goods()//采购订单
{
	int i;
	char s[10];

	printf("num\tname\tquantity\tprice\n");
	for (i = 0; i < n; i++)
	{
		if (goods[i].quantity < 10)
		{
			printf("%d\t%s\t%d\t\t%.2f\n",\
					goods[i].num,goods[i].name,goods[i].quantity,goods[i].price);
			printf("是否需要采购?input 'yes' or 'no'\n");
			scanf("%s",s);
			if(strncmp(s, "yes", 3) == 0)
			{
				goods[i].quantity += 10;
				printf("Successful\n");
			}
		}
	}

}

void shopcar()
{
	int num,quantity;
	int i;
	float sum = 0;

	list_goods();
REINPUT:
	printf("输入购买商品的序号和数量\n");
	scanf("%d%d",&num,&quantity);
	for (i = 0; i < n; i++)
	{
		if (num == goods[i].num)
		{
			if (quantity > goods[i].quantity || quantity < 0)
			{
				input_error();
				goto REINPUT;
			}
			else
			{
			goods[i].quantity -= quantity;
			sum = goods[i].price * quantity;
			sale += sum;
			printf("Successful");
			}
		}
	}
}

void input_error()
{
	printf("input error\n");
}

© 本文著作权归作者所有,未经许可不得转载使用。