Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class FrmDemo
	Inherits System.Windows.Forms.Form
	Private MAXSIZE As Integer
	Private tstAray() As String
	Private tmpAray() As String
	Private Structure Point3D
		Dim X As Single
		Dim Y As Single
		Dim Z As Single
	End Structure
	Private pts() As Point3D
	Private StrColl As New Collection
	Private Ang() As Single
	Private angColl As New Collection
	Dim start_time, num_seconds As Single
	
	Private Function BuildFourCharStr() As String
		
		' ------------------------------------------------
		' Define variables
		' ------------------------------------------------
		Dim n As Short
		Dim iTmpNum As Short
		Dim sTmpChar As String
		
		' ------------------------------------------------
		' Initialize variables
		' ------------------------------------------------
		n = 0
		iTmpNum = 0
		sTmpChar = ""
		
		' ------------------------------------------------
		' Seed the random generator with the number of
		' seconds that have elapsed since midnight
		' ------------------------------------------------
		List1.Items.Clear()
		Randomize(VB.Timer())
		
		' ------------------------------------------------
		' Loop until we have created three printable
		' characters
		' ------------------------------------------------
		Do 
			iTmpNum = Int((90 * Rnd()) + 1)
			
			Select Case iTmpNum
				'
				' we want A-Z (Uppercase only)
				Case 65 To 90
					' convert the the decimal value to ASCII text
					sTmpChar = sTmpChar & Chr(iTmpNum)
					
					' increment character count
					n = n + 1
					
				Case Else
					' Try again
			End Select
			
		Loop Until n = 4
		
		' ------------------------------------------------
		' Return the three character group
		' ------------------------------------------------
		BuildFourCharStr = sTmpChar
		
	End Function
	
	Private Function BuildFourNumbers() As String
		
		' ------------------------------------------------
		' Define variables
		' ------------------------------------------------
		Dim sNumStr As String
		
		' ------------------------------------------------
		' Initialize variables
		' ------------------------------------------------
		sNumStr = ""
		
		' ------------------------------------------------
		' Seed the random generator with the number of
		' seconds that have elapsed since midnight
		' ------------------------------------------------
		Randomize(VB.Timer())
		
		' ------------------------------------------------
		' generate a random number between 1 and 9999
		' ------------------------------------------------
		sNumStr = CStr(Int((9999 * Rnd()) + 1))
		
		' ------------------------------------------------
		' Return formatted number with leading zeros
		' for display purposes
		' ------------------------------------------------
        BuildFourNumbers = VB6.Format(sNumStr, "@@@@")
		
	End Function
	Private Sub DisplayTheData(ByRef ctl As System.Windows.Forms.Control, ByRef MAXSIZE As Integer)
		
		' ------------------------------------------------
		' Define variables
		' ------------------------------------------------
		Dim i As Integer
		Dim n As Short
		Dim sTmpStr As String
		Dim sNewStr As String
		'ReDim tstAray(1 To MAXSIZE) As String 不能用
		' ------------------------------------------------
		' Initialize variables
		' ------------------------------------------------
		
		' ------------------------------------------------
		' Go thru the sorted array and build the
		' display line for the output
		' ------------------------------------------------
		For i = 1 To MAXSIZE
			If Len(tstAray(i)) <> 0 Then
				' Increment the counter
				n = n + 1
				' No more than 12 items on a line
				If n = 13 Then
					sNewStr = sNewStr & sTmpStr & vbCrLf
					sTmpStr = ""
					n = 1
				End If
				
				' append additional items to the line
				sTmpStr = sTmpStr & Space(2) & VB6.Format(tstAray(i), "@@@")
				
			End If
		Next 
		
		' ------------------------------------------------
		' Check to see if there was any leftover data
		' in the temp string.  If so, append it to the
		' display string
		' ------------------------------------------------
		If Len(sTmpStr) > 0 Then
			sNewStr = sNewStr & sTmpStr & vbCrLf
		End If
		
		' ------------------------------------------------
		' Copy the sorted data to the text box and
		' update the screen
		' ------------------------------------------------
		ctl.Text = ""
		ctl.Text = sNewStr
		Me.Refresh()
		
	End Sub
	
	Private Sub CmdCreateNumber_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdCreateNumber.Click
		Dim i As Integer
		start_time = VB.Timer() 'Now
		List1.Items.Clear()
        StrColl.Clear()
		For i = 1 To 1000
			StrColl.Add(BuildFourNumbers)
			List1.Items.Add(BuildFourNumbers)
		Next i
		Lbl1.Text = "unSort Num= " & StrColl.Count()
		num_seconds = VB.Timer() - start_time ' Now - start_time
		lblSortTime.Text = "Elapsed time:  " & VB6.Format(num_seconds, "0.000000") & " seconds"
	End Sub
	
	Private Sub CmdCreateString_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdCreateString.Click
		Dim i As Short
		Dim sTmpStr As String
        StrColl.Clear() 'set StrColl = Nothing
		MAXSIZE = 1000
		Do 
			sTmpStr = ""
			sTmpStr = BuildFourCharStr
			StrColl.Add(sTmpStr) '
			i = i + 1 ' increment the index counter
		Loop Until i = MAXSIZE
		List1.Items.Clear()
		For i = 1 To StrColl.Count()
            List1.Items.Add(StrColl.Item(i))
		Next i
	End Sub
	Private Function ptToString(ByRef pt As Point3D, ByRef nDigit As Short) As String
		ptToString = Trim(System.Math.Round(pt.X, nDigit) & "," & System.Math.Round(pt.Y, nDigit) & "," & System.Math.Round(pt.Z, nDigit))
	End Function
	Private Function newPoint3D(ByRef X As Single, ByRef Y As Single, ByRef Z As Single) As Point3D
		newPoint3D.X = X
		newPoint3D.Y = Y
		newPoint3D.Z = Z
	End Function
	
	Private Sub cmdDuplicate_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDuplicate.Click
		Dim i As Short
		List3.Items.Clear()
		start_time = VB.Timer() 'Now      ' get the system time
		SortNum1_Duplicate(StrColl)
		num_seconds = VB.Timer() - start_time ' Now - start_time
		lblSortTime.Text = "Elapsed time:  " & VB6.Format(num_seconds, "0.000000") & " seconds"
        For i = 1 To StrColl.Count
            List3.Items.Add(StrColl.Item(i))
        Next i
		
		Lbl3.Text = "dulpictate Num= " & StrColl.Count()
	End Sub
	
	Private Sub CmdHiLo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdHiLo.Click
		
        Dim st(6) As String
		List1.Items.Clear()
		st(1) = "-999"
		st(2) = "999"
		st(3) = "zzzz"
		st(4) = "zzz"
		st(5) = "aaa"
		st(6) = "a"
		Dim i As Short
		For i = 1 To 6
			List1.Items.Add(st(i))
		Next i
		List1.Items.Add("str1<str2 ;" & (st(1) < st(2)))
		List1.Items.Add("str3<str2 ;" & (st(3) < st(2)))
		List1.Items.Add("str5<str4 ;" & (st(5) < st(4)))
		List1.Items.Add("str2<str4 ;" & (st(2) < st(4)))
		List1.Items.Add("str5>str6 ;" & (st(5) > st(6)))
	End Sub
	
	Private Sub CmdRemoveDuplicate_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdRemoveDuplicate.Click
		Call SortString_Duplicate(StrColl, 0)
		Dim i As Integer
		For i = 1 To StrColl.Count()
            List3.Items.Add("i= " & i & " ;str= " & StrColl.Item(i))
		Next 
	End Sub
	
	Private Sub CmdSort_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdSort.Click
		Call SortString(StrColl, 0)
		Dim i As Integer
		For i = 1 To StrColl.Count()
            List2.Items.Add("i= " & i & " ;str= " & StrColl.Item(i))
		Next 
	End Sub
	
	Private Sub CmdSortAng_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdSortAng.Click
		Dim i As Short
		Call SortNum1(angColl)
		For i = 1 To angColl.Count()
            List2.Items.Add(angColl.Item(i) & ";" & StrColl.Item(i))
		Next i
	End Sub
	
	Private Sub CmdSortNum_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdSortNum.Click
		Dim i As Short
		List2.Items.Clear()
		start_time = VB.Timer() 'Now      ' get the system time
		SortNum1(StrColl)
		num_seconds = VB.Timer() - start_time ' Now - start_time
		lblSortTime.Text = "Elapsed time:  " & VB6.Format(num_seconds, "0.000000") & " seconds"
		For i = 1 To StrColl.Count()
            List2.Items.Add(StrColl.Item(i))
		Next i
		
		Lbl2.Text = "unSort Num= " & StrColl.Count()
	End Sub
	
	Private Sub CmdSortStr_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdSortStr.Click
		Dim i As Short
		List2.Items.Clear()
		start_time = VB.Timer() 'Now      ' get the system time
		SortStr(StrColl)
		num_seconds = VB.Timer() - start_time ' Now - start_time
		lblSortTime.Text = "Elapsed time:  " & VB6.Format(num_seconds, "0.000000") & " seconds"
		For i = 1 To StrColl.Count()
			'UPGRADE_WARNING: 無法解析物件 StrColl() 的預設屬性。 按一下以取得詳細資訊: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			List2.Items.Add(StrColl.Item(i))
		Next i
		
		Lbl2.Text = "unSort Num= " & StrColl.Count()
	End Sub
	
	Private Sub CmdSortStr_Duplicate_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdSortStr_Duplicate.Click
		Dim i As Short
		List3.Items.Clear()
		start_time = VB.Timer() 'Now      ' get the system time
		SortStr_Duplicate(StrColl)
		num_seconds = VB.Timer() - start_time ' Now - start_time
		lblSortTime.Text = "Elapsed time:  " & VB6.Format(num_seconds, "0.000000") & " seconds"
		For i = 1 To StrColl.Count()
			'UPGRADE_WARNING: 無法解析物件 StrColl() 的預設屬性。 按一下以取得詳細資訊: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			List3.Items.Add(StrColl.Item(i))
		Next i
		
		Lbl3.Text = "Duplicate Str Num= " & StrColl.Count()
	End Sub
	
	Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
		Dim i As Short
		Call SortNum2_Duplicate(angColl, StrColl)
		For i = 1 To angColl.Count()
            List3.Items.Add(angColl.Item(i) & ";" & StrColl.Item(i))
		Next i
	End Sub
	
	Private Sub FrmDemo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim i As Short
		ReDim pts(20)

		pts(0) = newPoint3D(20, 20, 30)
        pts(1) = newPoint3D(30, 40, 30)
        pts(2) = newPoint3D(120, 20, 30)
        pts(3) = newPoint3D(20, 20, 30)
        pts(4) = newPoint3D(30, 40, 30)
        pts(5) = newPoint3D(120, 20, 30)
        pts(6) = newPoint3D(-20, 20, 30)
        pts(7) = newPoint3D(-30, 40, 30)
        pts(8) = newPoint3D(-120, 20, 30)
        pts(9) = newPoint3D(20, 20, 30)
        pts(10) = newPoint3D(30, 40, 30)
        pts(11) = newPoint3D(30, 40, 30)
        pts(12) = newPoint3D(120, 20, 30)
        pts(13) = newPoint3D(20, 20, 30)
        pts(14) = newPoint3D(30, 40, 30)
        pts(15) = newPoint3D(120, 20, 30)
        pts(16) = newPoint3D(-20, 20, 30)
        pts(17) = newPoint3D(-30, 40, 30)
        pts(18) = newPoint3D(-120, 20, 30)
        pts(19) = newPoint3D(-30, 40, 32)
        pts(20) = newPoint3D(-120, 20, 31)
        StrColl.Clear()
		For i = 0 To 20
            StrColl.Add(ptToString(pts(i), 0))
			List1.Items.Add(ptToString(pts(i), 0))
		Next i

		For i = 0 To 20
			ReDim Preserve Ang(i)
			Ang(i) = Rnd(i) * 20
			angColl.Add(System.Math.Round(Ang(i), 0))
		Next i
	End Sub
	
	Private Sub SortNum1(ByRef collMain As Collection)
		Dim i, j As Integer
		Dim collTpt As New Collection
		Dim collTptP As New Collection
		Dim testStrB, testStrA, testStrC As Double
        collTpt.Clear() 'collTpt = Nothing
		collTpt.Add("-99999999999999")
		collTpt.Add("99999999999999")
		
		For i = 1 To collMain.Count()
            testStrA = Val(collMain.Item(i))
			For j = 1 To collTpt.Count() - 1
                testStrB = Val(collTpt.Item(j))
                testStrC = Val(collTpt.Item(j + 1))
				
				If testStrA >= testStrB And testStrA <= testStrC Then

					collTpt.Add(collMain.Item(i),  ,  , j)
					
					GoTo nextA
				End If
			Next j
nextA: 
		Next i
		
		collTpt.Remove(1)
		collTpt.Remove(collTpt.Count())
		
		collMain = collTpt
		
		
	End Sub
	Private Sub SortStr(ByRef collMain As Collection)
		Dim i, j As Integer
		Dim collTpt As New Collection
		Dim collTptP As New Collection
		Dim testStrB, testStrA, testStrC As String
        collTpt.Clear() 'set collTpt = Nothing
		collTpt.Add("0")
		collTpt.Add("ZZZZZZZZZZZZZ")
		
		For i = 1 To collMain.Count()
            testStrA = collMain.Item(i)
			
			For j = 1 To collTpt.Count() - 1
                testStrB = collTpt.Item(j)
                testStrC = collTpt.Item(j + 1)
				If testStrA >= testStrB And testStrA <= testStrC Then
					' MsgBox ("yes")
                    collTpt.Add(collMain.Item(i), , , j)
					GoTo nextA
				End If
			Next j
nextA: 
		Next i
		
		collTpt.Remove(1)
		collTpt.Remove(collTpt.Count())
		
		collMain = collTpt
		
		
	End Sub
	Private Sub SortStr_Duplicate(ByRef collMain As Collection)
		Dim i, j As Integer
		Dim collTpt As New Collection
		Dim collTptP As New Collection
		Dim testStrB, testStrA, testStrC As String
        collTpt.Clear() 'set collTpt = Nothing
		collTpt.Add("0")
        collTpt.Add("ZZZZZZZZZZZZZ")
		For i = 1 To collMain.Count()
            testStrA = collMain.Item(i)
			
			For j = 1 To collTpt.Count() - 1
                testStrB = collTpt.Item(j)
                testStrC = collTpt.Item(j + 1)
				If testStrA > testStrB And testStrA < testStrC Then
					' MsgBox ("yes")
                    collTpt.Add(collMain.Item(i), , , j)
					GoTo nextA
				End If
			Next j
nextA: 
		Next i
		
		collTpt.Remove(1)
        collTpt.Remove(collTpt.Count())
        collMain = collTpt
	End Sub
	Private Sub SortNum2(ByRef collMain As Collection, ByRef collApend As Collection)
		Dim i, j As Integer
		Dim collTpt As New Collection
		Dim collTptP As New Collection
        Dim testStrB, testStrA, testStrC As Double
		Dim testStrBP, testStrAP, testStrCP As Double
        collTpt.Clear() 'collTpt = Nothing		
        collTptP.Clear() 'collTptP = Nothing
		collTpt.Add("-99999999999999")
		collTpt.Add("99999999999999")
		collTptP.Add("-99999999999999")
		collTptP.Add("99999999999999")
		For i = 1 To collMain.Count()
            testStrA = Val(collMain.Item(i))
            testStrAP = Val(collApend.Item(i))
			For j = 1 To collTpt.Count() - 1
                testStrB = Val(collTpt.Item(j))
                testStrC = Val(collTpt.Item(j + 1))
                testStrBP = Val(collTptP.Item(j))
                testStrCP = Val(collTptP.Item(j + 1))
				If testStrA >= testStrB And testStrA <= testStrC Then
					'MsgBox ("yes")
					collTpt.Add(collMain.Item(i),  ,  , j)
					collTptP.Add(collApend.Item(i),  ,  , j)
					GoTo nextA
				End If
			Next j
nextA: 
		Next i
		
		collTpt.Remove(1)
		collTpt.Remove(collTpt.Count())
		collTptP.Remove(1)
		collTptP.Remove(collTptP.Count())
		collMain = collTpt
		collApend = collTptP
	End Sub
	Private Sub SortNum1_Duplicate(ByRef collMain As Collection)
		Dim i, j As Integer
		Dim collTpt As New Collection
		Dim collTptP As New Collection
        Dim testStrB, testStrA, testStrC As Double
        collTpt.Clear() 'set collTpt = Nothing
		collTpt.Add("-99999999999999")
		collTpt.Add("99999999999999")
		
		For i = 1 To collMain.Count()
            testStrA = Val(collMain.Item(i))
			
			For j = 1 To collTpt.Count() - 1
                testStrB = Val(collTpt.Item(j))
                testStrC = Val(collTpt.Item(j + 1))
				
				If testStrA > testStrB And testStrA < testStrC Then
					'MsgBox ("yes")
					collTpt.Add(collMain.Item(i),  ,  , j)
					
					GoTo nextA
				End If
			Next j
nextA: 
		Next i
		
		collTpt.Remove(1)
		collTpt.Remove(collTpt.Count())
        collMain = collTpt
		
	End Sub
    Private Sub SortNum2_Duplicate(ByRef collMain As Collection, ByRef collApend As Collection)

        Dim i, j As Integer
        Dim collTpt As New Collection
        Dim collTptP As New Collection
        Dim testStrB, testStrA, testStrC As Double
        Dim testStrBP, testStrAP, testStrCP As Double
        collTpt.Clear() 'collTpt = Nothing
        collTptP.Clear() 'collTptP = Nothing
        collTpt.Add("-99999999999999")
        collTpt.Add("99999999999999")
        collTptP.Add("-99999999999999")
        collTptP.Add("99999999999999")
        For i = 1 To collMain.Count()
            testStrA = Val(collMain.Item(i))
            testStrAP = Val(collApend.Item(i))
            For j = 1 To collTpt.Count() - 1
                testStrB = Val(collTpt.Item(j))
                testStrC = Val(collTpt.Item(j + 1))
                testStrBP = Val(collTptP.Item(j))
                testStrCP = Val(collTptP.Item(j + 1))
                If testStrA > testStrB And testStrA < testStrC Then
                    'MsgBox ("yes")
                    collTpt.Add(collMain.Item(i), , , j)
                    collTptP.Add(collApend.Item(i), , , j)
                    GoTo nextA
                End If
            Next j
nextA:
        Next i

        collTpt.Remove(1)
        collTpt.Remove(collTpt.Count())
        collTptP.Remove(1)
        collTptP.Remove(collTptP.Count())
        collMain = collTpt
        collApend = collTptP
    End Sub
	Private Sub SortString(ByRef collMain As Collection, ByRef IndexKeyStr_0 As Short)
		Dim i, j As Integer
		Dim collsTpt As New Collection
		Dim starrayA() As String
		Dim starrayB() As String
		Dim starrayC() As String
        Dim testStrB, testStrA, testStrC As Double
        collsTpt.Clear() 'collsTpt = Nothing
		collsTpt.Add("-99999999999999,-99999999999999,-99999999999999")
		collsTpt.Add("99999999999999,99999999999999,99999999999999")
		For i = 1 To collMain.Count()
            starrayA = Split(collMain.Item(i), ",", -1, CompareMethod.Text)
			testStrA = Val(starrayA(IndexKeyStr_0))
			For j = 1 To collsTpt.Count() - 1
                starrayB = Split(collsTpt.Item(j), ",", -1, CompareMethod.Text)
				testStrB = Val(starrayB(IndexKeyStr_0))
                starrayC = Split(collsTpt.Item(j + 1), ",", -1, CompareMethod.Text)
				testStrC = Val(starrayC(IndexKeyStr_0))
				If testStrA >= testStrB And testStrA <= testStrC Then
					'MsgBox ("yes")
					collsTpt.Add(collMain.Item(i),  ,  , j)
					GoTo nextA
				End If
			Next j
nextA: 
		Next i
		
		collsTpt.Remove(1)
		collsTpt.Remove(collsTpt.Count())
		
		collMain = collsTpt
	End Sub
	Private Sub SortString_Duplicate(ByRef collsIn As Collection, ByRef IndexKeyStr_0 As Short)
		Dim i, j As Integer
		Dim collsTpt As New Collection
		Dim starrayA() As String
		Dim starrayB() As String
		Dim starrayC() As String

		Dim testStrB, testStrA, testStrC As Double
        collsTpt.Clear() 'collsTpt = Nothing
		collsTpt.Add("-99999999999999,-99999999999999,-99999999999999")
		collsTpt.Add("99999999999999,99999999999999,99999999999999")
		For i = 1 To collsIn.Count()
            starrayA = Split(collsIn.Item(i), ",", -1, CompareMethod.Text)
			testStrA = Val(starrayA(IndexKeyStr_0))
            For j = 1 To collsTpt.Count - 1
                starrayB = Split(collsTpt.Item(j), ",", -1, CompareMethod.Text)
                testStrB = Val(starrayB(IndexKeyStr_0))
                starrayC = Split(collsTpt.Item(j + 1), ",", -1, CompareMethod.Text)
                testStrC = Val(starrayC(IndexKeyStr_0))
                If testStrA > testStrB And testStrA < testStrC Then
                    'MsgBox ("yes")
                    collsTpt.Add(collsIn.Item(i), , , j)
                    GoTo nextA
                End If
            Next j
nextA: 
		Next i
		
		collsTpt.Remove(1)
		collsTpt.Remove(collsTpt.Count())
		
		collsIn = collsTpt
	End Sub
End Class