how do you calculate the minimum-coin change for transaction?

人盡茶涼 提交于 2019-12-31 06:52:33

问题


Hey everyone. I have a question. I am working on Visual Basic Express and I am supposed to calculate the change from a transaction.

Now what code would I use? I have it partly working but its starting to get a little confusing.

Thank you.

For you guys who wanted more information:

Say I have one dollar and I go to the store to purchase something. I have to ask the user to put in the amount they spent and then calculate the change and print to the screen.

Then I am supposed to use the least number of quarters, dimes nickels and pennies and print it to screen.

Any help would be greatly appreciated.


回答1:


I'm going to go out on a limb here and assume the OP is talking about change as in money returned from a transaction.

If that's the case, then it's probably homework, so pseudo-code only.

The simplest first-attempt way of doing it is as follows. Let cost be the cost of the transaction and tendered be the amount of money handed over (both in cents), and let's further assume your economy only has dollar bills, quarters and pennies (to make my code smaller).

change = tendered - cost

if change < 0:
    print "Pay up some more cash, cheapskate!"
    stop

dollars = 0
quarters = 0
cents = 0

while change >= 100:
    dollars = dollars + 1
    change = change - 100

while change >= 25:
    quarters = quarters + 1
    change = change - 25

while change >= 1:
    cents = cents + 1
    change = change - 1

print dollars " dollar(s), " quarters " quarter(s), and " cents " cent(s)."

Now this can no doubt be made more efficient with the use of modulo and divide operators but I leave that as an exercise for the reader.


My suggestion is to sit down with a pencil and a bit of paper with the following columns (for handing over ten dollars for a two-dollar-and-ninety-three cent purchase):

tendered      cost    change   dollars  quarters     cents
--------  --------  --------  --------  --------  --------
    1000       293

and run through the code line by line in your head, using the current values from the paper and writing down the new values where they change.

This will greatly assist your understanding.


In response to your update:

I have one dollar and I go to the store to purchase something. I have to ask the user to put in the amount they spent and then calculate the change and print to the screen. Then I am supposed to use the least number of quarters, dimes, nickels and pennies then print it to screen.

That's remarkably similar to what I had above:

tendered = 100
input cost
cost = int (cost * 100)
change = tendered - cost
if change < 0:
    print "Pay up some more cash, cheapskate!"
    stop
print "Change is ", (format "$9.99", change / 100)

quarters = 0, dimes = 0, nickels = 0, pennies = 0

while change >= 25:
    quarters = quarters + 1
    change = change - 25

while change >= 10:
    dimes = dimes + 1
    change = change - 10

while change >= 5:
    nickels = nickels + 1
    change = change - 5

while change >= 1:
    pennies = pennies + 1
    change = change - 1

print quarters, " quarters"
print dimes   , " dimes"
print nickels , " quarters"
print pennies , " pennies"



回答2:


A more "advanced" way of automating the process would be:

Private Function CalculateChange(ByVal dAmount As Decimal) As Decimal()
    Dim arrNotesAvailable() As Decimal = {200D, 100D, 50D, 20D, 10D, 5D, 2D, 1D, 0.5D, 0.2D, 0.1D, 0.05D, 0.02D, 0.01D}
    Dim arrChangeAmounts(arrNotesAvailable.Length - 1) As Decimal

    For iIndex As Integer = 0 To arrNotesAvailable.Length - 1
        arrChangeAmounts(iIndex) = dAmount \ arrNotesAvailable(iIndex)
        dAmount = dAmount Mod arrNotesAvailable(iIndex)
    Next

    Return arrChangeAmounts
End Function

This will automatically return an array filled with the exact amounts of each unit to be issued.

An even more advanced solution:

    Private Function CalculateChange(ByVal dAmount As Decimal) As Decimal()
    Dim arrNotesAvailable() As Decimal = {200D, 100D, 50D, 20D, 10D, 5D, 2D, 1D, 0.5D, 0.2D, 0.1D, 0.05D}
    Dim arrChangeAmounts(arrNotesAvailable.Length - 1) As Decimal

    For iIndex As Integer = 0 To arrNotesAvailable.Length - 2
        arrChangeAmounts(iIndex) = (dAmount * 100) \ (arrNotesAvailable(iIndex) * 100)
        dAmount = dAmount Mod arrNotesAvailable(iIndex)
    Next

    arrChangeAmounts(arrNotesAvailable.Length - 1) = Math.Ceiling((dAmount * 100) / (arrNotesAvailable(arrNotesAvailable.Length - 1) * 100))

    Return arrChangeAmounts
End Function

This will break down the amount of notes and coins needed to the second last specified unit and then 'fill' the remainder with the smallest unit of currency. For those countries that do not trade in units up to 1 (1 cent or 1 yen). In our country we trade up until 5 cents.




回答3:


Just did an Assignment like it. Public Class Form1 'Name: Aaron Holiday 'Class: IME 211 'Purpose: Change Calc

Dim Owed As Double
Dim Tend As Double
Dim Change As Double
Dim _20D As Integer
Dim _10D As Integer
Dim _5D As Integer
Dim _1D As Integer
Dim _25C As Integer
Dim _10C As Integer
Dim _5C As Integer
Dim _1C As Integer

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Close()
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    'Clear Due Amounts
    txtOwed.Text = ""
    txtTend.Text = ""
    lblChange.Text = ""
    'Clear Dollar Amounts
    lbl20D.Text = ""
    lbl10D.Text = ""
    lbl5D.Text = ""
    lbl1D.Text = ""
    'Clear Cents Amounts
    lbl25C.Text = ""
    lbl10C.Text = ""
    lbl5C.Text = ""
    lbl1C.Text = ""

    txtOwed.Focus()

End Sub

Private Sub btnRing_Click(sender As Object, e As EventArgs) Handles btnRing.Click
    'Check to see if Empty
    If ((txtOwed.Text) = Nothing) Then
        lblChange.Text = ("Error")
        MessageBox.Show("Please Enter Amount Owed")
        Exit Sub
    End If

    If ((txtTend.Text) = Nothing) Then
        lblChange.Text = ("Error")
        MessageBox.Show("Please Enter Amount Tendered")
        Exit Sub
    End If

    'Set Dim Values
    Owed = txtOwed.Text
    Tend = txtTend.Text

    'Calculate Change due
    Change = (Tend - Owed)
    lblChange.Text = Change.ToString("C2")

    'Check if they paid enough
    If ((lblChange.Text) < 0) Then
        lblChange.Text = ("Pay Up!")
        MessageBox.Show("Please Pay full Amount!")
        Exit Sub
    End If


    'Set Dim Values
    _20D = 0 '$20
    _10D = 0 '$10
    _5D = 0 '$5
    _1D = 0 '$1
    _25C = 0 '$0.25
    _10C = 0 '$0.10
    _5C = 0 '$0.05
    _1C = 0 '$0.01

    'Find Amounts of Each 
    Do While Change >= 20
        _20D = _20D + 1
        Change = Change - 20
    Loop
    'Display $20s
    lbl20D.Text = _20D

    '================================================

    Do While Change >= 10
        _10D = _10D + 1
        Change = Change - 10
    Loop
    'Display $10s
    lbl10D.Text = _10D

    '================================================

    Do While Change >= 5
        _5D = _5D + 1
        Change = Change - 5
    Loop
    'Display $5s
    lbl5D.Text = _5D

    '================================================

    Do While Change >= 1
        _1D = _1D + 1
        Change = Change - 1
    Loop
    'Display $1s
    lbl1D.Text = _1D

    '================================================

    Do While Change >= 0.25
        _25C = _25C + 1
        Change = Change - 0.25
    Loop
    'Display $0.25s
    lbl25C.Text = _25C

    '================================================

    Do While Change >= 0.1
        _10C = _10C + 1
        Change = Change - 0.1
    Loop
    'Display $0.10s
    lbl10C.Text = _10C

    '================================================

    Do While Change >= 0.05
        _5C = _5C + 1
        Change = Change - 0.05
    Loop
    'Display $0.05s
    lbl5C.Text = _5C

    '================================================

    Do While Change >= 0.01
        _1C = _1C + 1
        Change = Change - 0.01
    Loop
    'Display $0.01s
    lbl1C.Text = _1C

    'Display Thank you
    If ((lblChange.Text) >= 0) Then
        MessageBox.Show("Thank you, Come Again")
    End If
End Sub
End Class


来源:https://stackoverflow.com/questions/4894345/how-do-you-calculate-the-minimum-coin-change-for-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!