收银员计划

时间:2018-06-23 18:29:27

标签: c++ payment balance

这是我非常需要指导的收银员计划。该程序会计算一件商品的成本和该商品的付款金额,并计算出将要返还给客户的剩余“零钱”。但是,我遇到了一些无法解决的问题。首先是,在某些金额上,例如:成本= 0.25,已付款= 100.00,该程序会吐出一个零散的地方,该零钱应该没有零散的变化。第二个问题是cout的参数:只有零钱(包括零钱,二十,十,五,一)和硬币(四分之一,一角硬币,镍币,几美分)才具有cout。没有0二十0十等... 所需的示例:成本= 0.25,已支付= 100.00将是4个20(s)1个10(s)1个5(s) 4个季度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)似乎已经修复了它。只要有建设性,任何建议和批评都将受到欢迎。谢谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

发出变更时,并不是从变更值中减去变更。例如,尝试使用输入Purchase:$ 0.25和Payment:$ 100来跟踪代码。当你上线

nickel = (change%10) /5;

您已经用3个季度支付了差额,但是计算机不知道bc的变化值仍然是9975。9975%10 = 5、5 / 5 = 1,所以您得到了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;
}