找回密码
 加入
搜索
查看: 1829|回复: 5

关于一个排序的难题,20Q币以示在下一点小谢意

[复制链接]
发表于 2008-9-7 02:18:50 | 显示全部楼层 |阅读模式


以下是我的界面源码,请高手加进代码实现上面的要求,20个Q币以示在下的一点小谢意。
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include<ListBoxConstants.au3>
#include <File.au3>
#include <array.au3>;注意增加了数组函数
;#include <ListviewConstants.au3>
;#Include <Constants.au3>
#Region ### START Koda GUI section ### Form=
;Opt("GUIOnEventMode", 1)

;Global $Input2 ,$form2


dim $width=800
   Local $height=600
   $Form1=GuiCreate("Form1",$width,$height,(@DesktopWidth-$width)/2, (@DesktopHeight-$height)/2)

$Label1=GUICtrlCreateLabel("下边的框是个输入框GUICtrlCreateInput创建的",100, 480, 150, 30)

$Input3 = GUICtrlCreateInput("在此粘贴要排序的数据", 104, 510, 260, 21)

$Button3 = GUICtrlCreateButton("重新排序", 8, 540, 75, 25, 0)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                        Case $GUI_EVENT_PRIMARYDOWN
                EndSwitch
                WEnd

[ 本帖最后由 蓓蕾 于 2008-9-7 17:02 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2008-9-7 08:41:05 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include<ListBoxConstants.au3>
#include <File.au3>
#include <array.au3>;注意增加了数组函数
;#include <ListviewConstants.au3>
;#Include <Constants.au3>

;Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
;Global $Input2 ,$form2
Local $width = 800
Local $height = 600
$Form1 = GUICreate("Form1", $width, $height, (@DesktopWidth - $width) / 2, (@DesktopHeight - $height) / 2)
$Label1 = GUICtrlCreateLabel("下边的框是个输入框GUICtrlCreateInput创建的", 100, 480, 150, 30)
$Input3 = GUICtrlCreateInput("在此粘贴要排序的数据", 104, 510, 260, 21)
$Button3 = GUICtrlCreateButton("重新排序", 8, 540, 75, 25, 0)
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $GUI_EVENT_PRIMARYDOWN
                        
                Case $Button3
                        Local $s, $sArray, $i
                        $s = StringStripWS(GUICtrlRead($Input3), 1 + 2 + 4)
                        $sArray = StringSplit($s, " ")
                        $sArray = _NumSort($sArray)
                        $s = ""
                        For $i = 1 To $sArray[0]
                                $s = $s & $sArray[$i] & " "
                        Next
                        $s = StringTrimRight($s, 1)
                        GUICtrlSetData($Input3, $s)
        EndSwitch
WEnd

Func _NumSort($Array)
        For $i = 3 To $Array[0] - 1
                For $j = $i + 1 To $Array[0]
                        If $Array[$i] > $Array[$j] Then
                                $t = $Array[$i]
                                $Array[$i] = $Array[$j]
                                $Array[$j] = $t
                        EndIf
                Next
        Next
        Return $Array
EndFunc   ;==>_NumSort


帮楼主写好了,效果如下图:




额外的,具备一定的纠错功能,例如如果输入的号码前部有多余空格、后部有多余空格,或者数字与数字之间并非一个空格,这些错误都不会影响这个程序的运行

[ 本帖最后由 skyfree 于 2008-9-7 08:42 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2008-9-7 10:16:46 | 显示全部楼层
经测试,楼上的代码有时并不能正确排序,修改如下:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListBoxConstants.au3>
#include <File.au3>
#include <array.au3>;注意增加了数组函数
;#include <ListviewConstants.au3>
;#Include <Constants.au3>

;Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
;Global $Input2 ,$form2
Local $width = 800
Local $height = 600
$Form1 = GUICreate("Form1", $width, $height, (@DesktopWidth - $width) / 2, (@DesktopHeight - $height) / 2)
$Label1 = GUICtrlCreateLabel("下边的框是个输入框GUICtrlCreateInput创建的", 100, 480, 150, 30)
$Input3 = GUICtrlCreateInput("在此粘贴要排序的数据", 104, 510, 260, 21)
$Button3 = GUICtrlCreateButton("重新排序", 8, 540, 75, 25, 0)
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $GUI_EVENT_PRIMARYDOWN
                        
                Case $Button3
                        Local $s, $sArray, $i
                        $s = StringStripWS(GUICtrlRead($Input3), 1 + 2 + 4)
                        If Not StringRegExp($s, '^(\d+\s)+\d+$', 0) Then
                                MsgBox(64, "输入有误", "你输入的数据格式不正确!")
                                ContinueLoop
                        EndIf
                        $sArray = StringSplit($s, " ")
                        GUICtrlSetData($Input3, _NumSort())
        EndSwitch
WEnd

Func _NumSort() ;原_arraysort()函数有一定局限,为使程序简洁将此段代码独立出来,不具备通用性
        _ArrayDelete($sArray, 0)
        For $i = 0 To UBound($sArray) - 1
                $sArray[$i] = StringRegExpReplace($sArray[$i], '^0*', '');去掉数字前面的0
                $sArray[$i] = Number($sArray[$i]);转换为数字格式
        Next
        _ArraySort($sArray, 0, 2)
;~ _ArrayDisplay($sArray)
        For $j = 0 To UBound($sArray) - 1
                If StringLen($sArray[$j]) = 1 Then
                        $sArray[$j] = StringFormat('%02d', $sArray[$j]);为<10的数加上前导0
                EndIf
        Next
        Return _ArrayToString($sArray, ' ')
EndFunc   ;==>_NumSort
发表于 2008-9-7 10:48:07 | 显示全部楼层
不一定非得按数值排序,像08,15这种按字符串排序反倒更好一些,不用担心前导0
 楼主| 发表于 2008-9-7 15:03:21 | 显示全部楼层
二位恩人呐,俺一辈子都还不了你们的大恩大德啊,我要怎么给你们充Q币啊,告诉我你们的QQ号吧,我会一辈子记得这段代码这份情。
发表于 2008-9-7 19:12:45 | 显示全部楼层
唉,本来就不是冲着Q币来的。区区小事,何足挂齿?
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-12 16:54 , Processed in 0.076313 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表