Balanced brackets: Difference between revisions

Content added Content deleted
m (→‎{{header|J}}: eliminate unneeded punctuation)
(Changed method of balance checking...)
Line 594: Line 594:


=={{header|Batch File}}==
=={{header|Batch File}}==
Uses a "Markov-like" algorithm <code>"[]"->null</code> to check if brackets are balanced.
<lang dos>:: Balanced Brackets Task from Rosetta Code Wiki
<lang dos>:: Balanced Brackets Task from Rosetta Code Wiki
:: Batch File Implementation
:: Batch File Implementation
Line 635: Line 636:
goto :EOF
goto :EOF
::/Generate strings of brackets.
::/Generate strings of brackets.

::Check for Balance...
::Check for Balance...
::Uses Markov Algorithm.
:checkforbalance
:checkforbalance
set counter=0
set "changes=!samp!"
:check_loop
set cyc=0
if "!changes!"=="" goto itsbal
:scanchars
if "!input!"=="!changes!" goto notbal
set currchar=!samp:~%counter%,1!

if "!currchar!"=="" (
set /a prev=%counter%-1
set input=!changes!
set "changes=!input:[]=!"
for %%A in ("!prev!") do set prevchar=!samp:~%%A,1!
goto check_loop
if "!prevchar!"=="[" goto :notbal
goto :itsbal
)
if %cyc%==0 (
if "!currchar!"=="]" goto :notbal
set /a cyc+=1
) else (
if "!currchar!"=="[" (set /a cyc+=1) else (set /a cyc-=1)
)
set /a counter+=1&goto scanchars
:itsbal
:itsbal