China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 07:20
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » A batch script that can calculate simple expressions DigestI View 12,184 Replies 22
Floor 16 Posted 2005-08-31 12:57 ·  中国 上海 徐汇区 电信
银牌会员
★★★
永遠的DOS~~~
Credits 1,200
Posts 466
Joined 2005-02-01 00:00
21-year member
UID 35867
Gender Male
From 上海
Status Offline
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
32位才是DOS未来的希望
个人网志:http://sololand.moe
Floor 17 Posted 2005-09-11 13:33 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re Brglng:

The code has been reposted in the following thread, please check and test the stability of the code after simplifying the document.

{8905} A Curious Case of Batch Programming
http://www.cn-dos.net/forum/viewthread.php?tid=8905&page=1#pid54226
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 18 Posted 2005-09-12 11:00 ·  中国 上海 徐汇区 电信
银牌会员
★★★
永遠的DOS~~~
Credits 1,200
Posts 466
Joined 2005-02-01 00:00
21-year member
UID 35867
Gender Male
From 上海
Status Offline
After testing, everything is completely normal, and all parameters can be used normally. There is currently no problem found. Thanks a lot, Will Sort.
32位才是DOS未来的希望
个人网志:http://sololand.moe
Floor 19 Posted 2006-10-24 08:04 ·  中国 广东 广州 广东金万邦科技投资有限公司(新一代数据中心)IDC机房(BGP)
新手上路
Credits 9
Posts 4
Joined 2006-10-22 10:19
19-year member
UID 67689
Gender Male
Status Offline
Not bad, thanks
Floor 20 Posted 2006-10-24 10:25 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
I'll join in the fun, heh

Eval.vbs

WScript.Echo Eval(WScript.Arguments(0))


Run

cscript //NoLogo Eval.vbs 3.1415926*2E5
returns
628318.52


Success, yeah!

Or without input parameters, interactive input:


WScript.Echo Eval(WScript.StdIn.ReadLine)


[ Last edited by electronixtar on 2006-10-24 at 10:31 AM ]

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 21 Posted 2006-10-24 11:08 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Hehe, thanks to brother electronixtar, I've learned another trick. Keep going~Support~
Floor 22 Posted 2006-10-24 13:04 ·  中国 广东 潮州 电信
初级用户
Credits 23
Posts 10
Joined 2006-10-23 13:03
19-year member
UID 67889
Gender Male
Status Offline
Learning
Floor 23 Posted 2006-10-29 11:27 ·  中国 江苏 南京 电信
新手上路
Credits 6
Posts 3
Joined 2006-10-25 21:31
19-year member
UID 68245
Gender Male
Status Offline
Good stuff, thanks
Forum Jump: