当前位置: 首页 > 设计代码 > 正文

字符串的最大,最小和平均值

用两种不同的方法来求实现数字字符串的最大,最小和平均值
<%@ LANGUAGE = "VBScript" ENABLESESSIONSTATE = False %>
<% “1. Selecting the numbers from a string: myString = "2, 4, 6, 8, 10, 12" aValues = Split(myString, ",", -1, 1) Function Min(someNumbers) dblLowestSoFar = Null For I = LBound(someNumbers) to UBound(someNumbers) If IsNumeric(someNumbers(I)) Then If CDbl(someNumbers(I)) < dblLowestSoFar or IsNull(dblLowestSoFar) Then dblLowestSoFar = CDbl(someNumbers(I)) End If End If Next Min = dblLowestSoFar End Function Function Max(someNumbers) dblHighestSoFar = Null For I = LBound(someNumbers) to UBound(someNumbers) If IsNumeric(someNumbers(I)) Then If CDbl(someNumbers(I)) > dblHighestSoFar or IsNull(dblHighestSoFar) Then
dblHighestSoFar = CDbl(someNumbers(I))
End If
End If
Next
Max = dblHighestSoFar
End Function

Function Avg(someNumbers)
dblHighestSoFar = Null
For I = LBound(someNumbers) to UBound(someNumbers)
If IsNumeric(someNumbers(I)) Then
If CDbl(someNumbers(I)) > dblHighestSoFar or IsNull(dblHighestSoFar) Then
dblHighestSoFar = CDbl(someNumbers(I)) + CDbl(someNumbers(I)) /
End If
End If
Next
Avg = dblHighestSoFar
End Function

“2. or, selecting the numbers from a database:
Set cnDZ = Server.CreateObject("ADODB.Connection")
cnDZ.Open "DSN=myDatabase"
Set rsLo = cnDZ.Execute("Select Min(PRICE) AS loPRICE FROM TBLADS")
Set rsHi = cnDZ.Execute("Select Max(PRICE) AS hiPRICE FROM TBLADS")
Set rsAvg = cnDZ.Execute("Select Avg(PRICE) AS avgPRICE FROM TBLADS")
%>


1. Selecting the numbers from a string:
The lowest number is: <%=Min(aValues)%>
The highest number is: <%=Max(aValues)%>
The average number is: <%=Avg(aValues)%>

2. Selecting the numbers from a database:
The lowest priced ad is: <%=FormatCurrency(Int(rsLo("loPRICE")))%>
The highest priced ad is: <%=FormatCurrency(Int(rsHi("hiPRICE")))%>
The average selling price of these ads is: <%=FormatCurrency(Int(rsAvg("avgPRICE")))%>


<% cnDZ.Close Set cnDZ = Nothing rsLo.Close Set rsLo = Nothing rsHi.Close Set rsHi = Nothing rsAvg.Close Set rsAvg = Nothing %>

本文固定链接: http://blog.duoday.com/2006/139.html | 奇风's Blog

该日志由 于2006年08月02日发表在 设计代码 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。

原创文章转载请注明: 字符串的最大,最小和平均值 | 奇风's Blog

字符串的最大,最小和平均值:等您坐沙发呢!

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注
? razz sad evil ! smile oops grin eek shock ??? cool lol mad twisted roll wink idea arrow neutral cry mrgreen
快捷键:Ctrl+Enter