2016-07-26 2 views
1

배치 파일을 만들어 사용법이 무엇인지 알려주고 어떤 변수를 입력해야하는지 알려줍니다. 지금은 삼각형의 영역을 찾을 수 있도록 코딩하고 있습니다.기본 배치 수학 명령을 사용하여 소수점 이하의 답을 얻으려면 어떻게해야합니까?

코드를 작성하여 삼각형의 기본, 높이 및 단위를 묻습니다. 알다시피, 수식은 간단히 x x height/2입니다. 따라서 코드가 작동하는지 테스트했습니다. 그러나 홀수를 2로 나눌 때 십진수로 대답하지 않는다는 것을 알게되었습니다. 대신 숫자를 반올림합니다. (. 예 2분의 15 = 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 

그것은 나에게 오류 메시지를 제공하지 않고 모든 것이 원활하게 실행하는 것, 난 그냥 같이 알아낼 수 없습니다 왜 10 진수 답을주지 않을지에 대해서 말입니다.

+1

배치 수학은 32 비트 정수 만 처리 할 수 ​​있습니다. 십진수 나 큰 숫자가 필요한 경우에는 PowerShell을 사용하십시오. – SomethingDark

답변

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

(예 : 타이핑) (AN HTA에서 같은 일을하는 데 수를 .hta로 변경 웹 페이지를 %A

을 사용하여 대화 형으로 배치 사용 %%A에 기억하고 프로그램처럼 행동)

<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 
+0

그래서 처음에 "Execute"Wscript.echo "& wscript.Arguments (0)"를 입력 한 후 바로 "cscript // nologo script.vbs"를 넣으십시오. 2.5 * 2.5 * "" –

+0

이것을 시도하면 그 코드는 인식 할 수없는 명령이라고 말하는 오류 메시지가 표시되고 여전히 소수를 표시하지 않는 동일한 실수를 범했습니다. –

+0

아니요, 첫 번째 줄은 'script.vbs'로 저장하는 별도의 스크립트입니다 – SomethingDark

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를 사용하는 한이 방법이 효과적입니다.

+0

그냥 메모장 ++ 소프트웨어를 업데이트했는데 이제는 작동하지 않습니다. 그것은 어제 문제없이 일했습니다. 이제는 나에게 답을주지 않고, 답을주지 않기 때문에 배치 파일을 종료하려고 할 때 파일이 끝나기까지 10 초가 걸린다. 메모장에 넣고 .bat 파일로 저장해 봤는데 같은 결과가났습니다. –

+0

텍스트 편집기는 PowerShell과 같은 기본 시스템 유틸리티가 설치되어 있는지 여부에 영향을주지 않습니다. 시스템의 다른 부분이 변경되었지만, 여기가 아닌 수퍼 유저에게 질문입니다. – SomethingDark

0

당신이 followinf 샘플 코드와 같이 고정 소수점를 arithmetics의 일종에 의해 제한을 해결 할 수중인 작업의 경우 :

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로 나누는 것은 더 이상 분수가되는 부분을 처리하지 않습니다. 이후에는 소수점 구분 기호가 문자열 조작 조작에 의해 삽입됩니다.
입력 된 숫자가 정수인 경우에만 작동합니다. 물론 이것을 10 진수로 확장 할 수 있습니다. 중간 결과가 부호있는 32 비트 제한을 초과하지 않는다고 간주해야합니다.

관련 문제