2014-10-08 133 views
2

請幫我解決這個問題。即時創建在vb6一個陣列程序,其中過程是這樣的插入項目未排序陣列

  • 輸入的數組的大小:3
  • 輸入數組的元素:1
  • 輸入插入的位置:插入後5

:2

  • 輸入要插入的項

    這是我已經做在while循環具有錯誤

    Dim a(50) As Integer, n As Integer, loc As Integer, item As Integer, x As Integer 
    
    Private Sub Form_Load() 
        Dim i As Integer 
        n = InputBox("Enter Size of an Array: ", "Size") 
        Text1.Text = "" 
        For i = 1 To n 
        a(i) = InputBox("Enter Elemets of an Array: ", "Elements") 
        Next i 
        location = InputBox("Enter Location of Insertion: ", "Location") 
        item = InputBox("Enter Item to Insert: ", "Item") 
    
        unsorted 
    
        For i = 1 To n 
        Text1.Text = Text1.Text + "" & a(i) 
        list1.AddItem Text1.Text 
        Text1.Text = "" 
        Next i 
    End Sub 
    
    Public Sub unsorted() 
        While i >= (location - 1) 
        a(i + 1) = a(i) 
        i = i + 1 
        Wend 
        a(location - 1) = item 
        n = n + 1 
    End Sub 
    

    IM的代碼。請幫我

  • +0

    看起來像一個課堂作業。你爲什麼要混合使用'+'來連接它的替代'&'?誰在地球上使用過時的'While ... Wend'了?這兩個都有嚴重的缺點。 – Bob77 2014-10-08 17:38:25

    +0

    我應該使用什麼? – 2014-10-09 02:37:45

    +0

    順便說一句,它不是串聯我只是想添加1到數組的索引 – 2014-10-09 02:40:51

    回答

    1

    類似:

    Option Explicit 
    
    Private Sub Command1_Click() 
        Dim intLoop As Integer 
        Dim intA() As Integer 
        ReDim intA(2) As Integer 
        Dim intIndex As Integer 
        Dim intVal As Integer 
        intA(0) = 1 
        intA(1) = 2 
        intA(2) = 3 
        intIndex = 2 
        intVal = 5 
        intA = InsertVal(intA, intIndex, intVal) 
        For intLoop = 0 To UBound(intA) 
        Print CStr(intLoop) & " : " & CStr(intA(intLoop)) 
        Next intLoop 
    End Sub 
    
    Private Function InsertVal(intSrc() As Integer, intIndex As Integer, intVal As Integer) As Integer() 
        Dim intLoop As Integer 
        Dim intAdded As Integer 
        Dim intResult() As Integer 
        ReDim intResult(UBound(intSrc) + 1) 
        intAdded = 0 
        For intLoop = 0 To UBound(intSrc) 
        If intLoop = intIndex Then 
         intResult(intIndex) = intVal 
         intAdded = intAdded + 1 
        End If 
        intResult(intLoop + intAdded) = intSrc(intLoop) 
        Next intLoop 
        InsertVal = intResult 
    End Function 
    
    +0

    我需要一個程序,它會詢問用戶數組的大小和元素以及他們想要插入的項目的位置 – 2014-10-09 02:36:25

    +0

    將您發佈在您的問題中的代碼與我在此答案中發佈的代碼試着理解代碼,你會看到如何組合它 – Hrqls 2014-10-09 05:30:18

    1

    我已經在這裏要自己解決這就是我所做

    Dim a(50) As Integer 
    Dim n As Integer 
    Dim location As Integer 
    Dim item As Integer 
    
    Private Sub Command1_Click() 
    List1.Clear 
    Call Form_Load 
    End Sub 
    
    Private Sub Form_Load() 
    Dim i As Integer 
    On Error Resume Next 
    Form1.Show 
    n = InputBox("Enter Size of an Array: ", "Input") 
    If n > 50 Then 
        MsgBox "The maximum size of array is 50!", vbCritical, "Error" 
        Exit Sub 
    End If 
        For i = 1 To n 
         a(i) = InputBox("Enter Elements of an array: ", "Input") 
        Next i 
    
         location = InputBox("Enter Location of insertion: ", "Input") 
        If location > n Then 
         MsgBox "Error! Location not possible!", vbCritical, "Warning" 
         Exit Sub 
        End If 
         item = InputBox("Enter Item to insert: ", "Input") 
    
    insert_unsorted 
    
    'print 
        For i = 1 To n 
        Text1.Text = Text1.Text + "" & a(i) 
        List1.AddItem Text1.Text 
        Text1.Text = "" 
    Next i 
    
    End Sub 
    
    Public Sub insert_unsorted() 
    Dim i As Integer 
    i = n 
    
    Do While i >= location - 1 
        a(i + 1) = a(i) 
        i = i - 1 
    
    Loop 
        a(location) = item 
        n = n + 1 
    End Sub 
    

    還是要謝謝你