diff --git a/Uebung 5/Uebung5_3/backpack.cpp b/Uebung 5/Uebung5_3/backpack.cpp index c5c7ca5..b7e1f67 100644 --- a/Uebung 5/Uebung5_3/backpack.cpp +++ b/Uebung 5/Uebung5_3/backpack.cpp @@ -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; diff --git a/Uebung 5/Uebung5_3/backpack.h b/Uebung 5/Uebung5_3/backpack.h index 0e35d48..e50b23f 100644 --- a/Uebung 5/Uebung5_3/backpack.h +++ b/Uebung 5/Uebung5_3/backpack.h @@ -25,7 +25,7 @@ struct Backpack { : maxWeight(maxWeight), availableItems(availableItems), currentWeight(0){}; void sortAvailableItems(); - void greedyPack(); + void greedyPack(bool continuous); int getValuePacked(); void printItems(); }; \ No newline at end of file diff --git a/Uebung 5/Uebung5_3/main.cpp b/Uebung 5/Uebung5_3/main.cpp index 239bea7..69de776 100644 --- a/Uebung 5/Uebung5_3/main.cpp +++ b/Uebung 5/Uebung5_3/main.cpp @@ -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 } \ No newline at end of file