#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <iomanip>
#include <string>
#include <utility>
#include <vector>
struct coin
{
coin(std::string name_, int value_) : value(value_), name(name_) {}
unsigned value;
std::string name;
};
bool operator<(const coin& lhs, const coin& rhs)
{
return lhs.value < rhs.value;
}
int main()
{
std::vector<std::pair<coin, int>> money = { //Coin data and amount
{{"Quarter", 25}, 0}, {{"Dime", 10}, 0}, {{"Nickel", 5}, 0}, {{"Penny", 1}, 0}
};
std::sort(money.begin(), money.end(), std::greater<std::pair<coin, int>>());
srand(time(nullptr));
unsigned change = rand() % 100;
std::cout << std::fixed << std::setprecision(2);
std::cout << "Change Due: $" << (change / 100.0) << "\n";
std::cout << "Coin Dispenser will dispense:\n";
if (change == 0) {
std::cout << "No Coins\n";
return 0;
}
for(auto& data: money) {
data.second = change / data.first.value;
change %= data.first.value;
}
for(const auto& data: money) {
if (data.second != 0)
std::cout << std::left << std::setw(11) <<
(" " + data.first.name + ":") << data.second <<
" ($" << (data.second * data.first.value / 100.0) << ")\n";
}
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8dmVjdG9yPgoKc3RydWN0IGNvaW4KewogICAgY29pbihzdGQ6OnN0cmluZyBuYW1lXywgaW50IHZhbHVlXykgOiB2YWx1ZSh2YWx1ZV8pLCBuYW1lKG5hbWVfKSB7fQogICAgdW5zaWduZWQgdmFsdWU7CiAgICBzdGQ6OnN0cmluZyBuYW1lOwp9OwoKYm9vbCBvcGVyYXRvcjwoY29uc3QgY29pbiYgbGhzLCBjb25zdCBjb2luJiByaHMpCnsKICAgIHJldHVybiBsaHMud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