China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-23 14:11
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Script for removing the maximum value, removing the minimum value, and finding the average of the remaining values View 3,056 Replies 12
Original Poster Posted 2006-05-24 13:26 ·  中国 山西 运城 电信
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
As the title says:
For example, there is a set of values, can we use batch processing or VBS scripts to remove the maximum and minimum values, and then calculate the average of the remaining values?
房东说:这娃是个好孩子!
Floor 2 Posted 2006-05-25 21:28 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Of course, if using JS, it has a sorting function: sort, which is very simple to solve. If using VBS, there are also various methods. The most ideal one is to operate by creating a disconnected dataset object, which can edit those data like operating a database and can also be saved as files in various formats. However, in the China DOS Union, I'll give you a batch version under the command line:

@echo off
set Count=10 rem The number of numbers to participate in the calculation
call :GetNum %Count% rem Generate random numbers
echo The data to be processed is:%Num%
call :EditNum %Num%
pause
exit

:EditNum
set /a intMax=1,intMin=2147483647 rem Batch processing can only handle 32-bit data at most
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 There are %intCount% numbers, and the maximum and minimum are %intMax%,%intMin% respectively
set total=%Num: =+%
set /a total=%total%
set /a total=(%total% - %intMax% - %intMin%) / (%intCount% - 2)
echo The total is:%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=
Floor 3 Posted 2006-05-25 22:57 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 3742668:

Very good solution.

However, it seems that the initial value of intMax should be -2147483648, or both intMax and intMin can be initialized with %1.

In addition, :EditNum uses the global variable %Num%, although this is a very clever trick, it does not conform to the habit that local modules should not access global variables, and the universality is reduced. Usually, we find the sum of variables in the loop of judging the minimum and maximum values, and can perform some overflow error handling.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2006-05-25 23:14 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
The initial value of intMax was considered, but considering that %random% can only generate numbers between 1 and 32767, so 1 was chosen. As for intMin, 2147483647 was used because the range of 32767 seemed a bit too small, and besides, comparison of positive integers is relatively common.

As for the problem of accessing the %Num% global variable, if using VBS or something else, maybe this situation wouldn't occur. It's just that I'm really too lazy to write an extra for to get the formula from %*, because I think there are no local variables in batch processing, all are global variables.
Floor 5 Posted 2006-05-25 23:24 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 3742668:

Your code should not only be adapted to your test data. We cannot assume that the test data must be positive integers, so the modification of the initial value is still necessary.

2147483647 is a number related to the environment. We should avoid using it when designing the algorithm, otherwise problems may occur in a 64-bit system.

There are local variables in batch processing. At least in CMD, we can use setlocal to limit the scope and lifetime of variables.

If you transplant your :EditNum to other programs or call it directly, it is very likely to have problems.

Summing and counting the number can be realized in the for loop that judges the minimum and maximum values, which is not very complicated.

[ Last edited by willsort on 2006-5-25 at 23:27 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2006-06-02 22:54 ·  中国 山西 太原 电信
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
What I mean is, can it wait for input of numbers after opening, these numbers may be N numbers, then find the largest and the smallest, and finally calculate the average!
房东说:这娃是个好孩子!
Floor 7 Posted 2006-06-03 19:33 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
关于maya0su:

有了3742668兄的代码在前,编写等待输入求平均数的代码应该不是很难,根据你的要求作了相应的修改。


:: Average.cmd - 计算一批数字的平均数
:: 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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2006-07-24 14:09 ·  中国 山西 运城 电信
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
There is another new problem.
I want to use another batch file to call this program to achieve a loop.
@echo off
:loop
cls
call itbat.bat


set /p start=Do you want to continue?(Yes Y No N)
if "%start%"=="y" goto :loop
if "%start%"=="n" goto :end

:end
exit

In the process of implementing the loop, a phenomenon occurs that is
The result of the last calculation, that is, the average value, will appear in the next input number
Also participate in the next operation!
Is there a way to solve this?

[ Last edited by maya0su on 2006-7-24 at 14:11 ]
房东说:这娃是个好孩子!
Floor 9 Posted 2006-07-24 17:25 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The problem is in the line "call :Average %return%". If the value of %return% can be cleared during the loop call, it would be easy.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-07-24 18:30 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Isn't it just adding "set return=" after "call itbat.bat"?
Floor 11 Posted 2006-07-24 22:09 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, the person above, I also once had such an impulse, but have you tried it? Be careful when speaking哦.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-07-25 00:39 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by namejm at 2006-7-24 22:09:
  Hehe, the person above, I also once had such an impulse, but, have you tested it? Please be cautious when speaking.

Modify the one of moderator willsort:
:: Average.cmd - Evaluate the average of a batch of numbers
:: Will Sort - 2006-06-03 - CMD@WinXP
@echo off
set return=
call :GetNum
echo The data to be processed is: %return%
call :Average %return%
echo The average after removing the maximum and minimum values is %return%
goto :eof

:GetNum
if "%_n%"=="" setlocal
set _n=-
set /p _n=Please enter an integer (press Enter directly to end input):
if "%_n%"=="-" endlocal&set return=%return%&goto :eof
set /a _i=_n
if "%_i%" NEQ "%_n%" (echo Invalid input data: %_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

Or the one of maya0su:
@echo off
:loop
cls
set return=
call itbat.bat
set return=


set /p start=Do you want to continue?(Yes Y No N)
if "%start%"=="y" goto :loop
if "%start%"=="n" goto :end

:end
exit

Also OK

[ Last edited by zxcv on 2006-7-25 at 00:41 ]
Floor 13 Posted 2006-07-25 12:50 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
It turns out that two places need to be added, and the code of zxcv is good. It seems that I need to carefully study the format of call labels and parameters.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: