- ::按照定义描述编码
- @echo off
- set /p m=请输入m:
- set /p n=请输入n:
- set /a l=m+n
- rem echo 位数: %m%+%n%=%l%
- title %l%位的格雷码
- set /a sup=1^<^<%l%
- set /a sup-=1
- set /a l-=1
- rem echo 最大值: %sup%
- setlocal enabledelayedexpansion
- rem 一行输出信息
- set "Spring="
- set /a Bother=1^<^<n
- for /l %%z in (0,1,%sup%) do (
- rem 获取二进制代码
- rem 不知道批处理怎么取二进制,我只能一位一位地凑
- set "BinCode="
- for /l %%i in (0,1,%l%) do (
- set /a y=1^<^<%%i
- set /a x=%%z^&!y!
- if !x! gtr 0 (
- set "BinCode=1!BinCode!"
- ) else (
- set "BinCode=0!BinCode!"
- )
- )
- rem echo %%z 的二进制代码 !BinCode!
- rem 二进制转换格雷码
- set /a n1=!BinCode:~0,1!
- rem 根据定义,第一位不变
- set "GrayCode=!n1!"
- rem 后面的每位分别是这位的值与前以为的异或XOR
- for /l %%i in (1,1,%l%) do (
- set /a n2=!BinCode:~%%i,1!
- set /a x=!n2!^^^^!n1!
- set "GrayCode=!GrayCode!!x!"
- set /a n1=!n2!
- )
- rem echo %%z 的格雷码 !GrayCode!
- rem 下面是输出信息控制
- set "Spring=!Spring! !GrayCode!"
- set /a flag=%%z
- set /a flag+=1
- rem 按题中样式%Bother%个一行
- set /a flag=!flag!%%%Bother%
- if !flag! equ 0 (
- echo !Spring!
- set "Spring="
- )
- )
- endlocal
- pause>nul
复制代码
|