Batch-Jscript混合计算器 [英] Batch-Jscript Hybrid Calculator
问题描述
这是我的代码:
@if (@codesection==@batch) @then
@echo off
title
C:
cd %windir%\System32
set c=echo
:a
%c%abs: Absolute Value
%c%atn: Arctangent
%c%cos: Cosine in Degrees
%c%exp: e Raised to the Power of
%c%hex: Hexadecimal Value
%c%int: Integer Part
%c%log: Natural Logarithm
%c%oct: Octal Value
%c%rnd: Random Number from (0,1)
%c%sgn: Sign
%c%sin: Sine in Degrees
%c%sqr: Square Root
%c%tan: Tangent in Degrees
echo.
if defined a goto b
set /p a=
cls
for /f %%G in ('cscript //nologo //e:jscript "%~f0" "%a%"') do set b=%%G
goto a
:b
%c%%a%=%b%
echo.
goto:eof
@end
WScript.Echo("Math."+WScript.Arguments(0));
我不明白我在哪里使用了不正确的语法,如果有人可以帮助我调整此脚本,我将不胜感激.
I don't understand where I am using improper syntax and I'd greatly appreciate if somebody could help me tweak this script.
推荐答案
您的代码有一些问题. JScript中的所有数学函数都必须使用以下格式编写:Math.function(argument)
,例如:Math.sqrt(25)
;您可以在此处 a>.
You have some problems with your code. All mathematic functions in JScript must be written with this format: Math.function(argument)
, for example: Math.sqrt(25)
; you may consult a description of this format here.
在JScript中,WScript.Echo
是函数,因此它使用括号将参数括起来:
In JScript, WScript.Echo
is a function, so it use parentheses to enclose the arguments:
WScript.Echo(eval(WScript.Arguments(0)));
请参见这篇文章.
在分隔@end
之前需要一个goto :EOF
,否则JScript代码将作为批处理代码执行!
You need a goto :EOF
before the delimiting @end
, otherwise the JScript code will be executed as a Batch one!
编辑:回复评论
您尚未指出所需的输出是什么,因此我对其进行了修改.这是我的版本:
You had not indicated what is the desired output, so I modified it. This is my version:
@if (@codesection==@batch) @then
@echo off
set "c=echo "
%c%abs: Absolute Value
%c%atan: Arctangent
%c%cos: Cosine in Radians
%c%exp: e Raised to the Power of
%c%floor: Integer Part
%c%log: Natural Logarithm
%c%random: Random Number from [0,1)
%c%sin: Sine in Radians
%c%sqrt: Square Root
%c%tan: Tangent in Radians
echo.
:a
set "a="
set /p a=
if not defined a goto :EOF
for /f %%G in ('cscript //nologo //e:jscript "%~f0" "%a%"') do set b=%%G
%c%%a%=%b%
goto a
@end
WScript.Echo(eval("Math."+WScript.Arguments(0)));
输出示例:
C:\> test
abs: Absolute Value
atan: Arctangent
cos: Cosine in Radians
exp: e Raised to the Power of
floor: Integer Part
log: Natural Logarithm
random: Random Number from [0,1)
sin: Sine in Radians
sqrt: Square Root
tan: Tangent in Radians
abs(3-5)
abs(3-5)=2
cos(Math.PI/3)
cos(Math.PI/3)=0.5
exp(1)
exp(1)=2.71828182845905
log(10)
log(10)=2.30258509299405
random()
random()=0.137126887153577
random()
random()=0.174421542333208
sin(Math.PI/2)
sin(Math.PI/2)=1
sin(Math.PI/3)
sin(Math.PI/3)=0.866025403784439
sin(Math.PI/4)
sin(Math.PI/4)=0.707106781186547
sqrt(2)
sqrt(2)=1.4142135623731
tan(Math.PI/4)
tan(Math.PI/4)=1
这篇关于Batch-Jscript混合计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!