【问题标题】:Batch-Jscript Hybrid CalculatorBatch-Jscript 混合计算器
【发布时间】:2014-01-27 21:52:42
【问题描述】:

这是我的代码:

@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));

我不明白我在哪里使用了不正确的语法,如果有人可以帮助我调整这个脚本,我将不胜感激。

【问题讨论】:

    标签: batch-file jscript


    【解决方案1】:

    您的代码存在一些问题。 JScript 中的所有数学函数都必须用这种格式编写:Math.function(argument),例如:Math.sqrt(25);您可以查阅此格式的说明here

    在 JScript 中,WScript.Echo 是一个函数,所以它使用括号将参数括起来:

    WScript.Echo(eval(WScript.Arguments(0)));
    

    this post

    你需要一个goto :EOF在分隔@end之前,否则JScript代码将作为一个批处理执行!

    编辑回复cmets

    你没有指出想要的输出是什么,所以我修改了它。这是我的版本:

    @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
    

    【讨论】:

    • +1 - 几乎是我在写作过程中的逐字逐句:-) 除了我更喜欢exit /b 而不是goto :EOF。不需要我的回答:-)
    • @dbenham:谢谢,戴夫。我曾经在子程序中使用exit /B,在主文件中使用goto :EOF...
    • 所以我应该改用 WScript.Echo(Math.Arguments(0)); ?
    • @user3093536:正确,大小写正确:WScript.Echo(eval("Math."+WScript.Arguments(0)));
    • 由于某种原因,当我将它放在批处理代码的末尾时,它仍然无法正常工作。
    【解决方案2】:

    如果没有明确表明您要获得什么,这可能是一个近似值。

    @if (@codesection==@batch) @then
    @echo off
    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.
    set "a="
    set /p "a=?"
    if not defined a goto b
    cls
    for /f %%G in ('cscript //nologo //e:jscript "%~f0" "%a%"') do set "b=%%G"
    %c%%a%=%b%
    echo.
    goto a
    
    :b
    pause
    exit /b
    @end
    WScript.Echo( WScript.Arguments(0) );
    
    • 对代码进行了轻微的重组。
    • 对“评估”的调用已被删除(我不知道您要评估什么),在 JS 中,它是 eval(JS 区分大小写)。
    • WScript.Echo 中添加了括号(在 JS 中所有函数调用都包含括号)。
    • exit /b 已添加到批处理部分的末尾,以避免执行到达 JS 部分

    【讨论】:

    • eval 不评估数学输入吗?
    • @user3093536,在 javascript 中,eval 评估 javascript 代码。它包括数学运算,但是要对某些数据使用函数,您需要数据,并且在您的代码中,您不会检索/生成/将任何数据传递给 javascript 部分。所以,为了避免错误并留下一个工作骨架,我删除了 eval 部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2017-03-07
    • 1970-01-01
    相关资源
    最近更新 更多