急求使用栈检查表达式(2+3)*a*(3+b)/(2*(12+8)的括号是否匹配(使用VB语言)
答案:2 悬赏:50 手机版
解决时间 2021-03-31 05:32
- 提问者网友:皆是孤独
- 2021-03-30 12:16
急求使用栈检查表达式(2+3)*a*(3+b)/(2*(12+8)的括号是否匹配(使用VB语言)
最佳答案
- 五星知识达人网友:野味小生
- 2021-03-30 13:35
VB没有指针啊。真要用栈的话必须要指针。VB虽然没有但是有相应的win API函数。CopyMemory可以实现,比较麻烦。
可以用动态数组实现,仿栈的形式,其实也差不多。
Private Sub Command1_Click()
If check(Text1.Text) = True Then
MsgBox "匹配", vbInformation
Else
MsgBox "不匹配", vbCritical
End If
End Sub
Function check(ByVal str As String) As Boolean
Dim n() As String '动态数组
Dim i, j As Integer
Do
i = i + 1
If Mid(str, i, 1) = "(" Or Mid(str, i, 1) = ")" Or Mid(str, i, 1) = "[" Or Mid(str, i, 1) = "]" Or Mid(str, i, 1) = "{" Or Mid(str, i, 1) = "}" Or Mid(str, i, 1) = "<" Or Mid(str, i, 1) = ">" Then
j = j + 1 '
ReDim Preserve n(j) '重新定义数组,并保留数据
n(j) = Mid(str, i, 1)
If (n(j - 1) = "<" And n(j) = ">") Or (n(j - 1) = "{" And n(j) = "}") Or (n(j - 1) = "(" And n(j) = ")") Or (n(j - 1) = "[" And n(j) = "]") Then
j = j - 2 '对消后,数组长度缩小
End If
End If
Loop Until (Len(str) + 1 = i)
MsgBox j
If j = 0 Then
check = True
Else
check = False
End If
End Function
可以用动态数组实现,仿栈的形式,其实也差不多。
Private Sub Command1_Click()
If check(Text1.Text) = True Then
MsgBox "匹配", vbInformation
Else
MsgBox "不匹配", vbCritical
End If
End Sub
Function check(ByVal str As String) As Boolean
Dim n() As String '动态数组
Dim i, j As Integer
Do
i = i + 1
If Mid(str, i, 1) = "(" Or Mid(str, i, 1) = ")" Or Mid(str, i, 1) = "[" Or Mid(str, i, 1) = "]" Or Mid(str, i, 1) = "{" Or Mid(str, i, 1) = "}" Or Mid(str, i, 1) = "<" Or Mid(str, i, 1) = ">" Then
j = j + 1 '
ReDim Preserve n(j) '重新定义数组,并保留数据
n(j) = Mid(str, i, 1)
If (n(j - 1) = "<" And n(j) = ">") Or (n(j - 1) = "{" And n(j) = "}") Or (n(j - 1) = "(" And n(j) = ")") Or (n(j - 1) = "[" And n(j) = "]") Then
j = j - 2 '对消后,数组长度缩小
End If
End If
Loop Until (Len(str) + 1 = i)
MsgBox j
If j = 0 Then
check = True
Else
check = False
End If
End Function
全部回答
- 1楼网友:零点过十分
- 2021-03-30 14:12
不匹配啊,分母那不是少了一个)追问这就是用栈来检查是否匹配,因为少了一个),我不会做了。。。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯