1

I have been trying to make a program that detects if the string that is entered contains +, -, *, or / and then evaluates it. The problem is that var is always defined whether the string contains the characters or not.

Examples:

Input: not a math command

Output: var is defined (math input

What I wanted: var is not defined

Input:10+10

Output: var is defined (math input

What I wanted: var is defined (math input

Input: ABC123

Output: var is defined (math input

What I wanted: var is not defined

I have adapted my code from this link:

how to check if a parameter (or variable) is numeric in windows batch file

Code:

@echo off

SET "var="

set /p "COMMAND=Enter command>"

for /f "delims=^+-^*/" %%i in ("%COMMAND%") do set "var=%%i"


if not defined var (
    echo var not defined
) else (
    echo var is defined (math input)
)

pause
Community
  • 1
  • 1
nt314p
  • 13
  • 5
  • 1
    Perhaps you should tell us what you are entering and what you are expecting from that entry. – Magoo Jul 20 '17 at 22:00

2 Answers2

0

Ah - the code you are using is designed to detect whether the string in question contains only numerics or not.

Your requirement appears to be to detect whether the string contains at least one of the delimiters.

for /f "tokens=1*delims=^+-^*/" %%i in ("%COMMAND%") do set "svar=%%i"&set "var=%%j"

will set svar to the part before and var to the part after a delimiter but it will ignore any leading delimiter characters and will not object to successive delimiters such as abc++//++def as the partitioning into tokens observes the subject string as a sequence delimstoken1delimstoken2...

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks! I understand what you were doing with the svar but what does the tokens=1* part mean? – nt314p Jul 22 '17 at 01:02
  • The string is `command` is interpreted as a sequence of tokens and delimiter-sequences. "tokens=1*" means select the 1st token to `%%i`, and that remaining after the delimiter found to `%%j` (next `metavariable` in alphabetical sequence). The `tokens` selected may be separated by `,` or a range separated by `-` so "tokens=1,2,4-7,9*" selects the 1st token to `%%i`, 2nd to `%%j`, 4th .. 7th to `%%k..%%n`, 9th to `%%o` and `%%p` gets the remainder of the string from the beginning of the 10th token (the one after the highest explicitly chosen) - see `for /?|more` from the prompt for docco – Magoo Jul 22 '17 at 02:19
0

To ensure there are only digits and arithmetic operators in %command% there are several ways,
I'd use findstr.exe limited regular expression features.

As spaces in %command% will disturb the result I remove them
(the minus has to be escaped with a backslash because it denotes a range in a [class]):

:: Q:\Test\2017\07\21\SO_45225802.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Cls
:loop
SET "var="
set /p "ArithmeticExpression=Enter arithmetic expression> "

:: Check if input consists of only numbers and operators
Echo:%ArithmeticExpression: =%|Findstr "^[0-9+\-*/][0-9+\-*/]*$" >NUL 2>&1
If Errorlevel 1 (
    Echo Invalid characters in %ArithmeticExpression%
) Else (
    Set /a "var=%ArithmeticExpression%"
    If defined var (
      Echo %ArithmeticExpression% evaluates to !var!
    ) Else (
      Echo Error evaluating %ArithmeticExpression%
    ) 
)
Goto :loop

Sample run:

Enter arithmetic expression> 1 + 1
1 + 1 evaluates to 2
Enter arithmetic expression> 10 * 3
10 * 3 evaluates to 30
Enter arithmetic expression> 30 -3
30 -3 evaluates to 27
Enter arithmetic expression> 27 / 3
27 / 3 evaluates to 9
Enter arithmetic expression> ABC123
Invalid characters in ABC123
Enter arithmetic expression> 10/0
Divide by zero error.
Error evaluating 10/0
Enter arithmetic expression>