使用C++模拟实现道具店购物功能

此项目是学习C++网课的时候按上面的要求完成的,花费了将近2个小时。

范围限定以及实现步骤

1.商店暂时只支持一种类型的商品
2.商品具备名称、价格、库存等属性
3.模拟玩家购买游戏道具
  1. 玩家选择需要购买的道具
  2. 玩家同意交易后扣除相应游戏币
  3. 对应库存数中扣除需要购买的数量
  4. 玩家背包增加该商品且增加该商品数的对应数量

定义结构体的头文件,我将它命名为shop.h

#pragma once
typedef struct Shop
{
	char* name;			//商品名称
	char* desc;			//商品描述
	double price;		//商品价格
	int nums;				//商品余量

};//商城
typedef struct Bag
{
	char* name;			//物品名称
	char* desc;			//物品描述
	int nums;				//物品数量
};
//typedef struct Bag

源文件

#include <string>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "shop.h"
Shop shop[100]; 
Bag bag[100];
static double money = 139226280;
/*
	char* name;			//商品名称
	char* desc;			//商品描述
	double price;	//商品价格
	int nums;			//商品余量
*/
//初始化背包
void Init(struct Bag* bag, int i)//初始化背包
{
	i--;
	for (int n = 0; n < i; n++)
	{
		(bag + n)->name = "NULL";
		(bag + n)->desc = "NULL";
		(bag + n)->nums = 0;
	}
}
//初始化商城
void InputShop( struct Shop* shop,int i)
{
	int j = 0;
	(shop + j)->name = "双倍经验卡";
	(shop + j)->desc = "经验加成";
	(shop + j)->price = 20.0;
	(shop + j)->nums = 100;
	j++;
	(shop + j)->name = "幸运宝石";
	(shop + j)->desc = "幸运道具";
	(shop + j)->price = 30.0;
	(shop + j)->nums = 90;
	j++;
	(shop + j)->name = "五行转换卡";
	(shop + j)->desc = "五行转换";
	(shop + j)->price = 50.0;
	(shop + j)->nums = 70;
	j++;
	(shop + j)->name = "银两袋子";
	(shop + j)->desc = "银两";
	(shop + j)->price = 100.0;
	(shop + j)->nums = 50;
	j++;
	(shop + j)->name = "高级灵珠宝箱";
	(shop + j)->desc = "高级灵珠";
	(shop + j)->price = 100.0;
	(shop + j)->nums = 50;
	j++;
	(shop + j)->name = "置顶传音";
	(shop + j)->desc = "聊天置顶";
	(shop + j)->price = 300.0;
	(shop + j)->nums = 1000;
	j++;
	(shop + j)->name = "女娲转职卡";
	(shop + j)->desc = "转职卡";
	(shop + j)->price = 888.0;
	(shop + j)->nums = 5;
	j++;
	(shop + j)->name = "武神转职卡";
	(shop + j)->desc = "转职卡";
	(shop + j)->price = 888.0;
	(shop + j)->nums = 5;
	j++;
	(shop + j)->name = "魔尊转职卡";
	(shop + j)->desc = "转职卡";
	(shop + j)->price = 888.0;
	(shop + j)->nums = 5;
	j++;
	(shop + j)->name = "初级农场礼包";
	(shop + j)->desc = "农场物品";
	(shop + j)->price = 20.0;
	(shop + j)->nums = 500;
	j++;
	(shop + j)->name = "中级农村礼包";
	(shop + j)->desc = "农场物品";
	(shop + j)->price = 40.0;
	(shop + j)->nums = 300;
	j++;
	(shop + j)->name = "高级农村礼包";
	(shop + j)->desc = "农场物品";
	(shop + j)->price = 188.0;
	(shop + j)->nums = 100;
	return;
}
//输出商城物品信息
void OutputShop(struct Shop* shop, int i)
{
	for (int n = 0; n < i; n++)
	{
		printf_s("商品编号%-8d的名称为:%-8s\t商品描述:%-8s\t商品价格:%-8.2lf\t当前剩余量:%-8d\n",n+1,(shop + n)->name, (shop + n)->desc, (shop + n)->price, (shop + n)->nums);
	}
	
}
//输出背包信息
void OutputBag(struct Bag* bag, int i)
{
	for (int n = 0; n < i; n++)
	{
		printf_s("物品编号%2d的名称为:%2s\t物品描述:%-8s\t当前拥有量:%d\n", n + 1, (bag + n)->name, (bag + n)->desc,  (bag + n)->nums);
	}
}
//购买功能
void Buy(struct Shop* shop, struct Bag* bag)
{
	double* p_money = &money;
	printf_s("您目前拥有游戏币%.2lf个,请输入需要购买的商品编号,以及购买的数量:", *p_money);
	int index = 0;
	int num = 0;
	std::cin >> index;
	index--;
	std::cin >> num;
	double Buy_result;//购买结果
	Buy_result = money - ((shop + index)->price) * num;
	if (Buy_result < 0)
	{
		printf_s("您的游戏币不足以购买此物品\n");
	}
	else
	{
		*p_money = Buy_result;//扣除相应游戏币
		(shop + index)->nums -= num;//扣除商城该物品的库存
		(bag + index)->name = (shop + index)->name;//增加背包相应物品
		(bag + index)->desc = (shop + index)->desc;//增加背包相应物品的描述
		(bag + index)->nums += num;//增加背包相应物品的数量
		printf_s("购买成功!您还有%.2lf个游戏币", *p_money);
	}
}
int main()
{
	Init(bag, 12);//初始化背包
	InputShop(shop, 12);//初始化商品内容
	OutputShop(shop, 12);//打印商品内容
	Buy(shop, bag);
	OutputBag(bag,12);
	OutputShop(shop, 12);
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值