ถึงฟังชั้นไม่ได้คำนวณอะไรมากมาย และผมก็คิดเองได้ แต่มันดีมากที่ผมไม่ต้องมาเขียนเองคิดเอง
[code]'โจทย์ถาม AI: make a vb6 function that convert number array (1,2,3,4,5) to string "1-5"
Function ArrayToString(arr() As Integer) As String
'Sort the array in ascending order
Call QuickSort(arr, LBound(arr), UBound(arr))
'Initialize the result string and the current range
Dim result As String
Dim start As Integer
Dim ed As Integer
result = ""
start = arr(LBound(arr))
ed = start
'Loop through the array and append ranges to the result
Dim i As Integer
For i = LBound(arr) + 1 To UBound(arr)
If arr(i) = ed + 1 Then
'Extend the current range
ed = arr(i)
Else
'Add the current range to the result
If start = ed Then
result = result & start & ","
Else
result = result & start & "-" & ed & ","
End If
'Start a new range
start = arr(i)
ed = start
End If
Next i
'Add the last range to the result
If start = ed Then
result = result & start
Else
result = result & start & "-" & ed
End If
'Return the result string
ArrayToString = result
End Function
'QuickSort algorithm from
https://www.vbforums.com/showthread.php?786079-Classic-VB-Quicksort-algorithm-for-arrays-of-any-type-and-any-rank&p=4819663&viewfull=1#post4819663
Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLow
tmpHi = inHi
pivot = vArray((inLow + inHi) \ 2)
While (tmpLow <= tmpHi)
While (vArray(tmpLow) < pivot And tmpLow < inHi)
tmpLow = tmpLow + 1
Wend
While (pivot < vArray(tmpHi) And tmpHi > inLow)
tmpHi = tmpHi - 1
Wend
If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = tmpSwap
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Wend
If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
End Sub
'โจทย์ถาม AI 2: make a vb6 function to get summary of numbers from comma separated string Ex." ""a=6, b=6, c=6, d=2""
Function GetSummary(str As String) As Double
'Initialize the result variable
Dim result As Double
result = 0
'Split the string by "," and loop through the parts
Dim parts() As String
Dim part
parts = Split(str, ",")
For Each part In parts
'Split the part by "=" and get the second part
Dim values() As String
Dim value As String
values = Split(part, "=")
If UBound(values) >= 1 Then
'Convert the value to a double and add it to the result
result = result + CDbl(values(1))
End If
Next part
'Return the result
GetSummary = result
End Function
Sub Text()
Dim n(8) As Integer
n(0) = 6
n(1) = 3
n(2) = 2
n(3) = 5
n(4) = 4
n(5) = 1
n(6) = 11
n(7) = 12
n(8) = 13
MsgBox ArrayToString(n)
Dim n2 As String
n2 = "a=6, b=2, c=6, d=6"
MsgBox GetSummary(n2)
End Sub
[/code]
ปลื้ม โค้ดฟังก์ชั้นที่ Bing AI สร้างให้มากเลยครับ
[code]'โจทย์ถาม AI: make a vb6 function that convert number array (1,2,3,4,5) to string "1-5"
Function ArrayToString(arr() As Integer) As String
'Sort the array in ascending order
Call QuickSort(arr, LBound(arr), UBound(arr))
'Initialize the result string and the current range
Dim result As String
Dim start As Integer
Dim ed As Integer
result = ""
start = arr(LBound(arr))
ed = start
'Loop through the array and append ranges to the result
Dim i As Integer
For i = LBound(arr) + 1 To UBound(arr)
If arr(i) = ed + 1 Then
'Extend the current range
ed = arr(i)
Else
'Add the current range to the result
If start = ed Then
result = result & start & ","
Else
result = result & start & "-" & ed & ","
End If
'Start a new range
start = arr(i)
ed = start
End If
Next i
'Add the last range to the result
If start = ed Then
result = result & start
Else
result = result & start & "-" & ed
End If
'Return the result string
ArrayToString = result
End Function
'QuickSort algorithm from https://www.vbforums.com/showthread.php?786079-Classic-VB-Quicksort-algorithm-for-arrays-of-any-type-and-any-rank&p=4819663&viewfull=1#post4819663
Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLow
tmpHi = inHi
pivot = vArray((inLow + inHi) \ 2)
While (tmpLow <= tmpHi)
While (vArray(tmpLow) < pivot And tmpLow < inHi)
tmpLow = tmpLow + 1
Wend
While (pivot < vArray(tmpHi) And tmpHi > inLow)
tmpHi = tmpHi - 1
Wend
If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = tmpSwap
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Wend
If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
End Sub
'โจทย์ถาม AI 2: make a vb6 function to get summary of numbers from comma separated string Ex." ""a=6, b=6, c=6, d=2""
Function GetSummary(str As String) As Double
'Initialize the result variable
Dim result As Double
result = 0
'Split the string by "," and loop through the parts
Dim parts() As String
Dim part
parts = Split(str, ",")
For Each part In parts
'Split the part by "=" and get the second part
Dim values() As String
Dim value As String
values = Split(part, "=")
If UBound(values) >= 1 Then
'Convert the value to a double and add it to the result
result = result + CDbl(values(1))
End If
Next part
'Return the result
GetSummary = result
End Function
Sub Text()
Dim n(8) As Integer
n(0) = 6
n(1) = 3
n(2) = 2
n(3) = 5
n(4) = 4
n(5) = 1
n(6) = 11
n(7) = 12
n(8) = 13
MsgBox ArrayToString(n)
Dim n2 As String
n2 = "a=6, b=2, c=6, d=6"
MsgBox GetSummary(n2)
End Sub
[/code]