我正在尝试使自动售货机输出硬币的总金额,从四分之一开始,直到机器用完每枚硬币时都下降到零钱(随机确定机器中每种硬币的数量)原来)。我想我已经正确设置了所有主体,但是不确定如何执行此问题的数学运算。任何帮助将不胜感激
machine_quarters = rand() % 10;
machine_dimes = rand() % 10;
machine_nickels = rand() % 10;
cout << machine_quarters << endl;
cout << machine_nickels << endl;
cout << machine_dimes << endl;
cout << "(1) Lifesavers: $1.00\n";
cout << "(2) Juicy Fruit: $1.25\n";
cout << "(3) Big Red: $1.50\n";
cout << "(4) Lay's: $2.00\n";
cout << "(5) Dorito's: $2.25\n";
cout << "(6) Cheeto's: $2.75\n";
cout << "(7) Snickers: $3.00\n";
cout << "(8) Reese's: $3.25\n";
cout << "(9) Kit-Kat: $3.50\n";
cout << "\nInput Quarters: ";
cin >> quarter_input;
cout << "Input Dimes: ";
cin >> dime_input;
cout << "Input Nickels: ";
cin >> nickel_input;
quarter_total = quarter_input * quarter;
dime_total = dime_input * dime;
nickel_total = nickel_input * nickel;
money_input = quarter_total + dime_total + nickel_total;
quarter_change = quarter_input - machine_quarters;
dime_change = dime_input - machine_dimes;
nickel_change = nickel_input - machine_nickels;
cout << "\nMake Selection: ";
cin >> customerChoice;
switch (customerChoice)
{
case 1:
total_change = money_input - lifesavers;
cout << "Your Change is: $" << total_change << endl;
if (machine_quarters != 0 && machine_dimes != 0 && machine_dimes != 0)
{
}
else if (machine_quarters == 0 && machine_dimes != 0 && machine_nickels != 0)
{
}
else if (machine_quarters != 0 && machine_dimes == 0 && machine_nickels != 0)
{
}
else if (machine_quarters != 0 && machine_dimes != 0 && machine_nickels == 0)
{
}
else if (machine_quarters == 0 && machine_dimes == 0 && machine_nickels == 0)
{
cout << "Change Cannot Be Given...\n" << "Amount Returned: $" << money_input;
}
else if (total_change == 0)
{
break;
}
break;
我期望输出将显示: 您的更改是:$ X.XX 返回的宿舍:X 返回的镍:X 毛钱返回:X
答案 0 :(得分:0)
进行更改的算法
为了将逻辑放在一起,您必须提出一种选择硬币的策略。从每种最大的硬币类型中逐一选择硬币,直到耗尽该类型,然后进行下一个硬币类型,这才是合理的。
您可以通过多种方式来执行此操作,但是最简单(但不是最有效)的方法是仅保留季度,直到您用完季度或剩下的更改小于的值为止。四分之一,然后下降到零点。
我以美分存储所有值,但是在打印时返回双倍值。我尝试为处理美分的任何变量或函数添加_cents以避免混淆。
因此,基本逻辑包含在make_change_cents()函数中
bool make_change(const uint price_cents,
const uint proffered_cents,
uint &change_cents,
uint machine_quarters,
uint machine_dimes,
uint machine_nickels,
uint machine_pennies,
uint &quarters,
uint &dimes,
uint &nickels,
uint &pennies)
{
quarters = dimes = nickels = pennies = 0;
change_cents = proffered_cents - price_cents;
uint remaining_to_be_returned = change_cents;
while (remaining_to_be_returned >= QUARTER_VALUE && machine_quarters > 0) {
remaining_to_be_returned -= QUARTER_VALUE; // subtract value of 1 quarter
quarters++; // increment # to be returned
machine_quarters--; // decrement # remaining
}
while (remaining_to_be_returned >= DIME_VALUE && machine_dimes > 0) {
remaining_to_be_returned -= DIME_VALUE; // subtract value of 1 quarter
dimes++; // increment # to be returned
machine_dimes--; // decrement # remaining
}
while (remaining_to_be_returned >= NICKEL_VALUE && machine_nickels > 0) {
remaining_to_be_returned -= NICKEL_VALUE; // subtract value of 1 quarter
nickels++; // increment # to be returned
machine_nickels--; // decrement # remaining
}
while (remaining_to_be_returned >= PENNY_VALUE && PENNY_VALUE > 0) {
remaining_to_be_returned -= PENNY_VALUE; // subtract value of 1 quarter
pennies++; // increment # to be returned
machine_pennies--; // decrement # remaining
}
if (remaining_to_be_returned > 0) {
return false;
} else {
return true;
}
}
您说过数学有问题。此函数提供了一种处理数学的简单算法。
您说您已经编写了其余代码。即使这样,我还是继续写了一个实现来确保我的make_change_cents()函数正常工作。这是我其余代码的版本(如果您有任何疑问,欢迎与我联系,以引起堆栈溢出!):
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <tuple>
#include <vector>
/// make_change()
/// price_cents -- in
/// proffered_cents -- in
/// machine_quarters,-- in
/// machine_dimes, -- in
/// machine_nickels-- in
/// machine_pennies-- in
/// quarters --out
/// dimes --out
/// nickels --out
/// pennies --out
/// returns false if not able to make change
/// returns true if able to make change.
const uint QUARTER_VALUE{25};
const uint DIME_VALUE{10};
const uint NICKEL_VALUE{5};
const uint PENNY_VALUE{1};
std::vector<std::tuple<uint, uint, std::string>> tuples
{{1, 100, "Lifesavers"}, {2, 125, "Juicy Fruit"}, {3, 150, "Big Red"},
{4, 200, "Lay's"}, {5, 225, "Dorito's"}, {6, 275, "Cheeto's"},
{7, 300, "Snickers"}, {8, 325, "Reese's"}, {9, 350, "Kit-Kat"},};
bool make_change(const uint price_cents,
const uint proffered_cents,
uint &change_cents,
uint machine_quarters,
uint machine_dimes,
uint machine_nickels,
uint machine_pennies,
uint &quarters,
uint &dimes,
uint &nickels,
uint &pennies)
{
quarters = dimes = nickels = pennies = 0;
change_cents = proffered_cents - price_cents;
uint remaining_to_be_returned = change_cents;
while (remaining_to_be_returned >= QUARTER_VALUE && machine_quarters > 0) {
remaining_to_be_returned -= QUARTER_VALUE; // subtract value of 1 quarter
quarters++; // increment # to be returned
machine_quarters--; // decrement # remaining
}
while (remaining_to_be_returned >= DIME_VALUE && machine_dimes > 0) {
remaining_to_be_returned -= DIME_VALUE; // subtract value of 1 quarter
dimes++; // increment # to be returned
machine_dimes--; // decrement # remaining
}
while (remaining_to_be_returned >= NICKEL_VALUE && machine_nickels > 0) {
remaining_to_be_returned -= NICKEL_VALUE; // subtract value of 1 quarter
nickels++; // increment # to be returned
machine_nickels--; // decrement # remaining
}
while (remaining_to_be_returned >= PENNY_VALUE && PENNY_VALUE > 0) {
remaining_to_be_returned -= PENNY_VALUE; // subtract value of 1 quarter
pennies++; // increment # to be returned
machine_pennies--; // decrement # remaining
}
if (remaining_to_be_returned > 0) {
return false;
} else {
return true;
}
}
void generate_machine_reserves(uint &machine_quarters,
uint &machine_dimes,
uint &machine_nickels,
uint &machine_pennies)
{
/* initialize random seed: */
srand(time(NULL));
machine_quarters = rand() % 10; // range is 0 - 9
machine_dimes = rand() % 10;
machine_nickels = rand() % 10;
}
void display_choices()
{
int i = 1;
for (const auto &element: tuples) {
std::cout << "(" << std::get<0>(element) << ") " << std::get<2>(element)
<< std::fixed << ": $" << std::setprecision(2)
<< ((double) std::get<1>(element) / 100) << "\n";
}
}
uint get_choice()
{
uint choice = 0;
std::string input_line;
while (choice < 1 || choice > 9) {
std::cout << "Please provide a choice between 1 and 9:\n\n";
std::getline(std::cin, input_line);
std::istringstream ss(input_line);
ss >> choice;
}
return choice;
}
// Return submitted payment amount in cents
uint get_payment_amount_cents()
{
std::string input_line;
double parsed_double = -1;
uint retval = 0;
std::cout << "Please provide the payment amount you are submitting as\n"
"a decimal number in dollars. This machine cannot return\n"
"dollar bills. This machine only returns change in\n"
"quarters, dimes, nickels, and pennies, as long as the\n"
"total to be returned does not exceed the amount of change\n"
"on hand. \n";
while (parsed_double <= 0) {
getline(std::cin, input_line);
std::stringstream ss(input_line);
ss >> parsed_double;
if (parsed_double <= 0) {
std::cout
<< "Please try again. Your amount was not an acceptable number\n";
}
}
return (uint) (parsed_double * 100);
}
int main()
{
uint machine_quarters;
uint machine_dimes;
uint machine_nickels;
uint machine_pennies;
uint choice = 0;
uint quarters;
uint dimes;
uint nickels;
uint pennies;
uint change_cents;
generate_machine_reserves(machine_quarters,
machine_dimes,
machine_nickels,
machine_pennies);
display_choices();
choice = get_choice();
change_cents = get_payment_amount_cents();
std::cout << "machine_quarters: " << machine_quarters << std::endl;
std::cout << "machine_dimes: " << machine_dimes << std::endl;
std::cout << "machine_nickels: " << machine_nickels << std::endl;
std::cout << "machine_pennies: " << machine_pennies << std::endl;
if (make_change(std::get<1>(tuples[choice
- 1]), // 1-based choices in 0-based vector
change_cents,
change_cents,
machine_quarters,
machine_dimes,
machine_nickels,
machine_pennies,
quarters,
dimes,
nickels,
pennies)) {
std::cout << "Your Change is: $" << std::setprecision(2) << std::fixed
<< (double) change_cents / 100 <<
std::endl;
std::cout << "quarters: " << quarters << std::endl;
std::cout << "dimes: " << dimes << std::endl;
std::cout << "nickels: " << nickels << std::endl;
std::cout << "pennies: " << pennies << std::endl;
} else {
std::cout << "Sorry, change cannot be given at this time.\n";
}
}