习题4
完成程序:药品物资管理
要求:
1.利用结构体来存储目前拥有的药物的名称、种类、数量、买入价格、卖出价格。
2.利用枚举来设置药物的种类(回复mp和回复hp)。
3.编写函数来控制药物的买入和卖出,卖出价为买入价格的3/4。
4.编写函数来显示拥有的药物的剩余钱数。
5.通过输入数字来控制函数调用。
6.实现分离式编译。
//drug.h
#pragma once
#ifndef DRUG_H_
#define DRUG_H_
#include <string>
using namespace std;
enum Type {PlusHP,PlusMP};
struct Drug
{
string name;
Type type;
int count;
float buyPrice;
float sellPrice;
};
const float ratio = 0.75f;
constexpr float sellPrice(Drug &d) { return d.buyPrice*ratio; }
void buyDrug(Drug &d, float& money, int num);
void sellDrug(Drug &d, float&money, int num);
void display(const Drug &d1, const Drug &d2, const float money);
string showType(const Drug &d);
#endif // !DRUG_H_
//drug.cpp
#include "pch.h"
#include "drug.h"
#include <iostream>
void buyDrug(Drug &d, float& money, int num)
{
if (money > d.buyPrice * num)
{
money -= d.buyPrice*num;
d.count += num;
cout << "购买成功!" << endl;
}
else
{
cout << "警告:拥有的钱不足够购买" << num << "个药品!!!" << endl;
}
}
void sellDrug(Drug &d, float&money, int num)
{
if (d.count >= num)
{
d.count -= num;
money += d.sellPrice * num;
cout << "卖出成功!" << endl;
}
else
{
cout << "警告:没有" << num << "个药物可以售卖!!!" << endl;
}
}
void display(const Drug &d1, const Drug &d2, const float money)
{
cout << "目前拥有的药物:" << endl;
cout << "1:名称:" << d1.name << " 数量:" << d1.count << " 种类:" << showType(d1) << " 购入价格:" << d1.buyPrice << " 卖出价格:" << d1.sellPrice << endl;
cout << "1:名称:" << d2.name << " 数量:" << d2.count << " 种类:" << showType(d2) << " 购入价格:" << d2.buyPrice << " 卖出价格:" << d2.sellPrice << endl;
cout << "拥有钱数:" << money << endl;
cout << "显示完成!" << endl;
}
string showType(const Drug &d)
{
switch (d.type)
{
case 0:
return "PlusHP";
break;
case 1:
return "PlusMP";
break;
default:
break;
}
}
//习题4.cpp
#include "pch.h"
#include "drug.h"
#include <iostream>
int main()
{
Drug mpDrug = { "回魔药水",PlusMP,10,150,sellPrice(mpDrug) };
Drug hpDrug = { "回血药水",PlusHP,20,100,sellPrice(hpDrug) };
float totalMoney = 1000;
cout << "1:购买回血药品 / 2:购买回魔药品 / 3:卖出回血药品 / 4:卖出回魔药品 / 5:输出目前拥有的药水和金钱 / 6:退出" << endl;
cout << "请输入操作:" << endl;
int input = 0;
int num = 0;
while (cin >> input && input > 0 && input < 6)
{
if (input == 1)
{
cout << "请输入购买数量:" << endl;
if (cin >> num && num > 0)
{
buyDrug(hpDrug, totalMoney, num);
cout << "请继续输入操作:" << endl;
}
else
{
cout << "输入错误,请重新输入:" << endl;
}
}
else if (input == 2)
{
cout << "请输入购买数量:" << endl;
if (cin >> num && num > 0)
{
buyDrug(mpDrug, totalMoney, num);
cout << "请继续输入操作:" << endl;
}
else
{
cout << "输入错误,请重新输入:" << endl;
}
}
else if (input == 3)
{
cout << "请输入卖出数量:" << endl;
if (cin >> num && num > 0)
{
sellDrug(hpDrug, totalMoney, num);
cout << "请继续输入操作:" << endl;
}
else
{
cout << "输入错误,请重新输入:" << endl;
}
}
else if (input == 4)
{
cout << "请输入卖出数量:" << endl;
if (cin >> num && num > 0)
{
sellDrug(mpDrug, totalMoney, num);
cout << "请继续输入操作:" << endl;
}
else
{
cout << "输入错误,请重新输入:" << endl;
}
}
else
{
display(hpDrug, mpDrug, totalMoney);
cout << "请继续输入操作:" << endl;
}
}
return 0;
}
//自写:
//Medicine.h
#pragma once
#ifndef MEDICINE_H_
#define MEDICINE_H_
#include <iostream>
#include <string>
using namespace std;
enum Sort :short
{
PlusHP=0,
PlusMP=1
};
struct Medicine
{
string name;
int count;
Sort sort;
float buying_price;
float selling_price;
};
void BuyMedicine(Medicine& med, float& money);
void SellMedicine(Medicine& med, float& money);
void DisplayAllMedicineAndMoney(Medicine med1,Medicine med2, const float money);
#endif // !MEDICINE_H_
//Medicine.cpp
#include"pch.h"
#include "Medicine.h"
void BuyMedicine(Medicine& med, float& money)
{
int buyCount;
cout << "请输入购买数量:" << endl;
cin >> buyCount;
float totalPrice = buyCount * med.buying_price;
if (totalPrice < money && buyCount > 0)
{
money -= totalPrice;
med.count += buyCount;
cout << "购买成功!" << endl;
}
else
{
cout << "购买失败" << endl;
}
}
void SellMedicine(Medicine& med, float& money)
{
int sellCount;
cout << "请输入卖出数量:" << endl;
cin >> sellCount;
if (med.count >= sellCount && sellCount > 0)
{
money += sellCount * med.selling_price;
med.count -= sellCount;
cout << "卖出成功!" << endl;
}
else
{
cout << "卖出失败" << endl;
}
}
void DisplayAllMedicineAndMoney(Medicine med1, Medicine med2, const float money)
{
cout << "目前拥有的药品:" << endl;
cout << "1:名称:" << med1.name << " 数量:" << med1.count << " 种类:" << (med1.sort == 0 ? "PlusHP" : "PlusMP") << " 购入价格:" << med1.buying_price << " 卖出价格:" << med1.selling_price << endl;
cout << "2:名称:" << med2.name << " 数量:" << med2.count << " 种类:" << (med2.sort == 1 ? "PlusMP" : "PlusHP") << " 购入价格:" << med2.buying_price << " 卖出价格:" << med2.selling_price << endl;
cout << "拥有钱数:" << money << endl;
cout << "显示完成!" << endl;
}
//习题4.cpp
#include "pch.h"
#include "Medicine.h"
int main()
{
Medicine HP_Return = { "回血药水",20,PlusHP,100,75 };
Medicine MP_Return = { "回魔药水",10,PlusMP,150,112.5 };
float money = 1000.0f;
cout << "1.购买回血药品 / 2.购买回魔药品 / 3.卖出回血药品 / 4.卖出回魔药品 / 5.输出目前拥有的药水和金钱 / 6.退出" << endl;
bool isContinue = true;
int inputNum;
while (isContinue)
{
cout << "请输入操作:" << endl;
cin >> inputNum;
switch (inputNum)
{
case 1:
BuyMedicine(HP_Return, money);
break;
case 2:
BuyMedicine(MP_Return, money);
break;
case 3:
SellMedicine(HP_Return, money);
break;
case 4:
SellMedicine(MP_Return, money);
break;
case 5:
DisplayAllMedicineAndMoney(HP_Return, MP_Return, money);
break;
case 6:
isContinue = false;
break;
default:
break;
}
}
}