Gray code: Difference between revisions
m
→{{header|Batch File}}: less code, but slower (TnT)
Simple9371 (talk | contribs) (→{{header|Ursala}}: Added VBScript) |
Simple9371 (talk | contribs) m (→{{header|Batch File}}: less code, but slower (TnT)) |
||
Line 549:
<lang dos>:: Gray Code Task from Rosetta Code
:: Batch File Implementation
@echo off
rem -------------- define batch file macros with parameters appended
Line 562:
rem note: if natnum is negative, then !outputvar! is empty
set tobinary=for %%# in (1 2) do if %%#==2 ( %\n%
for /f "tokens=1,2,3" %%a in ("!args!") do (
set "!outputvar!=" %\n%
if !natnum! geq 0 ( %\n%
Line 574 ⟶ 575:
) else set args=
goto :main-thing %== jump to the main thing ==%
rem -------------- usual "call" sections
rem argument: binary bitlength outputvar▼
rem the sad disadvantage of using these is that they are slow (TnT)
▲ for /f "tokens=1,2,3" %%a in ("!args!") do (set "binary=%%a"^&set "bitlength=%%b"^&set "outputvar=%%c") %\n%
rem gray code
rem argument:
:encoder
set /a "%~2=%~1^(%~1>>1)"
goto :eof
rem gray code decoder
:decoder
:while-loop-1
if %inp% gtr 0 (
set /a
goto while-loop-1
)
goto :eof
rem -------------- main thing
:main-thing
setlocal enabledelayedexpansion
echo(# -^> bin -^> enc -^> dec
for /l %%n in (0,1,31) do (
%tobinary% %%n 5 bin
%
%tobinary% !dec! 5 rebin
echo(%%n -^> !bin! -^> !gray! -^> !rebin!
)
exit /b 0</lang>
|