我写了一个代码来计算一些表达式。它的工作原理是这样的:calc 5+ 6 + 8*7
必须输出 67。我面临的问题是按位运算符:calc 1 ^& 0
给出错误。我计算的想法很简单。首先将我们所有的输入粘在一起,set a
然后set /A a=%a%
计算表达式我的代码:
@echo off
if "%1" == "" goto :help
if "%1" == "/?" goto :help
set "g="
:start
rem ***Stick all our input together***
set "g=%g%%1"
if not "%1" == "" (
if "%1" == "/?" (
goto :help
)
shift
goto :start
)
echo %g%| findstr /R "[^0123456789\+\-\*\/\(\)] \+\+ \-\- \*\* \/\/ \= \=\= \^^" >nul 2>&1
if not ERRORLEVEL 1 goto error
set /A "g=%g%" 2>nul
if ERRORLEVEL 1 goto error
echo %g%
set g=
goto :EOF
:help
echo This is simple calculator
echo Usage: Mycalc.cmd [/?] (EXPRESSION)
echo Available operands:+,-,*,/,(,)
goto :EOF
:error
echo Wrong input or calculation error.
我认为我们输入时的问题calc 1 ^& 0
是echo %g%
:0 is not recognized as an internal or external command