我正在制作一个批处理文件,所以我只告诉它需要使用哪种公式,它告诉我需要输入哪些变量。现在,我正在对它进行编码,以便找到三角形的区域。
我编写了代码,以便它询问三角形的基数,高度和单位。正如你应该知道的那样,公式很简单,基础x高度/ 2.所以我测试了代码,看它是否有效。但我注意到,当它将奇数除以2时,它不会给出小数答案。它改为舍入一个数字。 (例如15/2 = 7)
这是我的代码:
set /p TriangleB=How long is the base of the triangle? (Don't put the unit, just put the number):
set /p TriangleH=What is the height of the triangle? (Don't put the unit, just put the number):
set /p TriangleAreaUnit=What unit is the triangle being measured in?:
set /a TriangleArea=%TriangleB% * %TriangleH% / 2
echo The area of the triangle is %TriangleArea% %TriangleAreaUnit%.
pause >nul
goto :EOF
它没有给我任何错误信息,一切似乎都在顺利进行,我只是无法弄清楚为什么它不会给我一个十进制答案。
答案 0 :(得分:2)
批量数学只能处理32位整数,但有一些方法可以解决这个问题。最简单的方法是使用PowerShell并利用for /f
让您将命令输出存储在变量中。
@echo off
set /p "TriangleB=How long is the base of the triangle? (Don't put the unit, just put the number): "
set /p "TriangleH=What is the height of the triangle? (Don't put the unit, just put the number): "
set /p "TriangleAreaUnit=What unit is the triangle being measured in?: "
for /f %%A in ('powershell %TriangleB% * %TriangleH% / 2') do set TriangleArea=%%A
echo The area of the triangle is %TriangleArea% %TriangleAreaUnit%.
pause >nul
只要您使用的是早于XP的Windows版本,就可以使用。
答案 1 :(得分:2)
您可以使用批处理文件尝试:
@echo off
set /p "TriangleB=How long is the base of the triangle ? (Don't put the unit, just put the number): "
set /p "TriangleH=What is the height of the triangle ? (Don't put the unit, just put the number): "
set /p "TriangleAreaUnit=What unit is the triangle being measured in ?: "
Call :makevbs %TriangleB% %TriangleH%
echo The area of the triangle is %TriangleArea% %TriangleAreaUnit%^2.
pause >nul
Exit /b
:makevbs
echo wscript.echo Eval( wscript.Arguments(0^) * wscript.Arguments(1^) / 2^) > "%tmp%\%~n0.vbs"
for /f %%A in ('cscript /nologo "%tmp%\%~n0.vbs" %~1 %~2') do set TriangleArea=%%A
exit /b
答案 2 :(得分:1)
使用这一行基本脚本
Execute "Wscript.echo " & wscript.Arguments(0)
使用
cscript //nologo script.vbs "2.5*2.5"
如何使用
C:\Users\User>cscript //nologo "C:\Users\User\Desktop\New Text Document.vbs" "2.5*2.5"
6.25
第二行是如何使用刚刚编写的程序作为命令提示符命令。 cscript //nologo "c:\somefolder\script.vbs" "%TriangleB% * %TriangleH% / 2"
。请注意,如果表达式包含空格,则必须用引号括起来。要放入命令提示符,请使用命令。 for /f "delims=" %%A in ('cscript //nologo c:\somefolder\script.vbs "%TriangleB% * %TriangleH% / 2"') Do Set Result=%%A
所以
C:\Users\User>Set TriangleB=5.5
C:\Users\User>Set TriangleH=3.5
C:\Users\User>for /f "delims=" %A in ('cscript //nologo "C:\Users\User\Desktop\New Text Document.vbs" "%TriangleB% * %TriangleH% / 2"') Do Set Result=%A
C:\Users\David Candy>Set Result=9.625
请记住批量使用%%A
并以交互方式(即打字)使用%A
在HTA(重命名为.hta的网页,使其像程序一样)中做同样的事情
<html>
<head>
<script language="VBScript">
Sub Calculate
Answr.innertext = tb1.value * tb2.value /2
End Sub
</script>
</head>
<body>
<p><INPUT Name=tb1 TYPE=Text Value="Height">
<p><INPUT Name=tb2 TYPE=Text Value="Width">
<p><INPUT NAME="Search" TYPE="BUTTON" VALUE="Calc" OnClick=Calculate>
<div ID=Answr></div>
</body>
</html>
仅限vbscript
Wscript.echo InputBox("Height") * Inputbox("width") /2
答案 3 :(得分:0)
对于手头的任务,你可以通过某种定点算术来解决限制问题,如下面的示例代码所示:
set /p TriangleB=How long is the base of the triangle? (Don't put the unit, just put the number):
set /p TriangleH=What is the height of the triangle? (Don't put the unit, just put the number):
set /p TriangleAreaUnit=What unit is the triangle being measured in?:
set /a TriangleArea=%TriangleB% * %TriangleH% * 10 / 2
echo The area of the triangle is %TriangleArea:~,-1%.%TriangleArea:~-1% %TriangleAreaUnit%.
pause >nul
goto :EOF
我在这里所做的只不过是将所有东西都乘以10,所以除以2不再会产生一个可能丢失的小部分;之后,通过字符串操作操作插入小数分隔符 仅当输入数字是整数时才有效。当然你可以将它扩展到更多的十进制数字;但您需要考虑中间结果不超过签名的32位限制。