首页 > 解决方案 > 收银员计划

问题描述

这是我迫切需要指导的收银员计划。该程序获取一个项目的成本和为该项目支付的金额,并计算要返还给客户的剩余“零钱”。但是,我遇到了一些我无法解决的问题。第一个是在一定的金额上,exp: cost= 0.25,paid= 100.00,程序吐出一个 nickle,而应该没有 nickle 的变化。第二个问题是为 cout 提供的参数:只有个别美元(二十、十、五、一)和硬币(25 美分、15 美分、5 美分、1 美分)存在于零钱中时才可计算。否 0 20 0 10 等... 所需示例:成本 = 0.25,已支付 = 100.00 将是 4 二十(s) 1 十(s) 1 五(s) 4 一(s) 3 个季度;硬币,镍和便士不应该显示,因为它们是 0。我是否只剩下大量的 If 语句,或者还有其他我可以做的事情吗?也许切换?我不确定,因为我还是个新手。

源代码:

#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;
}

cout 格式不是最终格式,我在 cout 上显示了“change”和“new_change”,因为在“new_change = change”分配期间我遇到了数字减少的另一个问题;ceil(change) 似乎已经修复了它。欢迎任何建议和批评,只要它们具有建设性。感谢您的时间。

标签: c++paymentbalance

解决方案


当你分发零钱时,你并没有从零钱的价值中减去它。例如,尝试使用输入 purchase:$0.25 和 payment:$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;
}

推荐阅读