1 #pragma once
2
3 #ifndef DRUG_H_
4 #define DRUG_H_
5 #include <string>
6
7 using namespace std;
8
9 enum Type
10 {
11 plusHP,
12 plusMP,
13 };
14
15
16 struct Drug //药水属性
17 {
18 string name;
19 Type type;
20 int count;
21 float buyprice;
22 float sellPrice;
23 };
24
25 const float ratio = 0.75;
26 constexpr float sellPrice(Drug &d) { return d.buyprice*ratio; }
27 void buyDrug(Drug &d, float &money, int num);
28 void sellDrug(Drug &d, float &money, int num);
29 void display(const Drug &d1, const Drug &d2, const float money);
30 string showType(const Drug &d);
31 #endif
1 #include "stdafx.h"
2 #include "drug.h"
3 #include <iostream>
4
5 void buyDrug(Drug &d, float &money,int num)//药品结构的引用 拥有的钱数 购买药品的数量
6 {
7 if (money >= d.buyprice*num) //如果拥有钱数大于购买指定商品的总钱数
8 {
9 money -= d.buyprice*num;
10 d.count += num;
11 cout << "购买成功!" << endl;
12 }
13 else
14 {
15 cout << "警告:拥有的钱不足购买" << num << "个药物!" << endl;
16 }
17
18 }
19 void sellDrug(Drug &d, float &money, int num)
20 {
21 if (d.count >= num)
22 {
23 d.count -= num;
24 money += d.sellPrice*num;
25 cout << "卖出成功!" << endl;
26 }
27 else
28 {
29 cout << "警告:没有" << num << "个药物可以售卖!!" << endl;
30 }
31 }
32
33 void display(const Drug &d1, const Drug &d2, const float money)
34 {
35 cout << "目前拥有的药物:" << endl;
36 cout << "1:名称:" << d1.name << " 数量:" << d1.count << " 种类:" << showType(d1)
37 << " 购入价格:" << d1.buyprice << " 卖出价格:" << d1.sellPrice << endl;
38 cout << "2:名称:" << d2.name << " 数量:" << d2.count << " 种类:" << showType(d2)
39 << " 购入价格:" << d2.buyprice << " 卖出价格:" << d2.sellPrice << endl;
40 cout << " 拥有的钱数:" << money << endl;
41 cout << "显示完成!" << endl;
42 }
43 string showType(const Drug &d)
44 {
45 switch (d.type)
46 {
47 case 0:
48 return "PlusHP";
49 break;
50 case 1:
51 return "PlusMP";
52 break;
53 default:
54 break;
55 }
56 }
1 // C++函数和类 16-习题4.cpp: 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include "drug.h"
6 #include <iostream>
7
8 //完成程序:药品物资管理
9 //要求:
10 // 1.利用结构体来存储目前拥有的药物的名称、种类、数量、买入价格、卖出价格。
11 // 2.利用枚举来设置药物的种类(回复mp和回复hp)。
12 // 3.编写函数来控制药物的买入和卖出,卖出价为买入价格的3/4。
13 // 4.编写函数来显示拥有的药物和剩余钱数。
14 // 5.通过输入数字来控制函数调用。
15 // 6.实现分离式编译。
16 int main()
17 {
18 Drug mpDrug = { "回魔药水",plusMP,10,150,sellPrice(mpDrug) };
19 Drug hpDrug = { "回血药水",plusHP,20,100,sellPrice(hpDrug) };
20 float totalMoney = 1000;
21
22 cout << "1:购买回血药水 / 2:购买回魔药水 / 3:卖出回血药水 /4:卖出回魔药水 /5.显示目前拥有的药水与金钱的数量 /6:退出" << endl;
23 cout << "请输入操作:" << endl;
24 int input = 0;
25 int num = 0;
26 while (cin>>input&&input>0&&input<6)
27 {
28 if (input == 1)
29 {
30 cout << "请输入购买的数量:" << endl;
31 if (cin >> num && num > 0)
32 {
33 buyDrug(hpDrug, totalMoney, num);
34 cout << "请继续输入操作:" << endl;
35 }
36 else
37 {
38 cout << "输入错误,请重新输入:" << endl;
39 }
40 }
41 else if (input == 2)
42 {
43 cout << "请输入购买的数量:" << endl;
44 if (cin >> num && num > 0)
45 {
46 buyDrug(mpDrug, totalMoney, num);
47 cout << "请继续输入操作:" << endl;
48 }
49 else
50 {
51 cout << "输入错误,请重新输入:" << endl;
52 }
53 }
54 else if (input == 3)
55 {
56 cout << "请输入卖出数量:" << endl;
57 if (cin >> num && num > 0)
58 {
59 sellDrug(hpDrug, totalMoney, num);
60 cout << "请继续输入操作:" << endl;
61 }
62 else
63 {
64 cout << "输入错误,请重新输入:" << endl;
65 }
66 }
67 else if (input == 4)
68 {
69 cout << "请输入卖出数量:" << endl;
70 if (cin >> num && num > 0)
71 {
72 sellDrug(mpDrug, totalMoney, num);
73 cout << "请继续输入操作:" << endl;
74 }
75 else
76 {
77 cout << "输入错误,请重新输入:" << endl;
78 }
79 }
80 else
81 {
82 display(hpDrug, mpDrug, totalMoney);
83 cout << "请继续输入操作:" << endl;
84 }
85 }
86 return 0;
87 }