Make continuous packing optional
This commit is contained in:
parent
8a95eb668b
commit
7dc503ca13
|
|
@ -28,14 +28,18 @@ int Backpack::getValuePacked() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* the algorithm to pack the backpack */
|
/* the algorithm to pack the backpack */
|
||||||
void Backpack::greedyPack() {
|
void Backpack::greedyPack(bool continuous) {
|
||||||
this->packedItems
|
this->packedItems
|
||||||
.clear(); /* resets the packedItems -> removes all elements! */
|
.clear(); /* resets the packedItems -> removes all elements! */
|
||||||
this->currentWeight = 0; /* ...and also sets the current weight to 0.0 */
|
this->currentWeight = 0; /* ...and also sets the current weight to 0.0 */
|
||||||
|
|
||||||
this->sortAvailableItems(); // sort the available items first!
|
this->sortAvailableItems(); // sort the available items first!
|
||||||
|
const char *text = "";
|
||||||
std::cout << "Backpack has been packed:\n"; // output
|
if (!continuous) {
|
||||||
|
text = "non-";
|
||||||
|
}
|
||||||
|
std::cout << "Backpack has been packed " << text
|
||||||
|
<< "continuously:\n"; // output
|
||||||
std::cout << "\t\tWeight\tValue\n"; // output
|
std::cout << "\t\tWeight\tValue\n"; // output
|
||||||
std::cout << "---------------------------------------------\n"; // output
|
std::cout << "---------------------------------------------\n"; // output
|
||||||
|
|
||||||
|
|
@ -52,7 +56,7 @@ void Backpack::greedyPack() {
|
||||||
<< this->availableItems[i].weight << "\t"
|
<< this->availableItems[i].weight << "\t"
|
||||||
<< this->availableItems[i].value
|
<< this->availableItems[i].value
|
||||||
<< "\t Factor: 1.00\n"; // output
|
<< "\t Factor: 1.00\n"; // output
|
||||||
} else {
|
} else if (continuous) {
|
||||||
float factor = (float)(this->maxWeight - this->currentWeight) /
|
float factor = (float)(this->maxWeight - this->currentWeight) /
|
||||||
this->availableItems[i].weight;
|
this->availableItems[i].weight;
|
||||||
factor = std::round(factor * 100) / 100;
|
factor = std::round(factor * 100) / 100;
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ struct Backpack {
|
||||||
: maxWeight(maxWeight), availableItems(availableItems),
|
: maxWeight(maxWeight), availableItems(availableItems),
|
||||||
currentWeight(0){};
|
currentWeight(0){};
|
||||||
void sortAvailableItems();
|
void sortAvailableItems();
|
||||||
void greedyPack();
|
void greedyPack(bool continuous);
|
||||||
int getValuePacked();
|
int getValuePacked();
|
||||||
void printItems();
|
void printItems();
|
||||||
};
|
};
|
||||||
|
|
@ -9,6 +9,7 @@ int main() {
|
||||||
{"7", 12, 10}, {"8", 15, 17}, {"9", 15, 20},
|
{"7", 12, 10}, {"8", 15, 17}, {"9", 15, 20},
|
||||||
{"10", 30, 20}};
|
{"10", 30, 20}};
|
||||||
Backpack bp((float)20,
|
Backpack bp((float)20,
|
||||||
items); // creation of the backpack, utilizing the constructor
|
items); // creation of the backpack, utilizing the constructor
|
||||||
bp.greedyPack(); // greedily pack backpack
|
bp.greedyPack(false); // greedily pack Backpack non-continuously
|
||||||
|
bp.greedyPack(true); // greedily pack Backpack continuously
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue