如果有人可以帮忙将这个if else语句转换成并行数组,以找出计算折扣。
并行数组
是表示记录数组的数据结构。它为记录的每个字段保留一个单独的同构数组,每个字段具有相同数量的元素。然后,位于每个数组中相同索引处的对象隐式地成为单个记录的字段。从一个对象指向另一个对象的指针被数组索引替换。这与将每个记录的所有字段一起存储在内存中的常规方法形成对比。
例如,可以声明一个包含100个名字(每个名字是字符串)和100个年龄(每个年龄是整数)的数组,并将每个名字与具有相同索引的年龄相关联。
含消费税的总成本折扣计算
if TotGST >= 5000 AND TotGST <= 9999 then
discount = (TotGST * 0.05)
else
if TotGST >= 10000 AND TotGST <= 49999 then
discount = (TotGST * 0.08)
else
if TotGST >= 50000 then
else
discount = (TotGST * 0.1)
end if
end if
end if
首先,设置并行数组。然后在循环中遍历数组以找到匹配的范围。如果匹配,则应用折扣。
参见下面的示例代码
<%
Function CalcDiscount(nAmount)
' Set up the arrays
Amin = Array(5000,10000,50000)
Amax = Array(9999,49999,-1)
Adiscount = Array(0.05,0.08,0.10)
' Initialise other variables
nUpper = uBound(Adiscount)
i = 0
bDiscount = false
CalcDiscount = 0
' Loop through the array to find a matching amount range
do until (i > nUpper or bDiscount = true)
If (nAmount >= Amin(i) and (nAmount <= Amax(i) or Amax(i) = -1)) Then
' Apply discount
CalcDiscount = nAmount * Adiscount(i)
bDiscount = true
End If
i = i + 1
loop
End Function
' Run some test cases
TotGST = 1000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 5000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 5500
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 9999
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 10000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 50000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
%>
输出<>之前折扣= 0折扣= 250折扣= 275TotGST=9999 Discount = 499.95折扣= 800折扣=5000