完成程序:药品物资管理
要求:1.利用结构体来存储目前用用的药物的名称,种类,数量,买入价格,卖出价格。
2.利用枚举来设置药物的种类(回复mp和回复hp)。
3.编写函数来控制药物的买入和卖出,卖出价格为买入价格的3/4。
4.编写函数来显示拥有的药物和剩余钱数。
5.通过输入数字来控制函数调用。
6.实现分离式编译。
头文件game.h
#pragma once
#ifndef GAME_H_
#define GAME_H_
#include "string"
using namespace std;
enum kind {hp,mp};
struct Bag
{
	string name;
	kind kin;
	int number;
	float buy;
	float sell;
};
int Buyin(Bag &b1,float res);
int Sellout(Bag &b1,float res);
void Display(Bag b1,int i);
#endif // !GAME_H_
game.cpp
#include <iostream>
#include "stdio.h"
#include "string"
#include "game.h"
int Buyin(Bag &b1, float res)
{
	float i = res;
	if (i < b1.buy)
	{
		cout << "金钱不足" << endl;
	}
	else
	{
		b1.number += 1;
		i -= b1.buy;
		cout << "购买一瓶药" << endl;
	}
	return i;
}
int Sellout(Bag &b1, float res)
{
	int i = res;
	if (b1.number< 0)
	{
		cout << "数量不足" << endl;
	}
	else
	{
		b1.number -= 1;
		i+= b1.sell;
		cout << "卖出一瓶药" << endl;
	}
	return i;
}
void Display(Bag b1,int i)
{
	if(i==0)
	cout << "名称:" << b1.name << "  " << "种类:" << "hp" << "  " << "数量:" << b1.number << "  " << "购买价格" << b1.buy << "  " << "出售价格" << b1.sell<<endl;
	else
	cout << "名称:" << b1.name << "  " << "种类:" << "mp" << "  " << "数量:" << b1.number << "  " << "购买价格" << b1.buy << "  " << "出售价格" << b1.sell << endl;
}
主文件
#include <iostream>
#include "stdio.h"
#include "string"
#include "game.h"
using namespace std;
int main()
{
	int select;
	float res = 1000;
	Bag bagh{ "hp药水",kind(0),5,100,75 };
	Bag bagm{ "mp药水",kind(1),5,100,75 };
	do {
		cout << "请输入对应指令" << endl << "1.购买hp药 2.卖出hp药 3.购买mp药 4.卖出mp药 5.显示背包 6.退出" << endl;
		cin >> select;
		switch (select)
		{
		case 1:
			res=Buyin(bagh, res);
			break;
		case 2:
			res = Sellout(bagh, res);
			break;
		case 3:
			res = Buyin(bagh, res);
			break;
		case 4:
			res = Sellout(bagm, res);
			break;
		case 5:
		{
			Display(bagh, bagh.kin);
			Display(bagm, bagm.kin);
			cout << "剩余金钱为:" << res << endl;
		}
		break;
		case 6:
			break;
		default:
			break;
		}
	}
	while (select != 6);
}