【问题标题】:Convert negative numbers from an array to positive numbers将数组中的负数转换为正数
【发布时间】:2015-08-26 18:05:46
【问题描述】:

我编写了一个 VBscript 来找出数组中的正数和负数。我已经设法将这些数字分开并打印出来。我现在想将正数转换为负数。

这是我写的vbscript:

    Option explicit
    Dim arr(), i, str, number,j,k,str1,p
    number=inputbox("The number of elements the array should have")
    ReDim arr(number-1)

    arr(0)=1
    arr(1)=2
    arr(2)=-4
    arr(3)=6
    arr(4)=-8


    For i=0 to number-1
        If arr(i)>0 Then
            j=i
            str=str&vbnewline&arr(j)&vbnewline
            msgbox ("The positive numbers from the array are " &str)
    end if
    next
    For i=0 to number-1
        If arr(i)<0 Then
             k=i
            str1=str1&vbnewline&arr(k)&vbnewline
            msgbox ("The negative numbers from the array are " &str1)
        End If
        Next

'ReDim preserve arr(6)
    'For i= 5 to 7
        'p=arr(k)*(-1)
        'Next
    'msgbox p

脚本成功执行,直到第二个 for 循环。我正在尝试将数组中的负数转换为正数(在本例中为 -4、-8)。当我执行注释代码时,我只得到第二个数字“8”。我必须一起显示所有数字(正数和转换后的数字)。怎么办?

【问题讨论】:

    标签: arrays vbscript qtp


    【解决方案1】:

    您做得对,但是您在最后评论的for loop 中使用了最后一个负数条目,因此您缺少-4。试试这样的:

    Dim arr()
    ReDim arr(5)
    
    arr(0)=1
    arr(1)=2
    arr(2)=-4
    arr(3)=6
    arr(4)=-8
    
    For i = 0 to UBound(arr) - 1
        If arr(i) < 0 Then
            Msgbox arr(i)
            arr(i) = arr(i)*-1  '<-- This will convert all the negative to positive
        End If
    Next
    
    For i = 0 to UBound(arr) - 1
        Msgbox arr(i)
    Next
    

    【讨论】:

    • @Ekkehard.Horner 我认为使用这种方式没有任何问题。我的回答只是一个例子。
    • Dim arr() cf stackoverflow.com/a/28798878/603855; ReDim arr(desiredUBound) 就是所需要的。 了解 UBound/Last Index vs. Size 避免创建带有虚假尾部和容易出错的循环标头(如For i = 0 to UBound(arr) - 1)的数组。
    【解决方案2】:

    这就是动态数组在 VBScript 中的处理方式:

    Option Explicit
    
    ' Decent way to initialize a dynamic array
    ReDim a(4) ' 5 slots 0 .. 4; no "Dim a()" == spurious creation of an abomination
    a(0) =  1
    a(1) =  2
    a(2) = -4
    a(3) =  6
    a(4) = -8
    WScript.Echo "a =  [" & Join(a) & "]"
    Dim b : b = mapFO(a, New cSignSwitch)
    WScript.Echo "b =  [" & Join(b) & "]"
    WScript.Echo "neg  [" & Join(grepFF(a, GetRef("lessZero"))) & "]"
    WScript.Echo "else [" & Join(grepFF(a, GetRef("geZero"))) & "]"
    ' Convenient way to initialize a dynamic array of Longs
    Dim c : c = mapFF(Split("1 2 -4 6 -8"), GetRef("XCLng"))
    WScript.Echo "c =  [" & Join(c) & "]"
    mapSE c, "a(i) * a(i)"
    WScript.Echo "x*x  [" & Join(c) & "]"
    
    ' return new array from mapping a's elements via o.map
    Function mapFO(a, o)
      Dim t : t = a ' array assignment copies!
      mapSO t, o
      mapFO = t
    End Function
    
    ' return new array from mapping a's elements via f
    Function mapFF(a, f)
      Dim t : t = a ' array assignment copies!
      mapSF t, f
      mapFF = t
    End Function
    
    ' apply o.map to a's elements
    Sub mapSO(a, o)
      Dim i
      For i = 0 To UBound(a)
          a(i) = o.map(a(i))
      Next
    End Sub
    
    ' apply f to a's elements
    Sub mapSF(a, f)
      Dim i
      For i = 0 To UBound(a)
          a(i) = f(a(i))
      Next
    End Sub
    
    ' eval ev for a's elements
    Sub mapSE(a, ev)
      Dim i
      For i = 0 To UBound(a)
          a(i) = Eval(ev)
      Next
    End Sub
    
    Class cSignSwitch
      Function map(e)
        map = e * -1
      End Function
    End Class
    
    ' return new array of a's elements satisfying f
    Function grepFF(a, f)
      ReDim t(UBound(a)) ' result can't be larger than source
      Dim j : j = -1     ' assume empty result
      Dim i
      For i = 0 To UBound(a)
          If f(a(i)) Then
             j    = j + 1
             t(j) = a(i)
          End If
      Next
      ReDim Preserve t(j)
      grepFF = t
    End Function
    
    Function lessZero(n)
      lessZero = (n < 0)
    End Function
    
    Function geZero(n)
      geZero = Not lessZero(n)
    End Function
    
    Function XCLng(x)
      XCLng = CLng(x)
    End Function
    

    输出:

    cscript 32233658.vbs
    a =  [1 2 -4 6 -8]
    b =  [-1 -2 4 -6 8]
    neg  [-4 -8]
    else [1 2 6]
    c =  [1 2 -4 6 -8]
    x*x  [1 4 16 36 64]
    

    【讨论】:

    • 这不是问题的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2022-12-06
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多