习题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;
};
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;
}
}