中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 14:57
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » Script for removing the maximum value, removing the minimum value, and finding the average of the remaining values
Printable Version  3,125 / 12
Floor1 maya0su Posted 2006-05-24 13:26
中级用户 Posts 131 Credits 241
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?
Floor2 3742668 Posted 2006-05-25 21:28
荣誉版主 Posts 718 Credits 2,013
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:
Floor3 willsort Posted 2006-05-25 22:57
元老会员 Posts 1,512 Credits 4,432
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.
Floor4 3742668 Posted 2006-05-25 23:14
荣誉版主 Posts 718 Credits 2,013
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.
Floor5 willsort Posted 2006-05-25 23:24
元老会员 Posts 1,512 Credits 4,432
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 ]
Floor6 maya0su Posted 2006-06-02 22:54
中级用户 Posts 131 Credits 241
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!
Floor7 willsort Posted 2006-06-03 19:33
元老会员 Posts 1,512 Credits 4,432
关于maya0su:

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

Floor8 maya0su Posted 2006-07-24 14:09
中级用户 Posts 131 Credits 241
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 ]
Floor9 namejm Posted 2006-07-24 17:25
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor10 zh159 Posted 2006-07-24 18:30
金牌会员 Posts 1,467 Credits 3,687
Isn't it just adding "set return=" after "call itbat.bat"?
Floor11 namejm Posted 2006-07-24 22:09
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Hehe, the person above, I also once had such an impulse, but have you tried it? Be careful when speaking哦.
Floor12 zh159 Posted 2006-07-25 00:39
金牌会员 Posts 1,467 Credits 3,687
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 ]
Floor13 namejm Posted 2006-07-25 12:50
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023