Should work now
This commit is contained in:
parent
155a7f282c
commit
b4f36b5b7a
|
|
@ -1,4 +1,4 @@
|
||||||
#include "cashInstance.h"
|
#include "CashInstance.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void CashInstance::print() {
|
void CashInstance::print() {
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,10 @@ public:
|
||||||
void print();
|
void print();
|
||||||
CashInstance(float value, int availableAmount)
|
CashInstance(float value, int availableAmount)
|
||||||
: value(value), availableAmount(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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
#include "Drone.h"
|
||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// #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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
#pragma once
|
||||||
|
#include "CashInstance.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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){};
|
||||||
|
};
|
||||||
|
|
@ -4,7 +4,7 @@ BINARY = main
|
||||||
# Name of the binary for Release
|
# Name of the binary for Release
|
||||||
FINAL = prototyp
|
FINAL = prototyp
|
||||||
# Object files
|
# Object files
|
||||||
OBJS = cashInstance.o main.o
|
OBJS = CashInstance.o Drone.o main.o
|
||||||
# Compiler flags
|
# Compiler flags
|
||||||
CFLAGS = -Werror -Wall -std=c++17 -fsanitize=address,undefined -g
|
CFLAGS = -Werror -Wall -std=c++17 -fsanitize=address,undefined -g
|
||||||
# Linker flags
|
# Linker flags
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,11 @@
|
||||||
|
#include "Drone.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
int main() { return 0; }
|
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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue