Search the Community
Showing results for tags 'balanced'.
-
This function will check for balanced parentheses in an expression just a simple exercise for fun (see Edit: today (july 5 2015) I discovered that the original listing that was here was lost for some reason (maybe one of the many issues arose after forum upgrades) I found a version of that function on my HD and I post again (hope it is the same version that was here before) ; https://gist.github.com/mycodeschool/7207410 ; Local $sExpr = "[{(1 + 3i)/((-1 - i)^2) + (-4 + i)(-4 - i)/(1 + i)}*{(1 + 3i)/((-1 - i)^2) + (-4 + i)(-4 - i)/(1 + i)}]" MsgBox(0, "Parenthesis check", AreParanthesesBalanced($sExpr)) Exit ; -------------------------------------------------------- ; Returns True if parenthesis in $sExpression are balanced ; -------------------------------------------------------- Func AreParanthesesBalanced($sExpression) Local $sStack = "" For $i = 1 To StringLen($sExpression) If StringInStr('({[', StringMid($sExpression, $i, 1)) Then ; opening parenthesis $sStack &= StringMid($sExpression, $i, 1) ElseIf StringInStr(')}]', StringMid($sExpression, $i, 1)) Then ; a closing parenthesis If Not StringLen($sStack) Or Not _ArePair(StringRight($sStack, 1), StringMid($sExpression, $i, 1)) Then Return False Else $sStack = StringLeft($sStack, StringLen($sStack) - 1) EndIf EndIf Next Return Not StringLen($sStack) ? True : False #cs ; alternative for old AutoIt versions ; without ternary operator If Not StringLen($sStack) Then Return True Else Return False EndIf #ce EndFunc ;==>AreParanthesesBalanced Func _ArePair($sOpening, $sClosing) If ($sOpening = '(' And $sClosing = ')') Then Return True If ($sOpening = '{' And $sClosing = '}') Then Return True If ($sOpening = '[' And $sClosing = ']') Then Return True Return False EndFunc ;==>ArePair