C++ 出纳程序
这是一个出纳计划,我迫切需要指导。该程序计算一个项目的成本和为该项目支付的金额,并计算要返还给客户的剩余“变化”。然而,我有几个问题我还没有解决。第一个是,在某些金额上,exp:cost=0.25,paid=100.00,程序会在没有零钱的地方吐出一分钱。第二个问题是为cout提供的参数:只有零钱(二十美元、十美元、五美元、一美元)和硬币(四分之一、一角硬币、五分镍币、一便士)只有在零钱中存在时才能使用。没有0个20个10个等。。。 所需示例:成本=0.25,已付=100.00将是4二十(s)1十(s)1五(s) 4一(s)三个季度;一角硬币、五分镍币和便士不应显示,因为它们是0。我是否只剩下大量的If语句,或者我还能做些什么;也许换一下?我不确定,因为我还是个新手 源代码:C++ 出纳程序,c++,payment,balance,C++,Payment,Balance,这是一个出纳计划,我迫切需要指导。该程序计算一个项目的成本和为该项目支付的金额,并计算要返还给客户的剩余“变化”。然而,我有几个问题我还没有解决。第一个是,在某些金额上,exp:cost=0.25,paid=100.00,程序会在没有零钱的地方吐出一分钱。第二个问题是为cout提供的参数:只有零钱(二十美元、十美元、五美元、一美元)和硬币(四分之一、一角硬币、五分镍币、一便士)只有在零钱中存在时才能使用。没有0个20个10个等。。。 所需示例:成本=0.25,已付=100.00将是4二十(s)1
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
float purchase, payment, change;
int new_change, one_dollar, five_dollar, ten_dollar, twen_dollar;
int quarter, nickel, dime, penny;
// Program explanation
cout << "This program calculates the change of a payment\n";
cout << "towards a purchase." << endl << endl;
// user prompt to input item cost and
cout << "- - Enter the following Information - -" << endl;
cout << endl << "Cost of Item: $";
cin >> purchase;
cout << endl << endl << "Payment: $";
cin >> payment;
// calculating the balance that is owed back to customer
change = (payment - purchase) * 100;
// this assignment used so that we can use modulo
new_change = ceil(change);
// Following equations used to retrieve respective bill
// amounts owed back to purchaser
twen_dollar = new_change / 2000;
ten_dollar = (new_change % 2000) / 1000;
five_dollar = (new_change % 1000) / 500;
one_dollar = (new_change % 500) / 100;
// Following equations used to retrieve respective coin
// amounts owed back to purchaser
quarter = (new_change % 100) / 25;
dime = (new_change % 25) / 10;
nickel = (new_change % 10) / 5;
penny = new_change % 5;
cout << endl << change << " " << new_change << " " << twen_dollar << "
twenty " << ten_dollar << " ten ";
cout << five_dollar << " five " << one_dollar << " one " << quarter;
cout << " quarter " << dime << " dime " << nickel << " nickel ";
cout << penny << " penny ";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
浮动采购、支付、变更;
零钱,一元,五元,十元,两元;
四分之一,五分之一,一角,一便士;
//程序说明
cout当您发出更改时,您不会从更改的值中减去它。例如,尝试使用输入跟踪您的代码购买:$0.25,付款:$100。当您到达该行时
nickel = (change%10) /5;
你已经用3个季度支付了差额,但是计算机不知道bc的零钱值仍然是9975.9975%10=5,5/5=1,所以你得到一个五分镍币。为了避免这一情况,每当你计算要返还的零钱金额时,就这样从总零钱中减去它
ten_dollar = change/1000;
change -= 1000*ten_dollar;
five_dollar = change /500;
change -= 500*five_dollar;
以下是我的代码工作版本:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
float purchase, payment;
int change, one_dollar, five_dollar, ten_dollar, twen_dollar;
int quarter, nickel, dime, penny;
// Program explanation
cout << "This program calculates the change of a payment\n";
cout << "towards a purchase." << endl << endl;
// user prompt to input item cost and
cout << "- - Enter the following Information - -" << endl;
cout << endl << "Cost of Item: $";
cin >> purchase;
cout << endl << endl << "Payment: $";
cin >> payment;
// calculating the balance that is owed back to customer
change = (payment - purchase)*100;
cout<<"$"<<(float)change/100<<endl;
// this assignment used so that we can use modulo
//don't need this, change is an int at this point
//new_change = ceil(change);
// Following equations used to retrieve respective bill
// amounts owed back to purchaser
twen_dollar = change / 2000;
change -= 2000*twen_dollar;
ten_dollar = change / 1000;
change -= 1000*ten_dollar;
five_dollar = change / 500;
change -= 500*five_dollar;
one_dollar = change / 100;
change -= 100*one_dollar;
// Following equations used to retrieve respective coin
// amounts owed back to purchaser
quarter = change / 25;
change -= 25*quarter;
dime = change / 10;
change -= 10*dime;
nickel = change / 5;
change -= 5*nickel;
penny = change;
cout << twen_dollar << "twenty " << ten_dollar << " ten ";
cout << five_dollar << " five " << one_dollar << " one " << quarter;
cout << " quarter " << dime << " dime " << nickel << " nickel ";
cout << penny << " penny ";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
浮动购买、支付;
零钱,一美元,五美元,十美元,两美元;
四分之一,五分之一,一角,一便士;
//程序说明
你能试着调试你的每一个问题吗?也许你想分割计算并编写一些自动化测试?这可能有助于你在不引入新错误的情况下一个一个地修复错误。你可能想用你的编程语言标记你的问题,以针对正确的读者。下面的阅读可能会也没有帮助。