gpt4 book ai didi

c++ - 在杂货店找零钱

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:54 25 4
gpt4 key购买 nike

我是计算机科学的新手,目前正在学习 C++。

我们的任务是计算大量变化。比如说,我在杂货店找的零钱是 37.37 美元。该程序的输出会告诉我每张找零的钞票有多少(1 张 20 美元钞票、1 张 10 美元钞票、1 张 5 美元钞票、2 张 1 美元钞票、25 美分硬币、1 美分硬币和 2 便士硬币) .

我已经想出如何用以前作业中的硬币来做这件事,但现在这些都是整美元这一事实让我碰壁了。

我试过除以钞票面额,但我无法弄清楚(我在开头注释掉了 cout/cin 语句所以每次我测试程序我不必输入数字):

#include <iostream> 
using namespace std;

int main() {
double price, change;
int paymentQ, quarters, dimes, nickels, pennies,
twentyDollar, fiftyDollar,
fiveDollar, dollars, tenDollar, totalChange;


//cout << "What is price? " << endl;
//cin >> price
//cout << "Please insert cash or select payment type: " << endl;
//cin >> paymentQ;
//change = (paymentQ - price);

change = 37.37;

dollars = static_cast<int>(change);
fiveDollar =
tenDollar =
twentyDollar =
fiftyDollar =
{
dollars = static_cast<int>(change);
quarters = (((change - dollars) * 100) / 25);
dimes = (((change - dollars) * 100) - (quarters * 25)) / 10;
nickels = (((change - dollars) * 100) - (quarters * 25) - (dimes * 10)) / 5;
pennies = (((change - dollars) * 100) - (quarters * 25) - (dimes * 10) - (nickels * 5) + .5);
}

cout << "\nYour change is\n " << twentyDollar << " Twenty Dollar bill/s " << endl
<< tenDollar << " Ten Dollar Bill/s" << endl <<
fiveDollar << " Five Dollar Bill/s" << endl <<
dollars << " One Dollar Bill/s" << endl <<
quarters<< " Quarters "<< endl <<
dimes<< " dimes " << endl <<
nickels << " Nickels" << endl <<
pennies << " pennies "<< endl << endl;

return 0;
}

最佳答案

您分配面额变量的方式完全错误,甚至可能不应该编译。即使有,它也有很多重复,一般来说肯定不容易阅读。

尝试更像这样的东西:

#include <iostream> 
using namespace std;

int main() {
double price, paymentQ, change;
int quarters, dimes, nickels, pennies,
fiftyDollars, twentyDollars, tenDollars, fiveDollars, oneDollars;

//cout << "What is price? " << endl;
//cin >> price
//cout << "Please insert cash or select payment type: " << endl;
//cin >> paymentQ;
//change = (paymentQ - price);
change = 37.37;

// despite what cout shows, 37.37 is actually 37.369999999999997 and
// so would become 3736 instead of 3737 when multiplied by 100 and
// truncated as-is to an int, so it needs to be rounded up a little
// bit to account for that (see https://stackoverflow.com/questions/149033/) ...
pennies = (change * 100) + 0.1; // see https://ideone.com/wmlzlm for why this work...

fiftyDollars = pennies / 5000; pennies %= 5000;
twentyDollars = pennies / 2000; pennies %= 2000;
tenDollars = pennies / 1000; pennies %= 1000;
fiveDollars = pennies / 500; pennies %= 500;
oneDollars = pennies / 100; pennies %= 100;
quarters = pennies / 25; pennies %= 25;
dimes = pennies / 10; pennies %= 10;
nickels = pennies / 5; pennies %= 5;

cout << "\nYour change is " << change << "\n"
<< twentyDollars << " Twenty Dollar Bill/s\n"
<< tenDollars << " Ten Dollar Bill/s\n"
<< fiveDollars << " Five Dollar Bill/s\n"
<< oneDollars << " One Dollar Bill/s\n"
<< quarters << " Quarter/s\n"
<< dimes << " Dime/s\n"
<< nickels << " Nickel/s\n"
<< pennies << " Penny/s\n"
<< endl;

return 0;
}

输出:

Your change is 37.371 Twenty Dollar Bill/s1 Ten Dollar Bill/s1 Five Dollar Bill/s2 One Dollar Bill/s1 Quarter/s1 Dime/s0 Nickel/s2 Penny/s

Live Demo

关于c++ - 在杂货店找零钱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54714957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com