//习题5.cpp
#include "pch.h"
#include "Drugs.h"
#include <iostream>
int main()
{
const int size = 2;
Drugs drugs[size];
drugs[0] = Drugs("回血药水", PlusHP, 20, 100);
drugs[1] = Drugs("回魔药水", PlusMP, 10, 150);
float totalMoney = 1000;
cout << "1.购买回血药品 / 2.购买回魔药品 / 3.卖出回血药品 / 4.卖出回魔药品 / 5.输出目前拥有的药水和金钱 / 6.退出" << endl;
cout << "请输入操作:" << endl;
int input = 0;
int number = 0;
while (cin >> input) //&& input > 0 && input < 7
{
if (input == 1)
{
cout << "请输入购买的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[0].buyDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 2)
{
cout << "请输入购买的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[1].buyDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 3)
{
cout << "请输入卖出的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[0].sellDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 4)
{
cout << "请输入卖出的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[1].sellDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 5)
{
cout << "目前拥有的药品:" << endl;
for (int i = 0; i < size; i++)
{
cout << i + 1 << ":" << endl;
drugs[i].showDrug();
}
cout << "拥有的金钱数:" << totalMoney << endl;
}
else if(input ==6)
{
break;
}
else
{
cout << "输入错误,请重新输入,或输入6退出!" << endl;
}
}
}
//自写
//Medicine.h
#pragma once
#ifndef MEDICINE_H_
#define MEDICINE_H_
#include <iostream>
#include <string>
using namespace std;
enum Sort {
PlusHP=0,
PlusMP=1
};
class Medicine
{
private:
string name;
Sort sort;
int count;
float buyPrice;
float sellPrice;
static constexpr float rate = 0.75f;
public:
Medicine(string name,int count ,Sort sort,float buyPrice);
~Medicine();
void ShowMedicineInfo();
void BuyMedicine(float &money);
void SellMedicine(float &money);
};
#endif // !MEDICINE_H_
//Medicine.cpp
#include "pch.h"
#include "Medicine.h"
Medicine::Medicine(string name, int count, Sort sort, float buyPrice)
{
this->name = name;
this->sort = sort;
this->count = count;
this->buyPrice = buyPrice;
this->sellPrice = this->buyPrice * rate;
}
Medicine::~Medicine()
{
}
void Medicine::ShowMedicineInfo()
{
cout << (this->sort + 1) << ".名称:" << this->name << " 数量:" << this->count << " 种类:" << (this->sort == 0 ? "PlusHP" : "PlusMP") << " 购入价格:" << this->buyPrice << " 卖出价格:" << this->sellPrice << endl;
}
void Medicine::BuyMedicine(float & money)
{
int buyCount;
cout << "请输入购买数量:" << endl;
cin >> buyCount;
if (buyCount <= 0)
{
return;
}
float buyTotalPrice = this->buyPrice * buyCount;
if (buyTotalPrice <= money)
{
money -= buyTotalPrice;
this->count += buyCount;
cout << "购买成功!" << endl;
}
else
{
cout << "购买失败,当前钱不够购买这么多药品。" << endl;
}
}
void Medicine::SellMedicine(float & money)
{
int sellCount;
cout << "请输入卖出数量:" << endl;
cin >> sellCount;
if (sellCount > 0 && sellCount <= this->count)
{
money += this->sellPrice *sellCount;
this->count -= sellCount;
cout << "卖出成功!" << endl;
}
else
{
cout << "卖出失败!" << endl;
}
}
//习题5.cpp
#include "pch.h"
#include "Medicine.h"
int main()
{
const int size = 2;
Medicine meds[size] = { {"回血药水",20,PlusHP,100}, {"回魔药水",10,PlusMP,150} };
float totalMoney = 1000.0f;
cout << "1.购买回血药品 / 2.购买回魔药品 / 3.卖出回血药品 / 4.卖出回魔药品 / 5.输出目前拥有的药水和金钱 / 6.退出" << endl;
bool isContinue = true;
while (isContinue)
{
int num;
cin >> num;
switch (num)
{
case 1:
meds[0].BuyMedicine(totalMoney);
break;
case 2:
meds[1].BuyMedicine(totalMoney);
break;
case 3:
meds[0].SellMedicine(totalMoney);
break;
case 4:
meds[1].SellMedicine(totalMoney);
break;
case 5:
cout << "目前拥有的药品:" << endl;
for (int i = 0; i < size; i++)
{
meds[i].ShowMedicineInfo();
}
cout << "拥有钱数:" << totalMoney << endl << "显示完成!" << endl;
break;
case 6:
isContinue = false;
return 0;
break;
default:
cout << "输入数字错误!" << endl;
break;
}
cout << "请继续输入操作:" << endl;
}
return 0;
}