user3093536
user3093536

Reputation: 95

Batch-Jscript Hybrid Calculator

This is my code:

@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.

Upvotes: 0

Views: 512

Answers (2)

Aacini
Aacini

Reputation: 67216

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.

In JScript, WScript.Echo is a function, so it use parentheses to enclose the arguments:

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

See this post.

You need a goto :EOF before the delimiting @end, otherwise the JScript code will be executed as a Batch one!

EDIT: Reply to the comments

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

Output example:

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

Upvotes: 3

MC ND
MC ND

Reputation: 70933

Without a clear indication of what you are trying to obtain, this can be an aproximation.

@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) );
  • Code has been slightly reorganized.
  • Call to 'Eval' has been removed (i don't know what you want to eval), and in JS, it is eval (JS is case sensitive).
  • Parenthesis has been added to WScript.Echo (in JS all function call include parenthesis).
  • exit /b has been added to the end of batch part to avoid the execution reaches the JS part

Upvotes: 0

Related Questions