<source id="v77gz"></source>
    <u id="v77gz"><p id="v77gz"></p></u>

      <samp id="v77gz"></samp>

      <var id="v77gz"><td id="v77gz"><ins id="v77gz"></ins></td></var>

      <video id="v77gz"></video>
      <source id="v77gz"></source>
    1. 江蘇省高校計算機等級考試命題研究院 江蘇省高校計算機等級考試輔導
      二級VB常用算法(七)數組元素插入刪除- -

      VB常用算法(七)數組元素插入刪除- -

      曹蘇群  http://caosuqun.bokee.com

      Tag插入    刪除    算法                                          

      1、算法說明

      數組中元素的插入和刪除一般是在已固定序列的數組中插入或刪除一個元素,使得插入或刪除操作后的數組還是有序的。

      基本思路:首先要找到插入位置或要刪除的元素。

      1)        插入

      代碼如下:

      Private Sub Command1_Click()

          Dim a(10) As Integer

          Dim i As Integer, k As Integer

          For i = 0 To 9       '生成數組

              a(i) = i * 3 + 1

          Print a(i);

          Next i

          Print

          Print "插入14"

          For k = 0 To 9       '查找插入14在數組中的位置

              If 14 < a(k) Then Exit For

          Next k

          For i = 9 To k Step -1 '從最后元素開始逐個后移,騰出位置

              a(i + 1) = a(i)

          Next i

          a(k) = 14            '插入數14

          For i = 0 To 10

              Print a(i);

          Next i

          Print

      End Sub

       

      2)       

      1       4      7      10     13    16     19     22     25    28

      K

      刪除

      代碼如下:

      Dim a() as integer

      ….

      ReDim a(1 to n)

      For i=k+1 to n

      a(i-1)=a(i)

      Next I

      Redim preserve a(1 to n-1)

       

      2、實戰練習

      1)        補充代碼(2001秋二(8))

               C盤根目錄下文件Data4.txt的內容是:2,4,6,8,10,1,3,5,7,9。下面程序的功能是將文件后半部分的奇數分別按序插入到前半部分的適當位置,得到的新數列是:1 2 3 4 5 6 7 8 9 10。(實現方法:第一次調整后的數列是:1 2 4 6 8 10 3 5 7 9第二次調整后的數列是:1 2 3 4 6 8 10 5 7 9)。

       

                        Option Explict

                        Private Sub Form_Click()

                                 Dim A(10) As Integer,i As Integer,J As Integer

                                 Open "c:\Data4.txt" For Input As #12

                                 Do   1 

                                          J=J+1

                                          Input #12,A(J)

                                 Loop

                                 Call Insert(A)

                                 For i=1 To 10

                                          Print A(i);

                                 Next i

                                 Print

                                 Close #12

                        End Sub

       

                        Private Sub Insert(A() As Integer)

                                 Dim i As Integer,Putp As Integer,J As Integer

                                 Dim Getp As Integer,N As Integer,Tem As Integer

                                 N=UBound(A)/2

                                 Putp=1

                                 Getp=N+1

                                 For i=1 To N

                                          Tem=A(Getp)

                                          For J=Getp To Putp +1 Step -1

                                                     2 

                                          Next J

                                          A(Putp)=Tem

                                          Getp=Getp+1

                                          Putp=  3  

                                 Next i

                        End Sub

       

      2)        補充代碼(2001春二(8))

               下面程序得功能是將無序數組中相同得數只保留一個,其余得刪除,并輸出經過刪除后的數組元素,刪除相同數是通過將該數組元素后面的元素在數組內依次前移替換前一個元素的值實現的。數組各元素的值從文件data.txt中讀取。

       

                        Option Explict

                        Option Base 1

                        Private Sub Form_Click()

                                 Dim I As Integer,J As Integer,K As Integer

                                 Dim A()As Integer,T As Integer,M As Integer

                                 Open "c:\my documents\2000test\data.txt" For Input As #1

                                 Do While  1 

                                          I=I+1

                                          Redim Preserve A(I)

                                          Input #1,A(I)

                                 Loop

                                 M=1:T=  2 

                                 Do While M

                                          I=M+1

                                          Do While I<=T

                                                   If A(I)=A(M)Then

                                                            For J=1 To   3 

                                                                     A(J)=A(J+1)

                                                            Next J

                                                            T=T-1

                                                   Else

                                                            I=  4 

                                                   End If

                                          Loop

                                          M=M+1

                                 Loop

                                 Redim Preserve A(T)

                                 For I=1 To T

                                          Print A(I);

                                 Next I

                                 Print

                        End Sub

      中文在线字幕第一页_中文字幕高跟丝袜作品番号_亚洲中文无无码第_日本亚洲欧美国产日韩a??y_在线观看亚洲av无码专区