home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Begin VB.Form frm8_2_1
- Caption = "8-2-1"
- ClientHeight = 1704
- ClientLeft = 1092
- ClientTop = 1488
- ClientWidth = 2676
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 7.8
- Charset = 0
- Weight = 700
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- LinkTopic = "Form1"
- PaletteMode = 1 'UseZOrder
- ScaleHeight = 1704
- ScaleWidth = 2676
- Begin VB.PictureBox picShowData
- Height = 855
- Left = 120
- ScaleHeight = 804
- ScaleWidth = 2364
- TabIndex = 1
- Top = 720
- Width = 2415
- End
- Begin VB.CommandButton cmdSort
- Caption = "Sort by Year of Birth"
- Height = 495
- Left = 120
- TabIndex = 0
- Top = 120
- Width = 2415
- End
- Attribute VB_Name = "frm8_2_1"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- Private Sub cmdSort_Click()
- Dim numPeople As Integer
- 'Sort data from YOB.TXT file by year of birth
- numPeople = NumberOfRecords(App.Path & "\YOB.TXT")
- ReDim nom(1 To numPeople) As String
- ReDim yearBorn(1 To numPeople) As Integer
- Call ReadData(nom(), yearBorn(), numPeople)
- Call SortData(nom(), yearBorn(), numPeople)
- Call ShowData(nom(), yearBorn(), numPeople)
- Call WriteData(nom(), yearBorn(), numPeople)
- End Sub
- Private Function NumberOfRecords(filespec As String) As Integer
- Dim nom As String, yearBorn As Integer
- Dim n As Integer 'Used to count records
- n = 0
- Open filespec For Input As #1
- Do While Not EOF(1)
- Input #1, nom, yearBorn
- n = n + 1
- Loop
- Close #1
- NumberOfRecords = n
- End Function
- Private Sub ReadData(nom() As String, yearBorn() As Integer, numPeople As Integer)
- Dim index As Integer
- 'Read data from file into arrays
- Open App.Path & "\YOB.TXT" For Input As #1
- For index = 1 To numPeople
- Input #1, nom(index), yearBorn(index)
- Next index
- Close #1
- End Sub
- Private Sub ShowData(nom() As String, yearBorn() As Integer, numPeople As Integer)
- Dim index As Integer
- 'Display the sorted list
- picShowData.Cls
- For index = 1 To numPeople
- picShowData.Print nom(index), yearBorn(index)
- Next index
- End Sub
- Private Sub SortData(nom() As String, yearBorn() As Integer, numPeople As Integer)
- Dim passNum As Integer, index As Integer
- 'Bubble sort arrays by year of birth
- For passNum = 1 To numPeople - 1
- For index = 1 To numPeople - passNum
- If yearBorn(index) > yearBorn(index + 1) Then
- Call SwapData(nom(), yearBorn(), index)
- End If
- Next index
- Next passNum
- End Sub
- Private Sub SwapData(nom() As String, yearBorn() As Integer, index As Integer)
- Dim stemp As String, ntemp As Integer
- 'Swap names and years
- stemp = nom(index)
- nom(index) = nom(index + 1)
- nom(index + 1) = stemp
- ntemp = yearBorn(index)
- yearBorn(index) = yearBorn(index + 1)
- yearBorn(index + 1) = ntemp
- End Sub
- Private Sub WriteData(nom() As String, yearBorn() As Integer, numPeople As Integer)
- Dim index As Integer
- 'Write data back into file
- Open App.Path & "\YOB.TXT" For Output As #1
- For index = 1 To numPeople
- Write #1, nom(index), yearBorn(index)
- Next index
- Close #1
- End Sub
-