Android Question vb.net -> basic4android :)

dkarnik

Member
Licensed User
Longtime User
anyone can translate this code to basic4android. i'm trying but... :(

(code is to find all combinations of numbers to reach a custom sum)

Public Class Form1


Sub Display(ByVal Values() As Integer, ByVal size As Integer)
Dim i As Integer

For i = 1 To size
Debug.Print(Values(i))
Next
Debug.Print("---")

End Sub

Sub Fill(ByVal Values() As Integer, ByVal size As Integer, ByVal Data As Integer)
Dim i As Integer

For i = 1 To size
Values(i) = Data
Next
End Sub

Sub Sort(ByVal Values() As Integer, ByVal size As Integer)
Dim i As Integer
Dim j As Integer

For i = 1 To size
For j = 1 To size - i
If Values(j + 1) < Values(j) Then
Call Swap(Values, j, j + 1)
End If
Next
Next
End Sub

Function Total(ByVal Values() As Integer, ByVal size As Integer) As Long
Dim i As Integer
Dim Sum As Long

Sum = 0
For i = 1 To size
Sum = Sum + Values(i)
Next
Total = Sum
End Function

Sub Copy(ByVal First() As Integer, ByVal Second() As Integer, ByVal size As Integer)
Dim i As Integer

For i = 1 To size
First(i) = Second(i)
Next
End Sub

Function Equal(ByVal First() As Integer, ByVal Second() As Integer, ByVal size As Integer) As Boolean
Dim i As Integer
Dim result As Boolean

result = True
For i = 1 To size
If Not First(i) = Second(i) Then
result = False
Exit Function
End If
Next
Equal = result
End Function

Function Sorted_Descending(ByVal Values() As Integer, ByVal size As Integer) As Boolean
Dim i As Integer
Dim Last As Long
Dim result As Boolean

Last = 2147483647.0# ' biggest possible positive number
result = True
For i = 1 To size
If Values(i) <= Last Then
Last = Values(i)
Else
result = False
End If
Next
Sorted_Descending = result
End Function

Sub Swap(ByVal Values() As Integer, ByVal i As Integer, ByVal j As Integer)
Dim temp As Integer

temp = Values(i)
Values(i) = Values(j)
Values(j) = temp
End Sub

Sub Reverse(ByVal Values() As Integer, ByVal First As Integer, ByVal Last As Integer)
Dim i As Integer
Dim j As Integer

i = First
j = Last

While i <= j
Call Swap(Values, i, j)
i = i + 1
j = j - 1
End While
End Sub

Function next_permutation(ByVal Values() As Integer, ByVal size As Integer) As Boolean
Dim i As Integer
Dim j As Integer
Dim k As Integer

If size = 1 Then
next_permutation = False
Exit Function
End If

i = size
While True
k = i
i = i - 1
If Values(i) < Values(k) Then
j = size
While Not Values(i) < Values(j)
j = j - 1
End While
Call Swap(Values, i, j)
Call Reverse(Values, k, size)
next_permutation = True
Exit Function
End If
If i = 1 Then
Call Reverse(Values, 1, size)
next_permutation = False
Exit Function
End If
End While
End Function

Private Sub Combinations(ByVal Values() As Integer, ByVal length As Integer, ByVal Sum As Integer)
Dim Last(10) As Integer
Dim size As Integer

Dim ima As Boolean = False
For size = 2 To length
Call Fill(Last, size, 0)
Call Sort(Values, length) ' have to do entire array
While next_permutation(Values, length) ' have to do entire
If Not Equal(Last, Values, size) Then
If Sorted_Descending(Values, size) Then
If Total(Values, size) = Sum Then
ima = True
Call Display(Values, size)
End If
End If
End If
Call Copy(Last, Values, size)
End While
Next
End Sub

Private Sub Test()
Dim Values(10) As Integer
Dim length As Integer
Dim Sum As Integer
Dim i As Integer

length = 7
Values(1) = 2
Values(2) = 1
Values(3) = 3
Values(4) = 7
Values(5) = 6
Values(6) = 1
Values(7) = 5
Sum = 22

Call Display(Values, length)
Call Combinations(Values, length, Sum)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Test()
End Sub

End Class
 

sirjo66

Well-Known Member
Licensed User
Longtime User
Sicuro si tratti di codice VB.NET ???? A me sembra codice VB6

Are you sure that is VB.NET code ??
I think that it is VB6 code !
Sergio
 
Upvote 0
Top