Then you should ask your teacher/author of the book you are learning from, why
on earth (s)he encourages you to
- hack away without a plan to solve problem(s) (Question C) before thinking about them (Question D)
- use InputBox for getting the program's parameters; because the information has to be entered for each (test) run and disappears immediately, InputBox isn't suitable neither for development nor production. If the parameters for 'real world' scripts must be obtained by a GUI, you should use a .HTA
The abstract structure of you paving problem is to compute the product of
two numbers: the number of squares to pave amd the number of pavers (tiles?)
to cover one square. The second depends on the type (regular or small) of the
pavers.
So you should start with a proof of concept script like this:
Const cnRegPavPerSq = 9
Const cnSmlPavPerSq = 16
Dim aTests : aTests = Array( _
Array( "r", 0, 0 ) _
, Array( "s", 0, 0 ) _
, Array( "x", 0, 0 ) _
, Array( "r", 1, cnRegPavPerSq ) _
, Array( "s", 1, cnSmlPavPerSq ) _
, Array( "s", 10, cnSmlPavPerSq * 10 ) _
)
Dim aTest
For Each aTest In aTests
Dim nFak : nFak = 0 ' used as error indicator
Select Case aTest( 0 )
Case "r"
nFak = cnRegPavPerSq
Case "s"
nFak = cnSmlPavPerSq
Case Else
WScript.Echo "bad paver type", aTest( 0 )
End Select
If 0 <> nFak Then
Dim nRes : nRes = aTest( 1 ) * nFak
WScript.Echo aTest( 1 ), "sq need(s)", nRes, "type", aTest( 0 ), "paver(s)", "(", CStr( nRes = aTest( 2 ) ), ")"
End If
Next
output:
0 sq need(s) 0 type r paver(s) ( True )
0 sq need(s) 0 type s paver(s) ( True )
bad paver type x
1 sq need(s) 9 type r paver(s) ( True )
1 sq need(s) 16 type s paver(s) ( True )
10 sq need(s) 160 type s paver(s) ( True )
that tests different parameter sets and reflect the fact that there are just
two types of tiles. The first enhancement is to allow for a third type of pavers
and to implement a check on the number of squares too:
Const cnRegPavPerSq = 9
Const cnSmlPavPerSq = 16
Const cnMinPavPerSq = 25
Const cnMaxSquares = 1000
Dim aTests : aTests = Array( _
Array( "r", 0, 0 ) _
, Array( "s", 0, 0 ) _
, Array( "x", 0, 0 ) _
, Array( "r", 1, cnRegPavPerSq ) _
, Array( "s", 1, cnSmlPavPerSq ) _
, Array( "s", 10, cnSmlPavPerSq * 10 ) _
, Array( "m", 10, 249 ) _
, Array( "m", 10, 250 ) _
, Array( "m", -1, 0 ) _
, Array( "m", 999, cnMinPavPerSq * 999 ) _
, Array( "s", 1000, cnSmlPavPerSq * 1000 ) _
, Array( "r", 1001, -1 ) _
)
Dim aTest
For Each aTest In aTests
Dim nFak : nFak = 0 ' used as error indicator
Select Case aTest( 0 )
Case "r"
nFak = cnRegPavPerSq
Case "s"
nFak = cnSmlPavPerSq
Case "m"
nFak = cnMinPavPerSq
Case Else
WScript.Echo "bad paver type", aTest( 0 )
End Select
Select Case True
Case Not IsNumeric( aTest( 1 ) )
WScript.Echo "not numeric", aTest( 1 )
nFak = 0 ' used as error indicator
Case 0 > aTest( 1 )
WScript.Echo "bad squares", aTest( 1 )
nFak = 0 ' used as error indicator
Case cnMaxSquares < aTest( 1 )
WScript.Echo "bad squares", aTest( 1 )
nFak = 0 ' used as error indicator
End Select
If 0 <> nFak Then
Dim nRes : nRes = aTest( 1 ) * nFak
WScript.Echo aTest( 1 ), "sq need(s)", nRes, "type", aTest( 0 ), "paver(s)", "(", CStr( nRes = aTest( 2 ) ), ")"
End If
Next
This can be enhanced to a script that can nearly be used in production:
Const cnRegPavPerSq = 9
Const cnSmlPavPerSq = 16
Const cnMinPavPerSq = 25
Const cnMaxSquares = 1000
Dim aTest : aTest = Array( "r", 1, cnRegPavPerSq )
Dim oWAU : Set oWAU = WScript.Arguments.Unnamed
On Error Resume Next
If 1 <= oWAU.Count Then aTest( 0 ) = Left( oWAU( 0 ), 1 )
If 2 <= oWAU.Count Then aTest( 1 ) = CLng( oWAU( 1 ) )
If 0 <> Err.Number Then
WScript.Echo Err.Description
WScript.Quit 1
End If
On Error GoTo 0
Dim nFak : nFak = 0 ' used as error indicator
Select Case aTest( 0 )
Case "r"
nFak = cnRegPavPerSq
Case "s"
nFak = cnSmlPavPerSq
Case "m"
nFak = cnMinPavPerSq
Case Else
WScript.Echo "bad paver type", aTest( 0 )
End Select
Select Case True
Case Not IsNumeric( aTest( 1 ) )
WScript.Echo "not numeric", aTest( 1 )
nFak = 0 ' used as error indicator
Case 0 > aTest( 1 )
WScript.Echo "bad squares", aTest( 1 )
nFak = 0 ' used as error indicator
Case cnMaxSquares < aTest( 1 )
WScript.Echo "bad squares", aTest( 1 )
nFak = 0 ' used as error indicator
End Select
If 0 <> nFak Then
Dim nRes : nRes = aTest( 1 ) * nFak
WScript.Echo aTest( 1 ), "sq need(s)", nRes, "type", aTest( 0 ), "paver(s)"
End If
Test it with
cscript pave.vbs => 1 sq need(s) 9 type r paver(s)
cscript pave.vbs s => 1 sq need(s) 16 type s paver(s)
cscript pave.vbs s 100 => 100 sq need(s) 1600 type s paver(s)
cscript pave.vbs r notanumber => Type mismatch
The polishing needed for selling the script is postponed.
To deal with the additional parameters (Question D), you
systematically expand
the first version of the script:
Const cnRegPavPerSq = 9
Const cnRegPrice = 16
Const cnSmlPavPerSq = 16
Const cnSmlPrice = 9
Const cnReduce = 0.95
Dim aTests : aTests = Array( _
Array( "r", 1, cnRegPavPerSq, cnRegPavPerSq * cnRegPrice, 9 * 16 * cnReduce ) _
, Array( "s", 1, cnSmlPavPerSq, 9 * 16, cnRegPavPerSq * cnRegPrice * 0.95 ) _
)
Dim aTest
For Each aTest In aTests
Dim nFak : nFak = 0 ' used as error indicator
Dim nPrice
Select Case aTest( 0 )
Case "r"
nFak = cnRegPavPerSq
nPrice = cnRegPrice
Case "s"
nFak = cnSmlPavPerSq
nPrice = cnSmlPrice
Case Else
WScript.Echo "bad paver type", aTest( 0 )
End Select
If 0 <> nFak Then
Dim nRes
nRes = aTest( 1 ) * nFak
WScript.Echo aTest( 1 ), "sq need(s)", nRes, "type", aTest( 0 ), "paver(s)", "(", CStr( nRes = aTest( 2 ) ), ")"
nRes = nRes * nPrice
WScript.Echo "Full Costs:", nRes, "(", CStr( nRes = aTest( 3 ) ), ")"
nRes = nRes * cnReduce
WScript.Echo "Reduced Costs:", nRes, "(", CStr( nRes = aTest( 4 ) ), ")"
End If
Next
output:
1 sq need(s) 9 type r paver(s) ( True )
Full Costs: 144 ( True )
Reduced Costs: 136,8 ( True )
1 sq need(s) 16 type s paver(s) ( True )
Full Costs: 144 ( True )
Reduced Costs: 136,8 ( True )
<message edited by ehvbs on Tuesday, August 09, 2011 12:33 AM>