我的代码是经典的ASP,但只要你能用任何语言(PHP,JavaScript,Java)给我一些技巧或算法,我就可以用经典的ASP来实现。
我已设法按价格按降序对记录集进行排序。更贵的物品是第一位的。目前,我遍历记录集并将每个项目放入多维数组中。
Do While NOT RS.EOF
arrBought(itemcount, 0) = RS("ItemID")
arrBought(itemcount, 1) = RS("Price")
arrBought(itemcount, 2) = RS("QuantityBought")
RS.MoveNext
itemcount = itemcount + 1
Loop
但是如何修改上述循环,为数量所购买的每5件物品分配20%的折扣?
假设客户购买了以下物品。记录集将包含以下数据:
ItemID:A005, 价格:100, QuantityBought:2
ItemID:A001, 价格:80, QuantityBought:2
ItemID:A006, 价格:60, QuantityBought:5
如何遍历该记录集并创建以下数组?
arrBought(0, 0) = "A005"
arrBought(0, 1) = 100
arrBought(0, 2) = 2
arrBought(1, 0) = "A001"
arrBought(1, 1) = 80
arrBought(1, 2) = 2
arrBought(2, 0) = "A006"
arrBought(2, 1) = 48 '20% discounted
arrBought(2, 2) = 1
arrBought(3, 0) = "A006"
arrBought(3, 1) = 60
arrBought(3, 2) = 4
答案 0 :(得分:1)
这是一个裂缝。用户3580294正在引导您完成,这基本上只是考虑定期定价的单位和打折的单位。
dim intItemCount
dim intQtyBought
dim strItemID
dim curPrice
dim intQtyToDiscount
dim intDiscountSwitch
intItemCount = 0
intDiscountSwitch = 0
Do While NOT RS.EOF
strItemID = RS("ItemID") 'More efficient than repetitively referencing the RS
curPrice = RS("Price")
intQtyBought = RS("QuantityBought")
intQtyToDiscount = 0 'Set to 0 each record
intDiscountSwitch = intDiscountSwitch + intQtyBought
if intDiscountSwitch >= 5 then 'Need to process a discount
intQtyToDiscount = intDiscountSwitch \ 5 'How many of the current item we need to discount
intDiscountSwitch = intDiscountSwitch Mod 5 'Reset the discount switch to the remainder
'First load the discounted items into the array
arrBought(intItemCount, 0) = strItemID
arrBought(intItemCount, 1) = curPrice * .8
arrBought(intItemCount, 2) = intQtyToDiscount
intItemCount = intItemCount + 1
end if
'Whether or not we had a discount, load the regular-priced items (intQtyToDiscount will be 0 if no discounted items)
arrBought(intItemCount, 0) = strItemID
arrBought(intItemCount, 1) = curPrice
arrBought(intItemCount, 2) = intQtyBought - intQtyToDiscount
RS.MoveNext
intItemCount = intItemCount + 1
Loop