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