嘿大家。我有个问题。我正在使用 Visual Basic Express,我应该计算交易的变化。
现在我会使用什么代码?我让它部分工作,但它开始变得有点混乱。
谢谢你。
对于想要更多信息的你们:
假设我有一美元,我去商店买东西。我必须要求用户输入他们花费的金额,然后计算变化并打印到屏幕上。
然后我应该使用最少数量的 25 美分硬币和硬币并将其打印到屏幕上。
任何帮助将不胜感激。
嘿大家。我有个问题。我正在使用 Visual Basic Express,我应该计算交易的变化。
现在我会使用什么代码?我让它部分工作,但它开始变得有点混乱。
谢谢你。
对于想要更多信息的你们:
假设我有一美元,我去商店买东西。我必须要求用户输入他们花费的金额,然后计算变化并打印到屏幕上。
然后我应该使用最少数量的 25 美分硬币和硬币并将其打印到屏幕上。
任何帮助将不胜感激。
我将在这里冒险并假设 OP 正在谈论从交易中返回的钱的变化。
如果是这样的话,那么它可能是作业,所以只有伪代码。
最简单的第一次尝试方法如下。让我们cost
成为交易成本和tendered
交出的金额(均以美分为单位),让我们进一步假设您的经济中只有美元钞票、25 美分硬币和便士(以使我的代码更小)。
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)."
现在,毫无疑问,使用模数和除法运算符可以提高效率,但我将其留给读者作为练习。
我的建议是用铅笔和一张纸坐下来,上面写着以下列(以 2 美元和 93 美分的价格购买 10 美元):
tendered cost change dollars quarters cents
-------- -------- -------- -------- -------- --------
1000 293
并在脑海中逐行运行代码,使用论文中的当前值并写下它们更改的新值。
这将极大地帮助您理解。
回应您的更新:
我有一美元,我去商店买东西。我必须要求用户输入他们花费的金额,然后计算变化并打印到屏幕上。然后我应该使用最少数量的硬币、硬币、镍和便士,然后将其打印到屏幕上。
这与我上面的内容非常相似:
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"
一种更“先进”的自动化流程方式是:
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
这将自动返回一个数组,其中填充了要发行的每个单位的确切数量。
更高级的解决方案:
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
这会将所需的纸币和硬币数量分解为倒数第二个指定单位,然后用最小的货币单位“填充”剩余部分。对于那些不以最多 1 单位(1 美分或 1 日元)进行交易的国家/地区。在我们国家,我们的交易价格高达 5 美分。
刚刚做了一个类似的作业。 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