2013-10-21 160 views
0

我正在爲「TechnoExpo」開展一個學校項目。我需要知道如何在兩個「()」之間拉字符串,我需要知道如何計算四個變量。如何在Visual Basic腳本(VBS)中提取部分字符串?

Input One Example: 6(3) 
Input Two Example: 2(7) 

我需要設置「6」到名爲「X1」變量,「3」到名爲「Y1」變量,「2」到名爲「X2」可變的,並且finaly「7」到一個名爲「Y2」的變量。接下來我需要計算(「Y2」 - 「Y1」)除以(「X2」 - 「X2」)。從這裏我可以自己顯示信息。這是一個測試版批處理文件版本。

@Echo Off 
:StartUpConfiguration 
Cls 
Mode Con Cols=50 Lines=25 
Color 0F 

:Start 
Set /P CordinateOne=[One] 
Set /P CordinateTwo=[Two] 
For /F "Tokens=1,2 delims=()" %%A In ("%CordinateOne%") Do Set "X1=%%A" & set "Y1=%%B" 
For /F "Tokens=1,2 delims=()" %%A In ("%CordinateTwo%") Do Set "X2=%%A" & set "Y2=%%B" 
Echo Slope: 
Set /A Y=%Y2%-%Y1% 
Set /A X=%X2%-%X1% 
Set /A M=%Y%/%X% 
Echo [%M%] 
Echo. 
Echo %Y2% - %Y1% [%Y%] 
Echo %X2% - %X1% [%X%] 
Pause 
+2

有什麼問題? – npocmaka

+0

你想要這個在vbs? – npocmaka

+0

我想這在vbs中,而不是vb。問題是我不知道如何用變量做數學,我不知道如何從輸入中拉出不同的字符串。 – user2828807

回答

2
strInput1 = UserInput("CordinateOne=:") 
strInput2 = UserInput("CordinateTwo=:") 


substr1=Split(strInput1,"(") 
substr2=Split(strInput2,"(") 

X1=CInt(substr1(0)) 
Y1=CInt(Split(substr1(1),")")(0)) 

X2=CInt(substr2(0)) 
Y2=CInt(Split(substr2(1),")")(0)) 

X=X2-X1 
Y=Y2-Y1 
M=Y/X 
MI=Y Mod X 

Wscript.Echo "[" & M & "]" & "or [" & M & "." & MI & "]" 
Wscript.Echo "" 
Wscript.Echo Y2 & "-" & Y1 & " [" & Y & "]" 
Wscript.Echo X2 & "-" & X1 & " [" & X & "]" 


Function UserInput(myPrompt) 
' This function prompts the user for some input. 
' When the script runs in CSCRIPT.EXE, StdIn is used, 
' otherwise the VBScript InputBox() function is used. 
' myPrompt is the the text used to prompt the user for input. 
' The function returns the input typed either on StdIn or in InputBox(). 
' Written by Rob van der Woude 
' http://www.robvanderwoude.com 
    ' Check if the script runs in CSCRIPT.EXE 
    If UCase(Right(WScript.FullName, 12)) = "\CSCRIPT.EXE" Then 
     ' If so, use StdIn and StdOut 
     WScript.StdOut.Write myPrompt & " " 
     UserInput = WScript.StdIn.ReadLine 
    Else 
     ' If not, use InputBox() 
     UserInput = InputBox(myPrompt) 
    End If 
End Function 

您可以使用cscript.exewscript.exe執行此操作。不驗證輸入。

1

回答您的標題How to extract part of string in Visual Basic Scripting (VBS)?

這裏是所有的信息,你需要:

MSDN: Mid Function

W3Schools: Mid Function

例子:

txt="This is a beautiful day!" 
wscript.Echo(Mid(txt,1,1)) 
+1

這兩個數字是座標,我仍然不知道如何將每個數字設置爲座標。 – user2828807