Hi Amida,
my mom said about your script:
(1) While it works in principle, InputBox and MsgBox are the worst possible user interface
(2) The variable names and the prompts are not so good
(3) The code layout is lousy (no indentation, missing spaces around operators)
then she forced me to spend a lazy sunday/holiday afternoon writing:
Option Explicit
' A common form of a linear equation in the two variables x and y is
'
' y = mx + b
'
' where m and b designate constants (the variable y is multiplied by
' the constant 1, which as usual is not explicitly written). The
' origin of the name "linear" comes from the fact that the set of
' solutions of such an equation forms a straight line in the plane. In
' this particular equation, the constant m determines the slope or
' gradient of that line; and the constant term c [sic!; should be b]
' determines the point at which the line crosses the y-axis.
' http://en.wikipedia.org/wiki/Linear_equation
' Default values for all parameters
Dim nXFrom : nXFrom = 0
Dim nXTo : nXTo = 5
Dim nSlope : nSlope = 1
Dim nYCross : nYCross = 0
' Evaluate command line parameters
Dim oWAN : Set oWAN = WScript.Arguments.Named
If oWAN.Exists( "nXFrom" ) Then
checkIntRange oWAN, "nXFrom", nXFrom, 0, 10
nXTo = nXFrom + 10
End If
If oWAN.Exists( "nXTo" ) Then checkIntRange oWAN, "nXTo" , nXTo , nXFrom, nXFrom + 10
If oWAN.Exists( "nSlope" ) Then checkIntRange oWAN, "nSlope" , nSlope , 1, 10
If oWAN.Exists( "nYCross" ) Then checkIntRange oWAN, "nYCross", nYCross, 0, 10
' Display parameters
WScript.Echo "+-------------+"
WScript.Echo "| nXFrom:", padL3( nXFrom ), "|"
WScript.Echo "| nXTo:", padL3( nXTo ), "|"
WScript.Echo "| nSlope:", padL3( nSlope ), "|"
WScript.Echo "|nYCross:", padL3( nYCross ), "|"
WScript.Echo "+------+------+"
WScript.Echo "| X | Y |"
WScript.Echo "+------+------+"
' Display computed values
Dim nX, nY
For nX = nXFrom To nXTo
nY = nSlope * nX + nYCross
WScript.Echo "| ", padL3( nX ), "| ", padL3( nY ), "|"
Next
WScript.Echo "+-------------+"
WScript.Echo
' Display defined Line
Dim aDisplay : aDisplay = createDisplay( nXTo, nSlope, nYCross )
For nX = nXFrom To nXTo
nY = nSlope * nX + nYCross
aDisplay( nX + 1, nY + 1 ) = padL3( "X" )
Next
displayDisplay aDisplay
WScript.Echo
WScript.Quit 0
' Create a canvas (two dimensional array) with legend
Function createDisplay( nXTo, nSlope, nYCross )
Dim nRows : nRows = 3 + nSlope * nXTo + nYCross
Dim nCols : nCols = 3 + nXTo
Dim nRow, nCol
ReDim aTmp( nCols, nRows )
aTmp( 0, 0 ) = padL3( "" )
aTmp( nCols, nRows ) = aTmp( 0, 0 )
For nCol = 1 To nCols
aTmp( nCol, 0 ) = padL3( nCol - 1 )
aTmp( nCol, nRows ) = aTmp( nCol, 0 )
Next
For nRow = 1 To nRows
aTmp( 0, nRow ) = padL3( nRow - 1 )
aTmp( nCols, nRow ) = aTmp( 0, nRow )
Next
nRows = nRows - 1
nCols = nCols - 1
For nRow = 1 To nRows
For nCol = 1 To nCols
aTmp( nCol, nRow ) = padL3( "." )
Next
Next
createDisplay = aTmp
End Function
' Display canvas
Sub displayDisplay( aDisplay )
Dim nRow, nCol
For nRow = 0 To UBound( aDisplay, 2 )
For nCol = 0 To UBound( aDisplay, 1 )
WScript.StdOut.Write aDisplay( nCol, nRow )
Next
WScript.StdOut.WriteLine
Next
End Sub
' Convenience function: padL with hardcoded width
Function padL3( nVal )
padL3 = Right( " " & nVal, 3 )
End Function
' Evaluate and check commandline parameters
Sub checkIntRange( oWAN, sKey, nVar, nFrom, nTo )
Dim sTmp : sTmp = oWAN( sKey )
Dim nTmp, nErr, sErr
On Error Resume Next
nTmp = CInt( sTmp )
nErr = Err.Number
sErr = Err.Description
On Error GoTo 0
If 0 <> nErr Then
sErr = sErr & " (""" & sTmp & """)"
Else
If nTmp >= nFrom And nTmp <= nTo Then
nVar = nTmp
Else
nErr = 1
sErr = "not in range: " & nFrom & " -| " & nTmp & " |- " & nTo
End If
End If
If 0 <> nErr Then
WScript.Echo "*** error:", sErr & "!", sKey, "=", nVar
End If
End Sub
output:
xplore forum28 computeLine /nYCross:-5
=== computeLine: compute linear equation ===============
*** error: not in range: 0 -| -5 |- 10! nYCross = 0
+-------------+
| nXFrom: 0 |
| nXTo: 5 |
| nSlope: 1 |
|nYCross: 0 |
+------+------+
| X | Y |
+------+------+
| 0 | 0 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+-------------+
0 1 2 3 4 5 6 7
0 X . . . . . . 0
1 . X . . . . . 1
2 . . X . . . . 2
3 . . . X . . . 3
4 . . . . X . . 4
5 . . . . . X . 5
6 . . . . . . . 6
7 0 1 2 3 4 5 6 7
=== computeLine: 0 done (00:00:00) =====================
############################################
xplore forum28 computeLine /nYCross:3 /nSlope:2 /nXFrom:3 /nXTo:12
=== computeLine: compute linear equation ============================
+-------------+
| nXFrom: 3 |
| nXTo: 12 |
| nSlope: 2 |
|nYCross: 3 |
+------+------+
| X | Y |
+------+------+
| 3 | 9 |
| 4 | 11 |
| 5 | 13 |
| 6 | 15 |
| 7 | 17 |
| 8 | 19 |
| 9 | 21 |
| 10 | 23 |
| 11 | 25 |
| 12 | 27 |
+-------------+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 . . . . . . . . . . . . . . 0
1 . . . . . . . . . . . . . . 1
2 . . . . . . . . . . . . . . 2
3 . . . . . . . . . . . . . . 3
4 . . . . . . . . . . . . . . 4
5 . . . . . . . . . . . . . . 5
6 . . . . . . . . . . . . . . 6
7 . . . . . . . . . . . . . . 7
8 . . . . . . . . . . . . . . 8
9 . . . X . . . . . . . . . . 9
10 . . . . . . . . . . . . . . 10
11 . . . . X . . . . . . . . . 11
12 . . . . . . . . . . . . . . 12
13 . . . . . X . . . . . . . . 13
14 . . . . . . . . . . . . . . 14
15 . . . . . . X . . . . . . . 15
16 . . . . . . . . . . . . . . 16
17 . . . . . . . X . . . . . . 17
18 . . . . . . . . . . . . . . 18
19 . . . . . . . . X . . . . . 19
20 . . . . . . . . . . . . . . 20
21 . . . . . . . . . X . . . . 21
22 . . . . . . . . . . . . . . 22
23 . . . . . . . . . . X . . . 23
24 . . . . . . . . . . . . . . 24
25 . . . . . . . . . . . X . . 25
26 . . . . . . . . . . . . . . 26
27 . . . . . . . . . . . . X . 27
28 . . . . . . . . . . . . . . 28
29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 29
=== computeLine: 0 done (00:00:00) ==================================
So how about
you checking
my code?
Regards
ehvbs