關於VB中排序的問題

2022-05-21 09:49:45 字數 2145 閱讀 7547

1樓:玫瑰為你盛開

'------------------------------------------

'需要準備兩組控制元件,左側豎著排列的text1(0)-text1(3)控制元件組、右側豎著排列的text2(0)-text2(3)控制元件組

'text1控制元件組的建立過程,先拖一個text1,複製,在窗體內貼上3次,text2控制元件組也這樣建立

'測試所用的command1控制元件,若不需要,則將command1那段的**註釋或去除

'------------------------------------------

'為前四個文字框比大小,為後四個文字框排順序

private sub bidx(byref a() as single)

dim t as single, k as integer

for i = 0 to 2

k = i

for j = i + 1 to 3

if a(j) > a(k) then k = j

next

t = a(i)

a(i) = a(k)

a(k) = t

next

for i = 0 to 3

for k = 0 to 3

if val(text1(i).text) = a(k) then text2(i).text = k + 1

next

next

end sub

'為前四個文字框生成資料結果,也可以手工輸入或更改

private sub command1_click()

randomize

for i = 0 to 3

text1(i).text = 20 * rnd

next

end sub

private sub form_activate()

for i = 0 to 3

text1(i).text = ""

text2(i).text = ""

next

command1.setfocus

end sub

'前四個文字框中的任何一個有變化時,則後四個文字框都會跟著變化

private sub text1_change(index as integer)

dim a1(0 to 3) as single

if text1(0).text <> "" and text1(1).text <> "" and text1(2).

text <> "" and text1(3).text <> "" then

for i = 0 to 3

a1(i) = val(text1(i).text)

next

bidx a1

else

for i = 0 to 3

text2(i).text = ""

next

end if

end sub

2樓:匿名使用者

先對問題做一簡化:假設前一列四個文字框為控制元件陣列,名字為text1,後一列也為控制元件陣列,名字為text2.再假設前一列文字框中已經有數字,且為整數.

option base 0

private sub command1_click()dim a(3) as integer, i as integer, j as integer

for i = 0 to 3

a(i) = val(text1(i))

next

for i = 0 to 2

k = i

for j = i + 1 to 3

if a(j) > a(k) then k = jnext

t = a(i)

a(i) = a(k)

a(k) = t

next

for i = 0 to 3

for k = 0 to 3

if val(text1(i)) = a(k) then text2(i) = k + 1

next

next

end sub

3樓:匿名使用者

前一列文字框用來顯示什麼計算結果?

我覺得前一列文字框應該用來顯示輸入的資料吧

vb程式設計,隨機數和排序的問題

private sub command1 click list2.clear for i 0 to 29 x list1.list i if x mod 4 0 and x mod 100 0 or x mod 400 0 then list2.additem x next i end sub pr...

問關於VB的問題,問一個關於VB的問題

執行順序如下 注 帶有 的是被執行的語句,後面的數字是被執行的順序。x 0 1 for i 1 to 10 2if i mod 2 1 then 3x x 1 4 else x x 1 end if 5 exit for 6 問題就出在這裡 你退出掉迴圈幹嘛 next i print x 7 結束所...

一個vb控制元件的問題,關於VB模組的問題

在窗體上新增兩個命令按鈕,將第二個命令按鈕command2的index屬性設定為0,下面的 實現點選command1按鈕,將新增一個按鈕,並自動排列。option explicit 輸出十個命令控制元件。dim i as integer private sub command1 click i i ...