1 #pragma once
2 #ifndef DRUG_H_
3 #define DRUG_H_
4 #include <string>
5 using namespace std;
6
7 enum Type
8 {
9 PlusHP,
10 PlusMP
11 };
12
13 class Drug
14 {
15 private:
16 string name_;
17 Type type_;
18 int count_;
19 float buyPrice_;
20 float sellPrice_;
21 static constexpr float Ratio = 0.75;
22 public:
23 Drug(string name,Type type,int count ,float buyPrice);
24 Drug();
25 ~Drug();
26 void buyDrug(float &money, int num);
27 void sellDrug(float &money, int num);
28 void showDrug();
29 string showType();
30 };
31
32
33 #endif // !DRUG_H_
1 #include "stdafx.h"
2 #include "drug.h"
3 #include <iostream>
4
5 Drug::Drug()
6 {
7 }
8
9 Drug::Drug(string name, Type type, int count, float buyPrice)
10 {
11 name_ = name;
12 type_=type;
13 count_=count;
14 buyPrice_ = buyPrice;
15 sellPrice_ = buyPrice * Ratio;
16 }
17
18 Drug::~Drug()
19 {
20 }
21
22 void Drug::buyDrug(float & money, int num)
23 {
24 if (num > 0)
25 {
26 if (buyPrice_*num <= money)
27 {
28 money -= buyPrice_ * num;
29 count_ += num;
30 cout << "购买成功!" << endl;
31 }
32 else
33 {
34 cout << "警告:您的钱不购买" << num << "个药物!" << endl;
35 }
36 }
37 else
38 {
39 cout << "警告:输入有误!" << endl;
40 }
41 }
42
43 void Drug::sellDrug(float & money, int num)
44 {
45 if (num > 0)
46 {
47 if (num <= count_)
48 {
49 count_ -= num;
50 money += sellPrice_ * num;
51 cout << "卖出成功!" << endl;
52 }
53 else
54 {
55 cout << "警告:您没有" << num << "个药物可以卖出!" << endl;
56 }
57 }
58 else
59 {
60 cout << "警告:输入有误!" << endl;
61 }
62
63 }
64
65 void Drug::showDrug()
66 {
67 cout << "药物名称:" << name_ << " 数量:" << count_ << " 种类:" << type_ << " 买入价格:" << buyPrice_ << " 卖出价格" << sellPrice_<< endl;
68 }
69
70 string Drug::showType()
71 {
72 if (type_ == 0)
73 {
74 return "PlusHP";
75 }
76 else
77 {
78 return "PlusMP";
79 }
80
81 }
1 // C++函数和类 26-习题5.cpp: 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include "drug.h"
6 #include <iostream>
7 //-------------------------习题5-----------------------------
8 //完成程序:药品物资管理
9 //要求:
10 //1.创建药品类来完成习题4中对药品相关的管理。
11 //2.利用对象数组来完成对不同种类药品的信息存储。
12 //3.使用*this指针。
13 //4.使用作用域为类的常量。
14
15 int main()
16 {
17 const int size = 2;
18 Drug drug[size];
19 drug[0] = Drug("回血药水", PlusHP, 20, 100); //使用自定义构造函数初始化
20 drug[1] = Drug("回魔药水", PlusMP, 10, 150);
21 float totalMoney = 1000;
22
23 cout << "1.购买回血药水/2.购买回魔药水/3.卖出回血药水/4.卖出回蓝药水/5.显示目前拥有的药水与金钱/6.退出" << endl;
24 cout << "请输入操作:" << endl;
25 int input = 0;
26 int number = 0;
27 while (cin >> input)
28 {
29 if (input == 1)
30 {
31 cout << "请输入购买的数量:" << endl;
32 if (cin >> number && number > 0)
33 {
34 drug[0].buyDrug(totalMoney, number);
35 cout << "请继续输入操作:" << endl;
36 }
37 }
38 else if (input == 2)
39 {
40 cout << "请输入购买的数量:" << endl;
41 if (cin >> number && number > 0)
42 {
43 drug[1].buyDrug(totalMoney, number);
44 cout << "请继续输入操作:" << endl;
45 }
46
47 }
48 else if (input == 3)
49 {
50 cout << "请输入卖出的数量:" << endl;
51 if (cin >> number && number > 0)
52 {
53 drug[0].sellDrug(totalMoney, number);
54 cout << "请继续输入操作:" << endl;
55 }
56 }
57 else if (input == 4)
58 {
59 cout << "请输入卖出的数量:" << endl;
60 if (cin >> number && number > 0)
61 {
62 drug[1].sellDrug(totalMoney, number);
63 cout << "请继续输入操作:" << endl;
64 }
65 }
66 else if (input == 5)
67 {
68 cout << "目前拥有的药品:" << endl;
69 for (int i = 0; i < size; i++)
70 {
71 cout << i + 1 << ": " << endl;
72 drug[i].showDrug();
73 }
74 cout << "拥有的金钱数: " << totalMoney << endl;
75 cout << "请继续输入操作:" << endl;
76 }
77 else if (input == 6)
78 {
79 break;
80 }
81 else
82 {
83 cout << "输入错误,请重新输入,或输入6退出!" << endl;
84 }
85
86 }
87 return 0;
88 }