level 1
偌遥sama521
楼主
Public Class 圆括号配对<?xml:namespace prefix="o" ns="urn:schemas-microsoft-com:office:office"></?xml:namespace>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dia As New OpenFileDialog()
dia.InitialDirectory = "C:\"
dia.Filter = "TextFiles|*.txt|All Files|*.*"
If dia.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim r As New StreamReader(dia.FileName, Encoding.Default)
Dim s As String = r.ReadLine()(定义为字符串= r.readline)
Do While Not s Is Nothing
ListBox1.Items.Add(s)
s = r.ReadLine()
<?xml:namespace prefix="st1" ns="urn:schemas-microsoft-com:office:smarttags">Loop</?xml:namespace>
r.Close()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click(ByVal :按值传递,对应还有:ByRef,按形传递,sender,事件源,System.EventArgs
事件类型)
Dim j As Integer(d定义整数变量)
For i = 0 To ListBox1.Items.Count - 1
j = KuoHaoMatch(ListBox1.Items(i))
If j = 0 Then
ListBox2.Items.Add("左右括号配对")
Else
ListBox2.Items.Add(IIf(j > 0, "缺少" & j & "个右括号", "缺少" & j & "个左括号"))
End If
Next
End Sub
Private Function KuoHaoMatch(ByVal StrTemp As String) As Integer
Dim j As Integer, k As Integer
j = 0
k = 0
For i = 1 To Len(StrTemp)
If Mid(StrTemp, i, 1) = "(" Then
j = j + 1
ElseIf Mid(StrTemp, i, 1) = ")" Then
k = k + 1
End If
Next
KuoHaoMatch = j - k
End Function
End Class