vb代码中,如何在一组排好序的数组中插入一个数,并按着原来的顺序排列 写出代码,谢谢了

如题所述

第1个回答  2012-12-09
function NumInsert(num,numindex,source_array(),source_array_count) as integer()
rem //num =要插入的数据
rem //numindex =要插入的位置
rem //source_array(),原始队列
rem //source_array_count=原始队列数据数量

dim abc(source_array_count+1) as integer '新建队列
dim i as integer
'将后面的数据复制到新队列
for i= source_array_count to numindex step -1
abc(i+1)=source_array(i)
next

‘ 将前面的数据复制到新队列
for i= 0 to numindex -1
abc(i)=source_array(i)
next

'插入数据
abc(numindex )= num

return abc
end function
第2个回答  2012-12-09
Private Sub Command1_Click()
Dim s1() As Integer, I As Integer, s2 As String
Dim t1 As Integer, t2 As Integer
ReDim s1(10)
For I = 0 To 10
s1(I) = I
Next
s2 = InputBox("请输入数值和位置,格式:值|位置", "", "31|2")
If s2 <> "" Then
t1 = Mid(s2, 1, InStr(1, s2, "|") - 1)
t2 = Mid(s2, InStr(1, s2, "|") + 1, 10)

If t2 <= UBound(s1) Then

ReDim Preserve s1(UBound(s1) + 1)
For I = UBound(s1) To t2 Step -1
s1(I) = s1(I - 1)
Next
s1(t2) = t1
Else
ReDim Preserve s1(UBound(s1) + 1)
s1(UBound(s1)) = t1
End If
End If

For I = 0 To UBound(s1)
Print s1(I)
Next

End Sub本回答被网友采纳
第3个回答  2012-12-09
redim,再在数组里排序