|
Answer» So I have a program that reorganizes math equations into a different format that some friends showed me. Instead of writing 5+7 it would be written 57+. Or instead of 1+4/2 it would be 42/1+. It started to get a little complicated when (1+4)/(4+6) came around, which should be 14+46+/. To handle the parenthesis I am trying to call a new instance of my batch file with the operation withing the parenthesis to store in that space. The script I have below stores each character in a different slot in a psudoarray, and them moves them around based on what they are and uses a second parallel array to handle order of operations.
Code: (Example of Steps) [SELECT]T:\>math 5+2/4 *** start *** OP = 5+2/4
array1_0=5 array1_1=+ array1_2=2 array1_3=/ array1_4=4 array2_0=0 array2_1=2 array2_2=0 array2_3=1 array2_4=0 array2_5=0
array1_0=5 array1_1=+ array1_4=24/ array2_0=0 array2_1=2 array2_2=0 array2_3=1 array2_4=0 array2_5=0
array1_0=5 array1_1=+ array1_4=24/ array2_0=0 array2_1=1 array2_2=0 array2_3=0 array2_4=0 array2_5=0
array1_4=524/+ array2_0=0 array2_1=1 array2_2=0 array2_3=0 array2_4=0 array2_5=0
array1_4=524/+ array2_0=0 array2_1=0 array2_2=0 array2_3=0 array2_4=0 array2_5=0
array1_4=524/+ array2_0=0 array2_1=0 array2_2=0 array2_3=0 array2_4=0 array2_5=0
524/+
The problem I think I'm running into is that when I call a new instance of the program to convert 1+4 to 14+, it is passing the FULL array in as well. How would I go about stopping this? My code is below. :open is the function that does the calling. There are currently some debug commands thrown in to show the CHANGE in my arrays. They will be removed.
Code: (math.bat) [Select]echo off setlocal EnableDelayedExpansion setlocal set op=%~1 set a=-1 set "self=%~pdnx0"
echo *** start *** echo op = %op%
REM set up array1 to contain the characters of %op% :a set /a a+=1 set working=!op:~%a%,1! if not "%working%"=="" ( set "array1_%a%=%working%" goto :a ) set op_size=%a%
REM set up array2 to contain order of operations starting from 1 REM a value of 0 signifies no operation to run, but a digit to MOVE set b=-1 set count=1 :b set /a b+=1 if not defined array1_%b% set array2_%b%=
set z=%b% set tocall= if "!array1_%b%:~0,1!"=="(" call :open set b=%z%
if "!array1_%b%:~0,1!"=="/" ( set array2_%b%=%count% set /a count+=1 ) if "!array1_%b%:~0,1!"=="*" ( set array2_%b%=%count% set /a count+=1 ) if not "%b%"=="%op_size%" goto :b set b=-1 :c set /a b+=1 if "!array1_%b%:~0,1!"=="+" ( set array2_%b%=%count% set /a count+=1 ) if "!array1_%b%:~0,1!"=="-" ( set array2_%b%=%count% set /a count+=1 ) if not "%b%"=="%op_size%" goto :c set b=-1 :d set /a b+=1 if "!array2_%b%!"=="" set array2_%b%=0 if not "%b%"=="%op_size%" goto :d set a= set b=
echo. set array echo.
REM locate operations before and after first operation :locate set loc=0 set a=-1 set last=-1 set next= :e set /a a+=1 if not "!array2_%a%!"=="0" ( if not "!array2_%a%!"=="1" ( if "%loc%"=="0" set last=%a% if "%loc%"=="1" ( set next=%a% goto :combine ) ) ) if "!array2_%a%!"=="1" set loc=1 if not "%a%"=="%op_size%" goto :e set next=%a% goto :combine :combine set working= set /a next=%next%-1 :f set /a last=%last%+1 if not "!array2_%last%!"=="1" ( set "working=%working%!array1_%last%!" set "array1_%last%=" ) else ( set "store=!array1_%last%!" set "array1_%last%=" ) if not "%last%"=="%next%" goto :f set "working=%working%%store%" set "array1_%next%=%working%"
echo. set array echo.
REM Check if done set g=0 for /f "tokens=2 delims==" %%A in ('set array2') do if not "%%A"=="0" set g=1 set a=0 set z= set b=-1 set count=1 set next= set last= set working= set store= if "%g%"=="1" goto :b
REM Echo Output and exit set output= set a=-1 :out set /a out+=1 if defined array1_%out% ( set output=!array1_%out%! goto :end ) goto :out :end echo %output% goto :exit
:open set "array1_%z%=" set /a z+=1
if not "!array1_%z%:~0,1!"==")" ( set "tocall=%tocall%!array1_%z%!" set "array1_%z%=" set "array2_%z%=: goto :open ) echo :open %tocall% set "array1_%z%="
for /f "delims=" %%A in ('call %self% %tocall%') do set array1_%z%=%%A echo !array1_%z%! goto :eof
:exit
EDIT: I know the system is somewhat stupid, but I'm doing it as more a challenge than to produce anything useful.If it helps, the script stalls on the for command in :open, but only if there are two or more ( characters.I found a work around if anyone is interested.
Code: [Select]echo off setlocal EnableDelayedExpansion if not "%~2"=="" setlocal set op=%~1 set a=-1 set "self=%~pdnx0" for /f "tokens=1 delims==" %%A in ('set array 2^>nul') do set "%%A="
REM set up array1 to contain the characters of %op% :a set /a a+=1 set working=!op:~%a%,1! if not "%working%"=="" ( set "array1_%a%=%working%" goto :a ) set op_size=%a%
REM set up array2 to contain order of operations starting from 1 REM a value of 0 signifies no operation to run, but a digit to move set b=-1 set count=1 :b set /a b+=1 if not defined array1_%b% set array2_%b%=
if "!array1_%b%:~0,1!"=="(" call :open
if "!array1_%b%:~0,1!"=="/" ( set array2_%b%=%count% set /a count+=1 ) if "!array1_%b%:~0,1!"=="*" ( set array2_%b%=%count% set /a count+=1 ) if not "%b%"=="%op_size%" goto :b set b=-1 :c set /a b+=1 if "!array1_%b%:~0,1!"=="+" ( set array2_%b%=%count% set /a count+=1 ) if "!array1_%b%:~0,1!"=="-" ( set array2_%b%=%count% set /a count+=1 ) if not "%b%"=="%op_size%" goto :c set b=-1 :d set /a b+=1 if "!array2_%b%!"=="" set array2_%b%=0 if not "%b%"=="%op_size%" goto :d set a= set b=
REM locate operations before and after first operation :locate set loc=0 set a=-1 set last=-1 set next= :e set /a a+=1 if not "!array2_%a%!"=="0" ( if not "!array2_%a%!"=="1" ( if "%loc%"=="0" set last=%a% if "%loc%"=="1" ( set next=%a% goto :combine ) ) ) if "!array2_%a%!"=="1" set loc=1 if not "%a%"=="%op_size%" goto :e set next=%a% goto :combine :combine set working= set /a next=%next%-1 :f set /a last=%last%+1 if not "!array2_%last%!"=="1" ( set "working=%working%!array1_%last%!" set "array1_%last%=" ) else ( set "store=!array1_%last%!" set "array1_%last%=" ) if not "%last%"=="%next%" goto :f set "working=%working%%store%" set "array1_%next%=%working%"
REM Check if done set g=0 for /f "tokens=2 delims==" %%A in ('set array2') do if not "%%A"=="0" set g=1 set a=0 set z= set b=-1 set count=1 set next= set last= set working= set store= if "%g%"=="1" goto :b
REM Echo Output and exit set output= set a=-1 :out set /a out+=1 if defined array1_%out% ( set output=!array1_%out%! goto :end ) goto :out :end echo %output% endlocal goto :exit
REM comine equations in paranthisis and convert them :open
set last=%b% set o=%b% set "array1_%o%=" :op set /a o+=1 if not "!array1_%o%!"==")" goto :op set next=%o%
set working= set /a next=%next%-1 :f2 set /a last=%last%+1 if not "!array1_%last%!"=="+" ( if not "!array1_%last%!"=="/" ( if not "!array1_%last%!"=="*" ( if not "!array1_%last%!"=="-" ( set "working=%working%!array1_%last%!" set "array1_%last%=" ) else ( set "store=!array1_%last%!" set "array1_%last%=" ) ) else ( set "store=!array1_%last%!" set "array1_%last%=" ) ) else ( set "store=!array1_%last%!" set "array1_%last%=" ) ) else ( set "store=!array1_%last%!" set "array1_%last%=" ) if not "%last%"=="%next%" goto :f2 set "working=%working%%store%" set "array1_%next%=%working%" set /a next+=1 set "array1_%next%="
goto :eof
:exit
|