阅读和分析txt文件

时间:2012-10-21 00:55:35

标签: arrays vb.net structure

我试图编写一个代码,其中两个txt文件相互比较。一个文本文件的答案就像“TFTTFFT”,其他txt文件的结果包括id如“1234 TT-FFT”疯狂的部分是每个正确答案,学生得到4分,每个错误的答案,学生获得-1并且对于每个没有答案,在这种情况下用短划线( - )表示,学生得到零。我如何为此编写代码?非常感谢您的帮助。我不一定需要一个人为我编码,因为我已经完成了文件的打开并将它们存储在适当的变量中。只是反馈如何去做这件事会很好。提前谢谢。

更新: 我已经修改了整个代码,并将其用于考虑。谢谢Josh,感谢所有迄今为止贡献的人。请让我进一步了解你们对修改后的编码的看法。

更新:程序无效:(

2 个答案:

答案 0 :(得分:0)

由于我假设您刚刚开始编程,我将分享一些建议和解决问题的一般方法。

作为程序员,您需要发展的最重要的技能是能够将复杂的问题分解为简单易消化的块。我认为这个问题有三个不连续的部分。我将尝试用伪代码表达这些。

读取和解析文件

Open (Answer Key File)

Read All Text From (Answer Key File) into (String)

Convert (String) into array of characters as (Answer Key Array)

Close (Answer Key File)

Open (Student's File)

Read All Text From (Student's File) into (String)

Extract (Student ID) from (String)

Extract (Student's Answers) from (String)

Convert (Student's Answers) to Character Array as (Student Answers Array)

Close (Student's File)

比较学生对答案的答案和计算学生的分数

Set (Student Score) equal to '0'

FOR EACH (Answer) in (Student's Answers Array)

   Get (Key) from (Answer Key Array) at (Current Loop Index)

   IF (Answer) equals [NO_ANSWER] THEN
      Continue

   IF (Answer) equals (Key) THEN
      Add 4 to (Student Score)
   ELSE
      Subtract 1 from (Student Score)

您的整个程序应该只包含几种方法。任何更复杂的事情,你都在思考它;)

请记住,对您的任何程序进行此练习都很有帮助。更好的是,将它们作为注释写在源文件中,然后填写必要的代码。你会很快就会感到惊讶。

答案 1 :(得分:0)

    Dim answers As String = 'Text from the answor file
    Dim student As String = 'Text from the student file

    ' a will be used to hold the first char from the answers file
    ' s will be used to hold the first char from the student file
    Dim a As String
    Dim s As String

    ' veriable that holds the student grad
    Dim grade As Integer = 0

    ' loop while we still have an answer
    While answers.Length > 0

        ' get the first answer and student respond
        a = answers.First
        s = student.First

        ' compare the two and update the student grade accourdingly
        If a = "-" Then
            grade = grade - 1

        ElseIf a = s Then
            grade = grade + 4
        Else
            grade = grade - 1
        End If

        ' remove the first answer from the two strings
        answers = answers.Substring(1)
        student = student.Substring(1)
    End While


    return grade