0

私は、投票タイプのプログラムにシーケンシャル アクセス ファイルを使用することを組み込んだ Visual Basic のプログラムに取り組んでいます。ユーザーが投票の保存ボタンをクリックするたびに、投票数が保存されます。私がやろうとしているのは、アクセス ファイルで投票数を実際の数として表示することです。私のプログラムの現在の書き方では、投票が保存された回数だけ候補者の名前が表示されます。たとえば、perez が 4 回投票された場合、アクセス ファイルでは perez が 4 つの異なる行に表示されます。実際に投票された回数を表示するにはどうすればよいですか。カウンター変数を使用することを考えていますが、実際にそれを実装する方法がよくわかりません。これが私がこれまでに持っているものです。

Public Class Voter

Dim file As IO.File

Dim infile As IO.StreamReader

Dim outfile As IO.StreamWriter



Private Sub Voter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    outfile = IO.File.CreateText("Votes.txt")
End Sub

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
    lstResult.Items.Clear()
    'which buttons is clicked
    If radMark.Checked = True Then
        outfile.WriteLine(radMark.Text)
        radMark.Checked = False
    ElseIf radSheima.Checked = True Then
        outfile.WriteLine(radSheima.Text)
        radSheima.Checked = False
    ElseIf radSam.Checked = True Then
        outfile.WriteLine(radSam.Text)
        radSam.Checked = False
    Else
        MessageBox.Show("You should select one among them")
    End If
End Sub

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
    'Dim Mark, Sheima, Sam As Integer
    Dim Mark As Integer = 0
    Dim Sheima As Integer = 0
    Dim Sam As Integer = 0
    Dim name As String
    'Mark = Sheima = Sam = 0
    outfile.Close()
    infile = IO.File.OpenText("Votes.txt")
    'keep track of votes
    While Not infile.EndOfStream
        name = infile.ReadLine()
        If name.Equals("Mark Stone") Then
            Mark += 1
        ElseIf name.Equals("Sheima Patel") Then
            Sheima += 1
        Else
            Sam += 1
        End If
    End While
    'results
    lstResult.Items.Clear()
    lstResult.Items.Add("Mark Stone     " & CStr(Mark))
    lstResult.Items.Add("Shemia Patel   " & CStr(Sheima))
    lstResult.Items.Add("Sam Perez      " & CStr(Sam))
    infile.Close()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Me.Close()

End Sub
End Class
4

1 に答える 1

2

btnVote_Click 関数では、クリックするたびにラジオボタンのテキストをファイルに書き込みます。これが問題の作成方法です。

また、名前がファイルに表示される回数を投票数として数えていますが、数ではありません。

名前だけでなく、投票ファイルに名前とカウントを入れてみてください。

マーク、1
シーマ、2 セイマ
、5

次に、[投票] ボタンをクリックすると、Mark の番号を読み取り、1 ずつインクリメントして書き戻す必要があります。

これを試して、

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
    lstResult.Items.Clear()
    'which buttons is clicked
    If radMark.Checked = True Then
        AddCount(radMark.Text)
        radMark.Checked = False
    ElseIf radSheima.Checked = True Then
        AddCount(radSheima.Text)
        radSheima.Checked = False
    ElseIf radSam.Checked = True Then
        AddCount(radSam.Text)
        radSam.Checked = False
    Else
        MessageBox.Show("You should select one among them")
    End If
End Sub

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click

    'results
    lstResult.Items.Clear()
    lstResult.Items.Add("Mark Stone     " & GetCount(radMark.Text))
    lstResult.Items.Add("Shemia Patel   " & CStr(radSheima.Text))
    lstResult.Items.Add("Sam Perez      " & CStr(radSam.Text))

End Sub

Private Sub AddCount(Byval VoteName As String)

    infile = New IO.File.StreamReader("Votes.txt")

    Dim whole_line As String
    Dim person As String
    Dim vote_count As Integer
    Dim found as boolean

    'read through the whole file until the end or find the name
    Do While infile.Peek() >= 0 And person <> VoteName

        'read each line
        whole_line = infile.ReadLine

        person = Split(whole_line, ",")(0)

        If person = VoteName Then

            vote_count = Split(whole_line, ",")(1)


            found = True
        End If
    Loop

    'Close the file after it is used.
    infile.Close()


    'Reopen the file with the StreamWriter
    outfile = IO.File.OpenText("Votes.txt")

    If found = True Then
        'the line will only be replace if the person name is found in the line
        whole_line = Whole_line.replace(VoteName & "," & vote_count, VoteName & "," & vote_count + 1)

        'Write back into the file
        outfile.WriteLine(whole_line)
    Else
        'if the vote name is not found in the file
        'Then create a new one
        outfile.WriteLine(VoteName & ",1" & VbCrLf)
    End If

        outfile.Close()

End Sub

Private Function GetCount(Byval VoteName As String)
    infile = New IO.File.StreamReader("Votes.txt")      

    Dim whole_line As String
    Dim person As String
    Dim vote_count As Integer

    'read through the whole file
    Do While infile.Peek() >= 0 And person <> VoteName

        'read each line
        whole_line = infile.ReadLine

        person = Split(Whole_line, ",")(0)

        If person = VoteName Then
            vote_count = Split(whole_line, ",")(1)
        End If
    Loop

    infile.Close()
    Return vote_count
End Sub
于 2012-04-27T00:57:59.213 に答える