Answer Posted / jerry prince
Option Explicit
'
' Student record user defined type.
'
Private Type tStud
RegNO As String * 9
ContactAddress As String * 40
StudentName As String * 30
Phone As String * 24
Sex As String * 10
End Type
'
' Array of students
'
Private StudArray() As tStud
Private StudRec As tStud
Private RecCt As Long
Private Sub cmdQuit_Click()
End
End Sub
Private Sub cmdSearchArray_Click()
Dim lngMatch As Long
'
' Search the array for a record.
'
lngMatch = fSearchArray(txtNO)
'
' If found, display the record.
'
If lngMatch Then
Call pShowArrayRecord(lngMatch)
Else
Call ClearRecord
End If
End Sub
Private Sub cmdSearchFile_Click()
Dim lngMatch As Long
'
' Search the file for a record.
'
lngMatch = fSearchFile(txtNO)
'
' If found, display the record.
'
If lngMatch Then
Call pShowFileRecord(lngMatch)
Else
Call ClearRecord
End If
End Sub
Private Sub Form_Load()
Dim GetStud As tStud
Dim l As Long
'
' Load an array with data from the file and
' load the listbox with the Reg NO from each record.
'
Open "StudList.dat" For Random As 1 Len = Len(GetStud)
RecCt = LOF(1) / Len(GetStud)
ReDim StudArray(1 To RecCt)
For l = 1 To RecCt
Get 1, l, StudArray(l)
lstRegNO.AddItem StudArray(l).RegNO
Next
Close 1
End Sub
Private Sub pShowArrayRecord(lngRecord As Long)
'
' Display a record from the array in the textboxes.
'
txtRegNO = StudArray(lngRecord).RegNO
txtContactAddress = StudArray(lngRecord).ContactAddress
txtStudentName = StudArray(lngRecord).StudentName
txtPhone = StudArray(lngRecord).Phone
txtSex = StudArray(lngRecord).Sex
End Sub
Private Sub pShowFileRecord(lngRecord As Long)
'
' Display a record from the file in the textboxes.
'
txtRegNO = StudRec.RegNO
txtContactAddress = StudRec.ContactAddress
txtStudentName = StudRec.StudentName
txtPhone = StudRec.Phone
txtSex = StudRec.Sex
End Sub
Private Sub ClearRecord()
'
' Clear the text boxes
'
txtRegNO = ""
txtContactAddress = ""
txtStudentName = ""
txtPhone = ""
txtSex = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
Erase StudArray
Set frmSearch = Nothing
End Sub
Private Sub lstRegNO_Click()
'
' Display the selected listbox item in the textbox.
'
txtNO = lstRegNO.Text
End Sub
Private Function fSearchArray(strSearchItem As String) As Long
Dim lngFirst As Long
Dim lngLast As Long
Dim lngMiddle As Long
Dim lngLastPass As Long
Dim strItem As String
Dim strValue As String
Dim blnDone As Boolean
'
' Search an array for an item using a binary search.
' The search is not case sensitive.
' Returned is the index of the matching array element.
'
'
' Initialize the pointers to the first
' and last records.
'
lngFirst = 1
lngLast = UBound(StudArray)
strItem = UCase$(Trim$(strSearchItem))
'
' If only one record, see if it is the desired one.
'
If lngLast = 1 Then
If strItem = UCase$(StudArray(1).RegNO) Then
fSearchArray = 1
Else
fSearchArray = 0
End If
Exit Function
End If
'
' Set the pointer to the middle record.
'
lngMiddle = ((lngLast - lngFirst) + 1) \ 2
'
' Apply the binary search criteria until the
' item is found or the list is exhausted.
'
Do Until blnDone
strValue = UCase$(StudArray(lngMiddle).RegNO)
If strItem = strValue Then
'
' Found it.
'
fSearchArray = lngMiddle
blnDone = True
Exit Do
ElseIf strItem < strValue Then
'
' Direction = down
' Remove the second half of the list.
'
lngLast = lngMiddle
lngMiddle = lngMiddle - ((lngLast - lngFirst) +
1) \ 2
ElseIf strItem > strValue Then
'
' Direction = Up
' Remove the first half of the list.
'
lngFirst = lngMiddle
lngMiddle = lngMiddle + ((lngLast - lngFirst) +
1) \ 2
End If
'
' See if list is still divisible.
'
If (lngMiddle = lngFirst) Or (lngMiddle = lngLast) Then
lngLastPass = lngLastPass + 1
If lngLastPass = 2 Then
lngLastPass = 0
fSearchArray = 0
blnDone = True
End If
End If
Loop
End Function
Private Function fSearchFile(strSearchItem As String) As Long
Dim lngFirst As Long
Dim lngLast As Long
Dim lngMiddle As Long
Dim lngLastPass As Long
Dim strItem As String
Dim strValue As String
Dim blnDone As Boolean
Open "StudList.dat" For Random As 1 Len = Len(StudRec)
RecCt = LOF(1) / Len(StudRec)
'
' Search a file for an item using a binary search.
' The search is not case sensitive.
' Returned is the index of the matching file element.
'
'
' Initialize the pointers to the first
' and last records.
'
lngFirst = 1
lngLast = RecCt
strItem = UCase$(Trim$(strSearchItem))
'
' If only one record, see if it is the desired one.
'
If lngLast = 1 Then
Get 1, 1, StudRec
If strItem = UCase$(StudRec.RegNO) Then
fSearchFile = 1
Else
fSearchFile = 0
End If
Close 1
Exit Function
End If
'
' Set the pointer to the middle record.
'
lngMiddle = ((lngLast - lngFirst) + 1) \ 2
'
' Apply the binary search criteria until the
' item is found or the file is exhausted.
'
Do Until blnDone
'
' Read a record from the file.
'
Get 1, lngMiddle, StudRec
strValue = UCase$(StudRec.RegNO)
If strItem = strValue Then
'
' Found it.
'
fSearchFile = lngMiddle
blnDone = True
Exit Do
ElseIf strItem < strValue Then
'
' Direction = down
' Remove the second half of the file.
'
lngLast = lngMiddle
lngMiddle = lngMiddle - ((lngLast - lngFirst) +
1) \ 2
ElseIf strItem > strValue Then
'
' Direction = Up
' Remove the first half of the file.
'
lngFirst = lngMiddle
lngMiddle = lngMiddle + ((lngLast - lngFirst) +
1) \ 2
End If
'
' See if file is still divisible.
'
If (lngMiddle = lngFirst) Or (lngMiddle = lngLast) Then
lngLastPass = lngLastPass + 1
If lngLastPass = 2 Then
lngLastPass = 0
fSearchFile = 0
blnDone = True
End If
End If
Loop
Close 1
End Function
Private Sub txtID_KeyPress(KeyAscii As Integer)
'
' Convert to upper case.
'
KeyAscii = Asc(UCase$(Chr(KeyAscii)))
End Sub
Is This Answer Correct ? | 4 Yes | 12 No |
Post New Answer View All Answers
How will you sort the elements of array in descending order?
Define non linear data structure.
List some applications of tree-data structure?
What is default array size?
What is data structure? Explain.
What is a subtree in data structures?
Explain set interface?
Differentiate between comparable and comparator.
What member function places a new node at the end of the linked list?
Draw a binary Tree for the expression : A * B - (C + D) * (P / Q)
What are the properties of binary heap?
Can tuple be sorted?
Is an arraylist an object?
Explain what are the types of collision resolution techniques and the methods used in each of the type?
What do you know about the big-o notation and can you give some examples with respect to different data structures?