帖一段容易看得明白一点的代码,换而言之,也就是比较臃肿的代码:
@echo off
setlocal enabledelayedexpansion
:begin
cls
set count=0
set item=0
set times=0
set expression=
set input1=
set input2=
set /p input1= 请指定参与运算的数字的个数(退出请直接按回车):
set /p input2= 请输入算术表达式可能出现的最大条数(退出请直接按回车):
if not defined input1 exit
if not defined input2 exit
cls
:loop
if %times% equ %input2% goto end
set expression=
set count=0
set str1=+-×
for /l %%i in (1,1,%input1%) do call :build
call :insert_sum
if %sum% lss 0 goto loop
set /a total+=%sum%&set /a item+=1
echo 第 %item% 条(%times%/%input2%): %expression:~1%=%sum%
goto loop
:end
set /a aver=%total%/%item%
set /a mod=%total%%%%item%
echo.
echo %item% 条记录的算术平均值是:%total%/%item%=%aver%……%mod%
echo.
echo 按任意键再来一次
pause>nul
goto begin
:build
:: 构造没有括号对的表达式
set /a num=%random%%%10
set /a SN=%random%%%3
set expression=!str1:~%SN%,1!%num%%expression%
goto :eof
:insert_sum
:: 随机插入括号对并计算每个表达式的结果
set /a length=2*%input1%
set /a num1=%random%%%%input1%
if %num1% equ 0 set num1=1
for /l %%i in (1,2,%length%) do (
set /a count+=1
if !count! equ %num1% set num1=%%i&goto next
)
:next
set count=0
set /a bg=%num1%+3
set /a remain=(%length%-%bg%)/2+1
if %remain% neq 1 (
set /a num2=%random%%%%remain%+1
) else set num2=1
for /l %%i in (%bg%,2,%length%) do (
set /a count+=1
if !count! equ %num2% set num2=%%i&goto add
)
:add
set /a dif=%num2%-%num1%
set expression=!expression:~0,%num1%!(!expression:~%num1%,%dif%!)!expression:~%num2%!
set sum=%expression:+=+%
set sum=%sum:-=-%
set sum=%sum:×=*%
set /a sum=%sum:~1%
set /a times+=1
goto :eof
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。