@echo off
set Count=10 rem 参加计算的数的个数
call :GetNum %Count% rem 产生随机数
echo 要处理的数据为:%Num%
call :EditNum %Num%
pause
exit
:EditNum
set /a intMax=1,intMin=2147483647 rem 批处理最大只能处理32位数据
setlocal enabledelayedexpansion
for %%i in (%*) do (if %%i GEQ !intMax! set /a intMax=%%i) & (if %%i LEQ !intMin! set /a intMin=%%i)
for %%j in (%*) do set /a intCount=!intCount! + 1
echo 有%intCount%个数,其中最大和最小分别为%intMax%,%intMin%
set total=%Num: =+%
set /a total=%total%
set /a total=(%total% - %intMax% - %intMin%) / (%intCount% - 2)
echo 总数为:%total%
goto end
:GetNum
if "%Flag%" == "%1" goto end
set Num=%Num% %Random%
set /a Flag = %Flag% + 1
goto GetNum
:end
endlocal
set Flag=
set intMax=
set intMin=
set intCount=
set total=
:: Average.cmd - Eval average of a batch of numbers
:: Will Sort - 2006-06-03 - CMD@WinXP
@echo off
call :GetNum
echo 要处理的数据为:%return%
call :Average %return%
echo 去除最大值和最小值的平均数为 %return%
goto :eof
:GetNum
if "%_n%"=="" setlocal
set _n=-
set /p _n=请输入一个整数(直接回车结束输入):
if "%_n%"=="-" endlocal&set return=%return%&goto :eof
set /a _i=_n
if "%_i%" NEQ "%_n%" (echo 无效的输入数据:%_n%
) else set return=%return% %_i%
goto GetNum
:Average
setlocal EnableDelayedExpansion
if "%3"=="" set return=N/A&goto :eof
set /a iMax=%1,iMin=%1
for %%i in (%*) do (
if %%i GTR !iMax! set /a iMax=%%i
if %%i LSS !iMin! set /a iMin=%%i
set /a iTotal+=%%i
set /a iCount+=1
)
set /a return=(iTotal-iMax-iMin) / (iCount-2)
endlocal&set return=%return%&goto :eof
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Originally posted by namejm at 2006-7-24 22:09:
呵呵,楼上的,我也曾有过这样的冲动,可是,你试验过了吗?发言要慎重哦。
改一下willsort版主的:
:: Average.cmd - Eval average of a batch of numbers
:: Will Sort - 2006-06-03 - CMD@WinXP
@echo off set return=
call :GetNum
echo 要处理的数据为:%return%
call :Average %return%
echo 去除最大值和最小值的平均数为 %return%
goto :eof
:GetNum
if "%_n%"=="" setlocal
set _n=-
set /p _n=请输入一个整数(直接回车结束输入):
if "%_n%"=="-" endlocal&set return=%return%&goto :eof
set /a _i=_n
if "%_i%" NEQ "%_n%" (echo 无效的输入数据:%_n%
) else set return=%return% %_i%
goto GetNum
:Average
setlocal EnableDelayedExpansion
if "%3"=="" set return=N/A&goto :eof
set /a iMax=%1,iMin=%1
for %%i in (%*) do (
if %%i GTR !iMax! set /a iMax=%%i
if %%i LSS !iMin! set /a iMin=%%i
set /a iTotal+=%%i
set /a iCount+=1
)
set /a return=(iTotal-iMax-iMin) / (iCount-2)
endlocal&set return=%return%&goto :eof set return=
OK
或者maya0su的:
@echo off
:loop
cls set return=
call itbat.bat set return=
set /p start=是否继续?(是Y 否N)
if "%start%"=="y" goto :loop
if "%start%"=="n" goto :end