标题: vbs冒泡排序演示 [打印本页]
作者: batman 时间: 2011-3-12 11:29 标题: vbs冒泡排序演示
- input = inputbox("请输入要排序的字符")
- Dim a, b, num, vbstr
- vbstr = " "
- For a=1 To Len(input)
- num = 0:b = 1
- For b = 1 To a
- If Mid(input,a,1) >= Mid(vbstr,b,1) Then num = num+1
- Next
- vbstr = Mid(vbstr,1,num) & Mid(input,a,1) & Mid (vbstr,num+1,Len(vbstr)-num)
- Next
- MsgBox "排序前:" & input & vbCrLf & "排序后:" & Mid(vbstr,2,Len(vbstr)) , 0 , "运算结果如下"
复制代码
作者: wc726842270 时间: 2011-3-13 10:23 标题: 回复 1楼 的帖子
版主不好意思,发现点问题
不能处理含有“0”的数据:比如90 32 19
作者: batman 时间: 2011-3-13 10:51 标题: 回复 2楼 的帖子
楼上是不是理解错了,一楼的vbs是按字符进行排序的,空格也是字符,"90 32 19"排序的结果是" 012399",如果要把空格视为分格符则要修改一楼的代码。。。
作者: wc726842270 时间: 2011-3-13 11:03
啊,是这样啊。确实没看,不好意思
作者: batman 时间: 2011-3-13 21:47
写这个写得快吐血了,代码兼容了数字和字符的排序,并考虑了重复字符的情况,用空格(全、半角)做为分隔- Dim input, num()
- input = InputBox ("请输入,注意将要排序的字符用空格格开:")
- ReDim num(Len (input))
- Dim a, b:b = 0
- For a = 1 To Len (input)
- flag = 0
- If Mid (input, a, 1) <> " " And Mid (input, a, 1) <> " " Then
- num(b) = num(b) & Mid (input, a, 1)
- Else
- If flag = 0 Then b = b+1:flag = 1
- End If
- Next
- Dim str():ReDim str(b)
- ReDim Preserve num(b):dim c, d:a = 1
- For a = 0 To b
- c = 0:d = 0
- For c = 0 To b
- If IsNumeric (num(a)) = True And IsNumeric (num(c)) = True Then
- If num(a) - num (c) > 0 Then d = d+1
- Else
- If num(a) > num(c) Then d = d+1
- End If
- Next
- If str(d) = "" Then
- str(d) = num(a)
- Else
- str(d) = str(d) & " " & num (a)
- End If
- Next
- Dim vbstr:d = 0
- For d = 0 To b
- vbstr = vbstr & " " & str(d)
- Next
- MsgBox "排序前:" & input & vbCrLf & "排序后:" & right (vbstr, Len(vbstr)-1) , 0 , "运算结果"
复制代码
[ 本帖最后由 batman 于 2011-3-13 23:00 编辑 ]
作者: batman 时间: 2011-3-13 23:06
索性再加个网页显示效果,呵呵。。。- Dim input, num()
- input = InputBox ("请输入,注意将要排序的字符用空格格开:")
- ReDim num(Len (input))
- Dim a, b:b = 0
- For a = 1 To Len (input)
- flag = 0
- If Mid (input, a, 1) <> " " And Mid (input, a, 1) <> " " Then
- num(b) = num(b) & Mid (input, a, 1)
- Else
- If flag = 0 Then b = b+1:flag = 1
- End If
- Next
- Dim str():ReDim str(b)
- ReDim Preserve num(b):dim c, d:a = 1
- For a = 0 To b
- c = 0:d = 0
- For c = 0 To b
- If IsNumeric (num(a)) = True And IsNumeric (num(c)) = True Then
- If num(a) - num (c) > 0 Then d = d+1
- Else
- If num(a) > num(c) Then d = d+1
- End If
- Next
- If str(d) = "" Then
- str(d) = num(a)
- Else
- str(d) = str(d) & " " & num (a)
- End If
- Next
- Dim vbstr:d = 0
- For d = 0 To b
- vbstr = vbstr & " " & str(d)
- Next
- Set ie = WScript.CreateObject("internetexplorer.application")
- With ie
- .Navigate "about:blank"
- .Width = 400
- .Height = 200
- With .Document.parentwindow
- .moveto (.Screen.AvailWidth-400)/2,(.Screen.AvailHeight-200)/2
- .Document.Write "<FONT face=楷体_GB2312 color=#FF9224 size=3><br>排序前:" & input & "<br>排序后:" & right(vbstr, len(vbstr)-1) & "</font></br><body bgcolor='#8020ff'></br>"
- .Document.title = "运算结果如下"
- End With
- .document.close
- .MenuBar = 0
- .ToolBar = 0
- .AddressBar = 0
- .StatusBar = 0
- .Visible = true
- .Resizable = 0
- End With
- WScript.Sleep 10000
- ie.Quit
- Set ie = nothing
复制代码
[ 本帖最后由 batman 于 2011-3-13 23:17 编辑 ]
作者: batman 时间: 2011-3-13 23:19
话说论坛处理字符的能力怎么这么差,上面的代码我贴了n次才成功,总是会吃掉字符。。。
作者: wc726842270 时间: 2011-3-13 23:33 标题: 回复 7楼 的帖子
十分有体会,但对于标注还是初学,不清楚其中的奥义。但是有意思的是和论谈的[CODE}很像(我想就是一种吧)
不过说实话,今天是VBS区发贴量最多的一天吧(我所看见的,目前:14)
[ 本帖最后由 wc726842270 于 2011-3-13 23:37 编辑 ]
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |