2013-10-21 114 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