0

我需要有关从特定文本文件 (C:\test.txt) 中提取数据的帮助。文本文件包含学生姓名和分数:

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

给定等级结构:A (70-100), B(60-69),C(50-59),D(40-49),F(0-39)

应用程序应计算每个学生获得的平均分和成绩 显示获得最高分的学生 显示班级所有学生的平均分 列出获得“C”级的学生

需要使用的方法是 Len, Mid 和 Instr File 逐行处理

非常感谢您的帮助!

4

1 回答 1

0

可以证明它可以被整理,因为我刚刚把它打掉了。

要对其进行测试,请在表单上放置一个多行文本框(名为 textbox1)和一个按钮(名为 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
于 2013-03-20T02:27:23.910 回答