From cf6edcdff6835bd4d80dd647fbd5cec9a6392bf7 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Sun, 26 Jun 2022 15:44:14 +0200 Subject: [PATCH] PartialWeight and partialValue for continuous case --- Uebung 5/Uebung5_3/backpack.cpp | 8 ++++++-- Uebung 5/Uebung5_3/main.cpp | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Uebung 5/Uebung5_3/backpack.cpp b/Uebung 5/Uebung5_3/backpack.cpp index b7e1f67..64cd2a4 100644 --- a/Uebung 5/Uebung5_3/backpack.cpp +++ b/Uebung 5/Uebung5_3/backpack.cpp @@ -35,6 +35,8 @@ void Backpack::greedyPack(bool continuous) { this->sortAvailableItems(); // sort the available items first! const char *text = ""; + float partialValue = 0.0f; + float partialWeight = 0.0f; if (!continuous) { text = "non-"; } @@ -64,6 +66,8 @@ void Backpack::greedyPack(bool continuous) { << this->availableItems[i].weight << "\t" << this->availableItems[i].value << "\t Factor: " << factor << std::endl; + partialWeight = this->availableItems[i].weight * factor; + partialValue = this->availableItems[i].value * factor; break; } if ((this->currentWeight - this->maxWeight) == @@ -73,8 +77,8 @@ void Backpack::greedyPack(bool continuous) { } } std::cout << "---------------------------------------------\n"; // output - std::cout << "\tTotal:\t" << this->currentWeight << "\t" - << this->getValuePacked() << "\n"; // output + std::cout << "\tTotal:\t" << this->currentWeight + partialWeight << "\t" + << this->getValuePacked() + partialValue << "\n"; // output } void Backpack::printItems() { diff --git a/Uebung 5/Uebung5_3/main.cpp b/Uebung 5/Uebung5_3/main.cpp index 69de776..200f1d5 100644 --- a/Uebung 5/Uebung5_3/main.cpp +++ b/Uebung 5/Uebung5_3/main.cpp @@ -8,7 +8,7 @@ int main() { {"4", 8, 5}, {"5", 10, 10}, {"6", 11, 5}, {"7", 12, 10}, {"8", 15, 17}, {"9", 15, 20}, {"10", 30, 20}}; - Backpack bp((float)20, + Backpack bp((float)24, items); // creation of the backpack, utilizing the constructor bp.greedyPack(false); // greedily pack Backpack non-continuously bp.greedyPack(true); // greedily pack Backpack continuously