'Command1,Text1,Picture1各1个
Dim n%, s(100) As String
Private Sub Form_Load()
Command1.Caption = "排序"
Picture1.AutoRedraw = True
Text1 = ""
End Sub
Private Sub text1_keypress(keyascii As Integer)
If n >= 100 Then Exit Sub
If keyascii = 13 Then
s(n) = Text1
Text1 = ""
n = n + 1
End If
End Sub
Private Sub Command1_Click()
Picture1.Cls
n = n - 1 '实际个数
For i = 0 To n - 1
For j = i + 1 To n
If s(j) <> "" Then
If s(i) < s(j) Then
temp = s(i): s(i) = s(j): s(j) = temp
End If
End If
Next j
Next i
For i = 0 To n
Picture1.Print s(i); "|";
If (i + 1) Mod 5 = 0 Then Picture1.Print
Next i
n = n + 1 '准备下次输入
End Sub
以下是完整版的内容(包括界面)。
用法如下:先把下面代码保存为Form1.frm文件,然后双击打开此文件,运行,输入数据,最后点击排序按钮即可。
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3585
ClientLeft = 60
ClientTop = 450
ClientWidth = 5175
LinkTopic = "Form1"
ScaleHeight = 3585
ScaleWidth = 5175
StartUpPosition = 3 '窗口缺省
Begin VB.PictureBox Picture1
Height = 2655
Left = 240
ScaleHeight = 2595
ScaleWidth = 4275
TabIndex = 2
Top = 720
Width = 4335
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 3360
TabIndex = 1
Top = 240
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Left = 240
TabIndex = 0
Text = "Text1"
Top = 240
Width = 3015
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim n%, s(100) As String
Private Sub Form_Load()
Command1.Caption = "排序"
Picture1.AutoRedraw = True
Text1 = ""
Command1.Enabled = False
End Sub
Private Sub text1_keypress(keyascii As Integer)
If keyascii = 13 Then
If n <= 100 Then
s(n) = Text1
Text1 = ""
n = n + 1
Else
MsgBox "输入数据个数已够,不能再输入!"
Command1.Enabled = True
End If
End If
End Sub
Private Sub Command1_Click()
Dim i, j, temp
Picture1.Cls
For i = 0 To n - 1
For j = i + 1 To n - 1
If s(i) < s(j) Then
temp = s(i): s(i) = s(j): s(j) = temp
End If
Next j
Next i
Picture1.Print "按递减次序的排列如下:"
For i = 0 To n
Picture1.Print s(i)
DoEvents
Next
End Sub
前面的代码不变,以下是排序代码:
for i=0 to 99
for j=i+1 to 100
if s(j)<>"" then
if s(i)temp=s(i):s(i)=s(j):s(j)=temp
end if
end if
next j
next i
for i=0 to 100
picture1.print s(i)
next i