需要帮助将此代码写入经典 ASP 中的并行数组
need help to write this code to parallel arrays in classic ASP
如果有人可以帮助将此 if else 语句转换为并行数组以计算折扣。
is a data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record. Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of each record together in memory.
For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index.
'总成本与 GST 的折扣计算
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>")
%>
输出
TotGST=1000 Discount = 0
TotGST=5000 Discount = 250
TotGST=5500 Discount = 275
TotGST=9999 Discount = 499.95
TotGST=10000 Discount = 800
TotGST=50000 Discount = 5000
如果有人可以帮助将此 if else 语句转换为并行数组以计算折扣。
is a data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record. Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of each record together in memory.
For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index.
'总成本与 GST 的折扣计算
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>")
%>
输出
TotGST=1000 Discount = 0 TotGST=5000 Discount = 250 TotGST=5500 Discount = 275 TotGST=9999 Discount = 499.95 TotGST=10000 Discount = 800 TotGST=50000 Discount = 5000