VB写代码,去重复数,重新排序

VB写代码,去重复数,重新排序,第1张

下面的程序采用的步骤是,先把文本匡怡当中的数字分解字符串数组中。然后调用第一个排序函数,对每一个分解出来的字符串进行内部的排序,使她按数字从小到大排列。这样就把123,321,132等等都变成了123。接着再调用第二个排序过程,把这些个字符串全部按照升序排列。最后把经过排序的字符串去重以后加入到文本框二中。下面是程序的代码和运行的结果。

Dim A() As String

Private Sub Command1_Click()

Text1 = "512 125 215 152 251 521 223 332 233 232 322 323"

A = Split(Text1, " ")

For i = 0 To UBound(A)

Call sort1(A(i))

Next i

Call sort2

i = 0

Do While i <= UBound(A)

Text2 = Text2 &A(i) &" "

i = i + 1

If i >UBound(A) Then Exit Do

Do While A(i) = A(i - 1)

i = i + 1

If i >UBound(A) Then Exit Do

Loop

Loop

End Sub

Private Sub Form_Load()

Text1 = ""

Text2 = ""

End Sub

Private Sub sort1(s As String)

If Mid(s, 1, 1) >Mid(s, 2, 1) Then t = Mid(s, 1, 1): Mid(s, 1, 1) = Mid(s, 2, 1): Mid(s, 2, 1) = t

If Mid(s, 1, 1) >Mid(s, 3, 1) Then t = Mid(s, 1, 1): Mid(s, 1, 1) = Mid(s, 3, 1): Mid(s, 3, 1) = t

If Mid(s, 2, 1) >Mid(s, 3, 1) Then t = Mid(s, 2, 1): Mid(s, 2, 1) = Mid(s, 3, 1): Mid(s, 3, 1) = t

End Sub

Private Sub sort2()

For i = 0 To UBound(A) - 1

For j = 0 To UBound(A) - 1 - i

If A(j) >A(j + 1) Then t = A(j): A(j) = A(j + 1): A(j + 1) = t

Next j

Next i

End Sub

下面是去重的,你要把text1的数组都去的话 需要分拆开来可以用split函数。

Private Sub Command1_Click()

Dim s As String, i As Integer, n

n = InputBox("输入要去重")

i = 1

Do While i <Len(n)

n = Mid(n, 1, i) &Replace(Right(n, Len(n) - i), Right(s, 1), "")

i = i + 1

Loop

Debug.Print n

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11487937.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存