-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbag.cpp
More file actions
115 lines (103 loc) · 1.8 KB
/
bag.cpp
File metadata and controls
115 lines (103 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <algorithm>
#include "struct.cpp"
#include "bag.h"
Bag::Bag()
{
bag.clear();
}
Bag::Bag(vector<bags> ibag)
{
bag = ibag;
}
bool Bag::put(int name)
{
if(get_item_num(name) == 0) { // 檢查要新增的物品是否已在背包中
// 要新增的物品不在背包中
if(item_full() == false) { // 檢查背包格數是否已滿
bags blanck;
blanck.item = name;
blanck.quantity = 1;
bag.push_back(blanck);
return true;
} else {
// 背包格數已滿,無法新增
return false;
}
} else {
if(quantity_full(name) == false) { // 檢查背包此物品空間是否已滿
bag[get_item_order(name)].quantity++;
return true;
} else {
// 背包此物品空間已滿,無法新增
return false;
}
}
}
bool Bag::take(int name)
{
if(get_item_num(name) == 0) { // 檢查要取出的物品是否有在背包中
// 背包中無此物品,無法取出
return false;
} else if(get_item_num(name) == 1) {
bag.erase(bag.begin() + get_item_order(name));
return true;
} else {
bag[get_item_order(name)].quantity--;
return true;
}
}
int Bag::bag_size()
{
return bag.size();
}
vector<bags> *Bag::get_items()
{
return &bag;
}
bool Bag::item_full()
{
if(bag.size() >= 10) {
return true;
} else {
return false;
}
}
bool Bag::quantity_full(int name)
{
if(bag[get_item_order(name)].quantity >= 10) {
return true;
} else {
return false;
}
}
int Bag::get_item_num(int name)
{
for(int i=0; i<bag.size(); i++)
{
if(bag[i].item == name)
{
return bag[i].quantity;
}
}
return 0;
}
int Bag::get_order_item(int order)
{
if(order >= bag.size()) {
return 0;
} else {
return bag[order].item;
}
}
int Bag::get_item_order(int name)
{
for(int i=0; i<bag.size(); i++)
{
if(bag[i].item == name)
{
return i;
}
}
return -1;
}