在批处理文件排序输入数字 [英] Sorting input numbers in batch file

查看:179
本文介绍了在批处理文件排序输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建一个程序中,你将进入 5号随机会自动的显示分类从输入的数字最低到最高 。这里是我的code。

 关闭@echo:主要    集/ p NUM1 =输入1
    集/ p NUM2 =输入第二个:
    集/ p NUM3 =输入3
    集/ p num4 =输入4
    集/ p num5 =输入5
    设置/高= 0
    设置/低= 0
    集/ A平均=(NUM1 + NUM2 + NUM3 + num4 + num5)/ 5    如果NUM1%GTR%NUM2%%(套/高=%NUM1%),否则设置/高=%NUM2%
    如果%NUM3%GTR%的高%(套/高=%NUM3%)
    如果%num4%GTR%的高%(套/高=%num4%)
    如果%num5%GTR%的高%(套/高=%num5%)    如果NUM1%LSS%NUM2%%(套/低=%NUM1%),否则设置/低=%NUM2%
    如果%NUM3%LSS%低%(套/低=%NUM3%)
    如果%num4%LSS%低%(套/低=%num4%)
    如果%num5%LSS%低%(套/低=%num5%)
    回声。
    ECHO平均:AVE%%
    ECHO最高:%高%
    ECHO最低:%低%
    ECHO输入数字:%NUM1%%NUM2%%NUM3%%num4%%num5%:结束集/ p回报=是否继续?    如果%的回报率%==是GOTO主
    GOTO退出:出口


解决方案

有几种方法可以解决这个问题。在一一的方法每个变量必须与另一个进行比较;这种方法是过于复杂并且是你用于读取的值之一。您也可以使用排序命令的数量进行排序(如拉斐尔在他的回答试过);然而,数字必须在左侧用零以正确排序填充。

当重复在多个元件的某些方法中,通常的方法来解决上述问题是通过一种的阵列的。您可能会发现有关数组概念描述的几个网站,如维基百科 。你可以看一下如何在<一个使用数组在批处理文件href=\"http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990\">this帖子

下面的批处理文件利用好这样的事实环境变量保存自动排序;这样,该程序只插入中的数字数组,然后取出它的元素,所以排序处理不需要本身

 关闭@echo
SETLOCAL EnableDelayedExpansion设定N = 5:主要    REM删除从previous循环,数组元素如果有的话:
    FOR / Fdelims ==%%一中(设置阵列[2 ^&GT; NUL')也将一%% =    REM阅读数;在同一时间,他们积累在AVE变量
    REM和创建具有适当的指数阵列(左零)
    集平均= 0
    对/ L %%我在(1,1,%N%)做(
        集/ p NUM =输入号码%%我:
        集/ A AVE + = NUM
        集索引= 000000000!NUM!
        一套数组[指数:!〜-10]!= NUM​​!
    )    集/ A平均= AVE / N    REM组装输出线,并获得低和高值
    集产量
    设置低=
    FOR / F令牌= 2 delims ==%%一中(设置阵列[')做(
        如果没有定义设置低低= %%一
        设置高= %%一
        组输出=!输出! %%一个
    )    回声/
    ECHO平均:AVE%%
    ECHO最高:%高%
    ECHO最低:%低%
    ECHO输入数字:%输出%    集/ p回报=是否继续?如果%的回报率%==是GOTO主

Im trying to create a program in which you will enter 5 numbers randomly and will automatically shows the sorted entered numbers from lowest to highest. Here is my code.

@echo off

:main

    set /p num1="Enter 1st :"
    set /p num2="Enter 2nd :"
    set /p num3="Enter 3rd :"
    set /p num4="Enter 4th :"
    set /p num5="Enter 5th :"
    set /a high=0
    set /a low=0


    set /a ave=(num1+num2+num3+num4+num5)/5

    if %num1% GTR %num2% (set /a high=%num1%) ELSE set /a high =%num2%
    if %num3% GTR %high% (set /a high=%num3%) 
    if %num4% GTR %high% (set /a high=%num4%) 
    if %num5% GTR %high% (set /a high=%num5%) 

    if %num1% LSS %num2% (set /a low=%num1%) ELSE set /a low =%num2%
    if %num3% LSS %low% (set /a low=%num3%) 
    if %num4% LSS %low% (set /a low=%num4%) 
    if %num5% LSS %low% (set /a low=%num5%) 


    ECHO.
    ECHO Average: %ave%
    ECHO Highest: %high%
    ECHO Lowest: %low%
    ECHO Input Numbers: %num1%    %num2%    %num3%    %num4%    %num5%

:end

set /p return="Continue?"

    if "%return%"=="yes" GOTO main 
    GOTO exit   

:exit   

解决方案

There are several ways to solve this problem. In the "one by one" method each variable must be compared vs. another one; this method is too much complex and is the one you used to read the values. You may also use sort command to sort the numbers (as Rafael tried in his answer); however, the numbers must be padded at left side with zeros in order to be correctly sorted.

When a certain process is repeated over several elements, the usual way to solve such a problem is via an array. You may find descriptions about array concept in several sites, like in Wikipedia. You may read how to use arrays in a Batch file at this post.

The Batch file below make good use of the fact that environment variables are kept automatically sorted; this way, the program just insert the numbers in an array and then take out its elements, so the sort process itself is not required:

@echo off
setlocal EnableDelayedExpansion

set N=5

:main

    rem Delete array elements from previous cycle, if any:
    for /F "delims==" %%a in ('set array[ 2^>NUL') do set "%%a="

    rem Read the numbers; at same time, accumulate they in "ave" variable
    rem and create the array with the proper index (with left zeros)
    set ave=0
    for /L %%i in (1,1,%N%) do (
        set /P num="Enter number %%i :"
        set /A ave+=num
        set index=000000000!num!
        set array[!index:~-10!]=!num!
    )

    set /A ave=ave/N

    rem Assemble the output line and get low and high values
    set "output="
    set "low="
    for /F "tokens=2 delims==" %%a in ('set array[') do (
        if not defined low set low=%%a
        set high=%%a
        set output=!output!   %%a
    )

    ECHO/
    ECHO Average: %ave%
    ECHO Highest: %high%
    ECHO Lowest: %low%
    ECHO Input Numbers: %output%

    set /p return="Continue?"

if "%return%"=="yes" GOTO main 

这篇关于在批处理文件排序输入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆