diff --git a/Uebung 5/Uebung5_2/CashInstance.cpp b/Uebung 5/Uebung5_2/CashInstance.cpp index cb81add..b7d4e1f 100644 --- a/Uebung 5/Uebung5_2/CashInstance.cpp +++ b/Uebung 5/Uebung5_2/CashInstance.cpp @@ -1,4 +1,4 @@ -#include "cashInstance.h" +#include "CashInstance.h" #include void CashInstance::print() { diff --git a/Uebung 5/Uebung5_2/CashInstance.h b/Uebung 5/Uebung5_2/CashInstance.h index cd398e6..1d9dc14 100644 --- a/Uebung 5/Uebung5_2/CashInstance.h +++ b/Uebung 5/Uebung5_2/CashInstance.h @@ -7,4 +7,10 @@ public: void print(); CashInstance(float value, int availableAmount) : value(value), availableAmount(availableAmount){}; + bool operator<(const CashInstance &other) const { + return this->value < other.value; + } + bool operator>(const CashInstance &other) const { + return this->value > other.value; + } }; \ No newline at end of file diff --git a/Uebung 5/Uebung5_2/Drone.cpp b/Uebung 5/Uebung5_2/Drone.cpp new file mode 100644 index 0000000..5571e48 --- /dev/null +++ b/Uebung 5/Uebung5_2/Drone.cpp @@ -0,0 +1,47 @@ +#include "Drone.h" +#include +#include + +// #define DEBUG + +float Drone::calculateChange(float total, float paid) { return paid - total; } + +void Drone::returnChange(float change) { + float restChange = change; + std::cout << "Restbetrag: " << restChange << std::endl; + std::sort(availableCashInstances, availableCashInstances + CashInstanceSize, + std::greater<>()); + for (int i = 0; i < CashInstanceSize; i++) { + if (availableCashInstances[i].value >= restChange) { + } + while (restChange >= availableCashInstances[i].value && + availableCashInstances[i].availableAmount > 0) { +#ifdef DEBUG + std::cout << "restChange: " << restChange + << " Current Coin: " << availableCashInstances[i].value + << std::flush << std::endl; +#endif + availableCashInstances[i].availableAmount--; + restChange = + std::round((restChange - availableCashInstances[i].value) * 100) / + 100; + } + } + if (restChange > 0.009) { + for (int i = CashInstanceSize - 1; i >= 0; i--) { + if (availableCashInstances[i].value >= restChange && + availableCashInstances[i].availableAmount > 0) { + availableCashInstances[i].availableAmount--; + restChange = + std::round((restChange - availableCashInstances[i].value) * 100) / + 100; + std::cout << "Warnung: Rückgeld kann nicht genau herausgegeben werden! " + "Ausbezahlter Betrag überschreitet den genauen Wert um " + << restChange * (-1) << std::endl; + return; + } + } + std::cout << "Nicht genügend Restgeld vorhanden! Fehlender Betrag: " + << restChange << std::endl; + } +} \ No newline at end of file diff --git a/Uebung 5/Uebung5_2/Drone.h b/Uebung 5/Uebung5_2/Drone.h new file mode 100644 index 0000000..ad81751 --- /dev/null +++ b/Uebung 5/Uebung5_2/Drone.h @@ -0,0 +1,16 @@ +#pragma once +#include "CashInstance.h" +#include + +class Drone { +private: + uint16_t CashInstanceSize = 0; + +public: + CashInstance *availableCashInstances; + float calculateChange(float total, float paid); + void returnChange(float change); + Drone(CashInstance *availableCashInstances, uint16_t CashInstanceSize) + : CashInstanceSize(CashInstanceSize), + availableCashInstances(availableCashInstances){}; +}; \ No newline at end of file diff --git a/Uebung 5/Uebung5_2/Makefile b/Uebung 5/Uebung5_2/Makefile index 2e38b58..3d62b59 100644 --- a/Uebung 5/Uebung5_2/Makefile +++ b/Uebung 5/Uebung5_2/Makefile @@ -4,7 +4,7 @@ BINARY = main # Name of the binary for Release FINAL = prototyp # Object files -OBJS = cashInstance.o main.o +OBJS = CashInstance.o Drone.o main.o # Compiler flags CFLAGS = -Werror -Wall -std=c++17 -fsanitize=address,undefined -g # Linker flags diff --git a/Uebung 5/Uebung5_2/main.cpp b/Uebung 5/Uebung5_2/main.cpp index b90e9c8..6d0056d 100644 --- a/Uebung 5/Uebung5_2/main.cpp +++ b/Uebung 5/Uebung5_2/main.cpp @@ -1,3 +1,11 @@ +#include "Drone.h" - -int main() { return 0; } \ No newline at end of file +int main() { + CashInstance wallet[8] = {CashInstance(0.01, 0), CashInstance(0.02, 0), + CashInstance(0.05, 0), CashInstance(0.1, 2), + CashInstance(0.2, 0), CashInstance(0.5, 10), + CashInstance(1.0, 4), CashInstance(2, 4)}; + Drone drone(wallet, 8); + drone.returnChange(drone.calculateChange(5, 20)); + return 0; +} \ No newline at end of file