2013-03-20 51 views
0

我需要從特定文本文件(C:\ test.txt)中提取數據的幫助。文本文件包含學生姓名和分數:如何使用字符串操作方法在VB 2010中從文本文件中提取和處理數據

愛麗絲,76,45,87,23 奔,76,48,85,65 朱莉,76,36,49,86 莫妮卡,85,90,83, 76

鑑於級結構,其:A(70-100),B(60-69),C(50-59),d(40-49),F(0-39)

應用程序應計算每個學生接收的平均分數和分數 顯示誰收到最高分 顯示該班級中所有學生的平均分數 收到分數「C」的學生名單

使用所必需的方法萊恩,中,INSTR 文件由線

處理線

的幫助非常感謝!

回答

0

它可以被整理起來,因爲我只是把它敲掉了。

要測試它,在窗體上放置多行TEXTBOX(名爲textbox1)和Button(名爲button1),然後單擊按鈕。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 

'-> Data 
' Alice, 76, 45, 87, 23 
' Ben, 76, 48, 85, 65 
' Julie, 76, 36, 49, 86 
' Monica, 85, 90, 83, 76 

'-> Variables 
Dim strTextLine As String 
Dim intCounter As Integer 
Dim chrCurrentCharacter As String 
Dim strBuffer As String 
Dim strResults As String 
Dim booNameFound As Boolean 

'-> Processing 
'1 calculate the average score and grade received by each student 
Dim intIndividualAverage As Integer = 0 
'2 Display who received the highest mark 
Dim intHighest As Integer = 0 
Dim intTmpHighest As Integer = 0 
Dim strHighest As String = "" 
'3 Display the average mark of all students in the class 
Dim intGroupAverage As Integer = 0 
'4 List students who received grade "C" 
Dim strGradeCStudents = "" 
Dim strName As String = "" 

'-> Grading Rules 
' A (70-100), B(60-69),C(50-59),D(40-49),F(0-39) 

'-> Open file and process data one line at a time 
Try 
    ' Create an instance of StreamReader to read from a file 
    Dim objStreamReader As StreamReader = New StreamReader("c:\temp\test.txt") 

    ' Read the lines from the file until the end of the file is reached. 
    Do 
    strTextLine = objStreamReader.ReadLine() 
    'if not eof or empty text line 
    If Trim(strTextLine) <> "" Then 
     'reset vars 
     strName = "" 
     strBuffer = "" 
     strResults = "" 
     booNameFound = False 
     intIndividualAverage = 0 
     For intCounter = 1 To Len(strTextLine) 
     chrCurrentCharacter = Mid(strTextLine, intCounter, 1) 
     'is this a comma? 
     If chrCurrentCharacter = "," Then 
      'has the name been found? 
      If booNameFound Then 
      'this is a grade result(note there is no Grade E!) 
      Select Case Val(strBuffer) 
       Case 70 To 100 
       strResults = strResults & " A" 
       intIndividualAverage = intIndividualAverage + Val(strBuffer) 
       intGroupAverage = intGroupAverage + Val(strBuffer) 
       Case 60 To 69 
       strResults = strResults & " B" 
       intIndividualAverage = intIndividualAverage + Val(strBuffer) 
       intGroupAverage = intGroupAverage + Val(strBuffer) 
       Case 50 To 59 
       strResults = strResults & " C" 
       intIndividualAverage = intIndividualAverage + Val(strBuffer) 
       intGroupAverage = intGroupAverage + Val(strBuffer) 
       'grade C Students 
       If InStr(strGradeCStudents, strName) > 0 Then 
        'alreadylisted inthe grade c list 
       Else 
        strGradeCStudents = strGradeCStudents & strName & " " 
       End If 
       Case 40 To 49 
       strResults = strResults & " D" 
       intIndividualAverage = intIndividualAverage + Val(strBuffer) 
       intGroupAverage = intGroupAverage + Val(strBuffer) 
       Case Else 
       strResults = strResults & " F" 
       intIndividualAverage = intIndividualAverage + Val(strBuffer) 
       intGroupAverage = intGroupAverage + Val(strBuffer) 
      End Select 
      strBuffer = "" 
      Else 
      'this is name 
      booNameFound = True 
      strResults = strBuffer 
      strName = strBuffer 
      strBuffer = "" 
      End If 
      'if its not a space 
     ElseIf chrCurrentCharacter <> " " Then 
      strBuffer = strBuffer & chrCurrentCharacter 
     Else 
      'Spaces are not processed 
     End If 
     Next 
     'Process LAST result because there is no comma after it 
     'so it must be done after the endof the line 
     Select Case Val(strBuffer) 
     Case 70 To 100 
      strResults = strResults & " A" 
      intIndividualAverage = intIndividualAverage + Val(strBuffer) 
     Case 60 To 69 
      strResults = strResults & " B" 
      intIndividualAverage = intIndividualAverage + Val(strBuffer) 
     Case 50 To 59 
      strResults = strResults & " C" 
      intIndividualAverage = intIndividualAverage + Val(strBuffer) 
     Case 40 To 49 
      strResults = strResults & " D" 
      intIndividualAverage = intIndividualAverage + Val(strBuffer) 
     Case Else 
      strResults = strResults & " F" 
      intIndividualAverage = intIndividualAverage + Val(strBuffer) 
     End Select 
     strBuffer = "" 
     'Student average 
     strResults = strResults & " (avg=" & (intIndividualAverage/4) & ")" & vbCrLf & vbCrLf 
     TextBox1.Text = TextBox1.Text & strResults 
     'Console.WriteLine(strResults) 
     'Highest? 
     If intHighest = 0 Then 
     strHighest = strName 
     ElseIf intTmpHighest > intHighest Then 
     strHighest = strName 
     ElseIf intTmpHighest = intHighest Then 
     strHighest = strHighest & " " & strName 
     End If 
    Else 
     'The line was empty 
    End If 
    Loop Until strTextLine Is Nothing 
    objStreamReader.Close() 
    '-> Display Summary 
    TextBox1.Text = TextBox1.Text & vbCrLf & "SUMMARY" & vbCrLf 
    TextBox1.Text = TextBox1.Text & "Highest Marks: " & strHighest & vbCrLf 
    TextBox1.Text = TextBox1.Text & "Group Average: " & ((intGroupAverage/4)/4) & vbCrLf 
    TextBox1.Text = TextBox1.Text & "Grade C Students: " & strGradeCStudents & vbCrLf 
Catch Ex As Exception 
    ' Let the user know what went wrong. 
    TextBox1.Text = TextBox1.Text & "The file could not be read:" 
    TextBox1.Text = TextBox1.Text & Ex.Message 
    'Console.WriteLine("The file could not be read:") 
    'Console.WriteLine(Ex.Message) 
End Try 
End Sub 
+0

另外不要忘記將文件名/路徑從c:\ TEMP \ test.txt更改爲c:\ test.txt – Zeddy 2013-03-20 02:29:11

相關問題