求个小算法,关于括号的⋯⋯
时间:2011-10-11
来源:互联网
有一个表达式,类似:1+(((((3-5))+((2)))))+2
改为:1+((3-5)+2)+2
改为:1+((3-5)+2)+2
作者: ybh37 发布时间: 2011-10-11

偷懒了⋯⋯
大家有好办法吗?没有的话⋯⋯
作者: ybh37 发布时间: 2011-10-11
较复杂啊,算法的话就是判断所有的双括号是否满足删除条件。
作者: sysdzw 发布时间: 2011-10-11
正序存栈
反序存栈
数
反序存栈
数
作者: alifriend 发布时间: 2011-10-11
都是加啊减的,怎么不能改成 1+3-5+2+2呢
作者: king06 发布时间: 2011-10-11
为了挣分拼了。
Option Explicit
VB code
Option Explicit
VB code
Private Sub Form_Load() Dim s$, isContinue As Boolean s = "1+(((((3-5))+((2)))))+2" Dim s2$, v, i%, j%, i1%, i2% MsgBox getMatch(s, 3) Do isContinue = False For i = 1 To Len(s) If Mid(s, i, 1) = "(" Then i1 = getMatch(s, i) If Mid(s, i + 1, 1) = "(" Then '两个( i2 = getMatch(s, i + 1) If i1 - i2 = 1 Then s = Left(s, i - 1) & Mid(s, i + 1, i2 - i - 1) & Mid(s, i2 + 1) isContinue = True End If ElseIf i1 - i = 2 Then '类似(2) s = Left(s, i - 1) & Mid(s, i + 1, 1) & Mid(s, i + 3) isContinue = True End If End If Next If Not isContinue Then Exit Do Loop MsgBox s End Sub Private Function getMatch(strKey$, intPos%) As Integer Dim s$, v, i%, j%, intLCount%, intRCount% Dim strTmp$ For i = intPos To Len(strKey) strTmp = Mid(strKey, i, 1) If strTmp = "(" Then intLCount = intLCount + 1 ElseIf strTmp = ")" Then intRCount = intRCount + 1 If intRCount = intLCount Then getMatch = i Exit Function End If End If Next End Function
作者: sysdzw 发布时间: 2011-10-11
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28