批处理文件小于和大于?

时间:2017-06-25 21:00:39

标签: batch-file

我正在尝试获取一个批处理文件来测试一个数字是否在两个数字之间,但它只检查第一个数字。我正在使用“和”运算符但它似乎不起作用。这是代码:

fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1,
                 1, 1, 1, 2, 1, 1, 1, 1, 1,2,2,2,2),
               levels = c(1, 2), labels = c("male", "female"))
gender <- factor(rep(c(1:9),3))

mypalette <- colorRampPalette(c("lightblue","darkblue"))
tbl <- spineplot(fail, gender, xlab="Gender", ylab="Income levels: 1 is lowest",
     xaxlabels=c("Male","Female"), col=mypalette(nlevels(gender)) )
print(tbl)

#        Income levels: 1 is lowest
# Gender   1 2 3 4 5 6 7 8 9
# male   2 1 2 1 3 2 2 2 1
# female 1 2 1 2 0 1 1 1 2

print.perc <- function(k, tbl, ndigits=2, str.pct="%") {
   # These lines of codes are the same used by from spineplot
   # for the calculation of the x-position of the stacked bars
   nx <- nrow(tbl)
   off <- 0.02
   xat <- c(0, cumsum(prop.table(margin.table(tbl, 1)) + off))
   posx <- (xat[1L:nx] + xat[2L:(nx + 1L)] - off)/2
   # Proportions by row (gender)       
   ptbl <- prop.table(tbl,1)
   # Define labels as strings with a given format
   lbl <- paste(format(round(100*ptbl[k,], ndigits), nsmall=ndigits), str.pct, sep="")
   # Print labels
   # cumsum(ptbl[k,])-ptbl[k,]/2 is the vector of y-positions
   # for the centers of each stacked bar
   text(posx[k], cumsum(ptbl[k,])-ptbl[k,]/2, lbl)
}

# Print income levels for males and females
strsPct <- c("%","%")
for (k in 1:nrow(tbl)) print.perc(k, tbl, ndigits=2, str.pct=strsPct[k])

3 个答案:

答案 0 :(得分:2)

逻辑and运营商不存在于BAT,

您必须针对您的变量进行第二次if测试:

else (if !total! gtr 4 if !total! leq 8 (....))

编辑:

 else (
     if !total! gtr 4 if !total! leq 8 (echo between 4 and 8)
     if !total! gtr 16 if !total! leq 32 (echo between 16 and 32)
     )

答案 1 :(得分:0)

请参阅...链接IF命令(AND)。 https://ss64.com/nt/if.html

以下是Timo Salmi教授在IF声明中对AND / OR / XOR / NOT运算符的实现 http://www.netikka.net/tsneti/info/tscmd120.htm

另一个好主意 Logical operators ("and", "or") in DOS batch

答案 2 :(得分:0)

:: I used code from here; http://www.robvanderwoude.com/battech_booleanlogic.php
:: Requirements
:: Test for x >8 & <=16, >16 & <=32, etc
:: I only implemented the first two tests
:: A loop could condense the code maybe?
::
:: Reference: IF statement logical operators
:: EQU : Equal
:: NEQ : Not equal

:: LSS : Less than <
:: LEQ : Less than or Equal <=

:: GTR : Greater than >
:: GEQ : Greater than or equal >=

:: Initialize Variables
set Comp1=0
set Comp2=0

:: Set a test case
set X=18
echo x=%x%
echo.

:: Test "8 < x <= 16"
IF %x% GTR 8 (SET Cond1=1) ELSE (SET Cond1=0)
IF %x% LEQ 16 (SET Cond2=1) ELSE (SET Cond2=0)
SET /A "ResultAND = %Cond1% & %Cond2%"
IF %ResultAND% EQU 1 (SET Comp1=1) ELSE (SET Comp1=0)
IF %Comp1% EQU 1 (ECHO %x% is between 9 and 16) ELSE (ECHO %x% is NOT between 9 and 16)

:: Test "8 < x <= 16 OR 16 < x <= 32"
IF %x% GTR 16 (SET Cond1=1) ELSE (SET Cond1=0)
IF %x% LEQ 32 (SET Cond2=1) ELSE (SET Cond2=0)
SET /A "ResultAND = %Cond1% & %Cond2%"
IF %ResultAND% EQU 1 (SET Comp2=1) ELSE (SET Comp2=0)
IF %Comp2% EQU 1 (ECHO %x% is between 17 and 32) ELSE (ECHO %x% is NOT between 17 and 32)

SET /A "ResultOR = %Comp1% | %Comp2%"
IF %ResultOR% EQU 1 (ECHO %x% is between 9 and 16 OR between 17 and 32) ELSE (ECHO %x% is NOT between 9 and 16 OR between 17 and 32)

:: Logical Operator Tests
::SET /A "ResultAND = %Cond1% & %Cond2%"
::IF %ResultAND% EQU 1 (ECHO AND: TRUE) ELSE (ECHO AND: FALSE)

::SET /A "ResultOR = %Cond1% | %Cond2%"
::IF %ResultOR% EQU 1 (ECHO OR:  TRUE) ELSE (ECHO OR:  FALSE)

::SET /A "ResultXOR = %Cond1% ˆ %Cond2%"
::IF %ResultXOR% EQU 1 (ECHO XOR: TRUE) ELSE (ECHO XOR: FALSE)

pause