谢谢Will Sort指点!
我又改进了一下代码:
:: Eval.cmd - 2K/XP表达式计算批处理
:: Brglng - 2005.8.31
:: Thanks to: Will Sort modified on 2005.8.26
:: This batch file can only run in Chinese version of NT-based Windows! It cannot be run in DOS/Win9x/Me!
:: And do not use COMMAND.COM instead of CMD.EXE! Command.com cannot display Chinese, and its SET command is not as powerful as that of CMD.EXE
@echo off
setlocal
if /i "%1"=="/A" if "%2"=="" echo 必须指定文件路径!& goto end
if /i not "%1"=="/A" if /i not "%1"=="/N" if not "%1"=="/?" if not "%1"=="" goto EvalArgument
:EvalLogo
echo *****************************************************************
echo * Eval.cmd - 2K/XP表达式计算批处理 *
echo * Brglng 2005.8.31 http://brglng.ys168.com *
echo * Thanks to: Will Sort modified on 2005.8.26 *
echo *****************************************************************
echo.
if not "%1"=="/?" echo 请输入表达式(输入"?"查看帮助, "Q"退出) & goto inputExpr
:Help
echo 命令行书写帮助:
echo.
echo %0
echo 表达式 : 指定要计算的表达式
echo 文件路径 : 若指定此参数则自动将表达式和计算结果存入指定文件。
echo.
echo %0 : 普通模式下运行时总是将计算结果自动存入指定文件。
echo %0 : 普通模式下运行时只计算而不存入文件。
echo %0 : 查看命令行书写帮助
echo %0 : 查看命令行书写帮助与表达式书写帮助
echo.
echo 如果不使用参数则进入默认的计算模式, 每次都须输入表达式并
echo 询问是否存入文件。
if /i not "%2"=="Expr" goto end
echo.
pause
:ExprHelp
echo 表达式书写帮助:
echo.
echo 本表达式计算批处理能处理简单计算,不支持浮点数以及高级数学函数。
echo 它以递减的优先权顺序支持下列操作:
echo.
echo () - 分组
echo ! ~ - - 一元运算符
echo * / %% - 算数运算符
echo + - - 算数运算符
echo ^<^< ^>^> - 逻辑移位
echo - 按位“与”
echo ^^ - 按位“异”
echo ^| - 按位“或”
echo = *= /= %%= += -= - 赋值
echo ^&= ^^= ^|= ^<^<= ^>^>=
echo , - 表达式分隔符
echo.
echo 如果在命令行下用参数计算,使用任何逻辑或取余操作符需要将表达式字符串用引号扩起来。表达式中的任何非数字字符串键作为环境变量名称,这些环境变量名称的值应在使用前定义,如果未在当前环境中定义,则默认为为零。您可以直接使用环境变量值做计算而不用键入那些 %% 符号来得到它们的值。除十六进制有 0x 前缀,八进制有 0 前缀的,数字值为十进位数字。因此, 0x12 与 18 和 022 相同。请注意八进制公式可能很容易搞混: 08 和 09 是无效的数字,因为 8 和 9 不是有效的八进制位数。
goto end
:EvalArgument
set /a EvalExpr="%1"
if "%EvalExpr%"=="" echo 表达式输入错误! & goto end
echo %1=%EvalExpr%
if "%2"=="" goto end
echo %1=%EvalExpr% >>"%2"
echo 算式和计算结果已存入"%2"!
goto end
:inputExpr
set Expr=
set EvalExpr=
set /p Expr=^>
if "%Expr%"=="" goto inputExpr
if "%Expr%"=="?" call:ExprHelp & goto inputExpr
if /i "%Expr%"=="Q" goto end
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" echo 输入错误! & goto inputExpr
echo 计算结果为:%EvalExpr%
if /i "%1"=="/A" goto autosave
if /i "%1"=="/N" goto inputExpr
:if_save
set SaveEval=
set /p SaveEval=是否将表达式和计算结果存入文件?
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto inputExpr
goto if_save
:save
set /p SaveEvalPath=请输入文件路径及文件名(如C:\Windows\file.txt。若只输入文件名则自动存入当前文件夹,留空则为上一次输入的路径):
echo "%Expr%"=%EvalExpr% >>"%SaveEvalPath%"
echo 表达式以及计算结果已存入"%SaveEvalPath%"!
goto inputExpr
:autosave
echo "%Expr%"=%EvalExpr% >>"%2"
goto inputExpr
:end
Thanks to Will Sort for the guidance!
I have improved the code again:
:: Eval.cmd - 2K/XP Expression Calculation Batch
:: Brglng - 2005.8.31
:: Thanks to: Will Sort modified on 2005.8.26
:: This batch file can only run in the Chinese version of NT-based Windows! It cannot run in DOS/Win9x/Me!
:: And do not use COMMAND.COM instead of CMD.EXE! Command.com cannot display Chinese, and its SET command is not as powerful as CMD.EXE
@echo off
setlocal
if /i "%1"=="/A" if "%2"=="" echo Must specify the file path! & goto end
if /i not "%1"=="/A" if /i not "%1"=="/N" if not "%1"=="/?" if not "%1"=="" goto EvalArgument
:EvalLogo
echo *****************************************************************
echo * Eval.cmd - 2K/XP Expression Calculation Batch *
echo * Brglng 2005.8.31 http://brglng.ys168.com *
echo * Thanks to: Will Sort modified on 2005.8.26 *
echo *****************************************************************
echo.
if not "%1"=="/?" echo Please enter the expression (enter "?" to view help, "Q" to exit) & goto inputExpr
:Help
echo Command line writing help:
echo.
echo %0
echo expression: Specify the expression to calculate
echo file path: If this parameter is specified, the expression and calculation result will be automatically saved to the specified file.
echo.
echo %0 : When running in normal mode, the calculation result is always automatically saved to the specified file.
echo %0 : When running in normal mode, only calculate and do not save to file.
echo %0 : View command line writing help
echo %0 : View command line writing help and expression writing help
echo.
echo If no parameters are used, it will enter the default calculation mode, and you need to enter the expression each time and
echo ask if you want to save to file.
if /i not "%2"=="Expr" goto end
echo.
pause
:ExprHelp
echo Expression writing help:
echo.
echo This expression calculation batch can handle simple calculations and does not support floating-point numbers and advanced mathematical functions.
echo It supports the following operations in descending priority order:
echo.
echo () - Grouping
echo ! ~ - - Unary operator
echo * / %% - Arithmetic operator
echo + - - Arithmetic operator
echo ^<^< ^>^> - Logical shift
echo - Bitwise "AND"
echo ^^ - Bitwise "XOR"
echo ^| - Bitwise "OR"
echo = *= /= %%= += -= - Assignment
echo ^&= ^^= ^|= ^<^<= ^>^>=
echo , - Expression separator
echo.
echo If calculating with parameters on the command line, any logical or remainder operator needs to enclose the expression string in quotes. Any non-numeric string key in the expression is used as an environment variable name. The value of these environment variable names should be defined before use. If not defined in the current environment, it defaults to zero. You can directly use the environment variable value for calculation without typing those %% symbols to get their values. Except that hexadecimal has a 0x prefix and octal has a 0 prefix, the numeric value is a decimal digit. Therefore, 0x12 is the same as 18 and 022. Please note that octal formulas may be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.
goto end
:EvalArgument
set /a EvalExpr="%1"
if "%EvalExpr%"=="" echo Expression input error! & goto end
echo %1=%EvalExpr%
if "%2"=="" goto end
echo %1=%EvalExpr% >>"%2"
echo The formula and calculation result have been saved to "%2"!
goto end
:inputExpr
set Expr=
set EvalExpr=
set /p Expr=>
if "%Expr%"=="" goto inputExpr
if "%Expr%"=="?" call:ExprHelp & goto inputExpr
if /i "%Expr%"=="Q" goto end
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" echo Input error! & goto inputExpr
echo The calculation result is: %EvalExpr%
if /i "%1"=="/A" goto autosave
if /i "%1"=="/N" goto inputExpr
:if_save
set SaveEval=
set /p SaveEval=Do you want to save the expression and calculation result to a file ?
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto inputExpr
goto if_save
:save
set /p SaveEvalPath=Please enter the file path and file name (such as C:\Windows\file.txt. If you only enter the file name, it will be automatically saved to the current folder, leave it blank for the last entered path):
echo "%Expr%"=%EvalExpr% >>"%SaveEvalPath%"
echo The expression and calculation result have been saved to "%SaveEvalPath%"!
goto inputExpr
:autosave
echo "%Expr%"=%EvalExpr% >>"%2"
goto inputExpr
:end