食譜

Examplets.org

VBScript Daily (June 27, 2017)

 

Date 2011/4/10
Name BullsAndCows
Description A number guessing game written in VBScript
Filename BullsAndCowsVBS.vbs
Content
  1. Print the following message:
    Bulls and Cows
    ==============
    This is an ancient game with guessing numbers.
  2. Draw 4-digit random number.
  3. Print the following message:
    A secret number of 4-digit has been draw!
  4. Obtain a 4-digit number or "quit" from user by prompting following message repeatly:
    Please enter a 4-digit number (or type 'quit' to exit):
  5. If the input is "quit" show the following message (replace 0000 with the actual number being drawed
    The secret number is: 0000
  6. Show the number of digit(s) that have correct position as bull and the number of digit(s) that appeared without exact position as cow. For instance: The random number is 1234 and the user input 1355. Exact position: "1", appeared without exact position: "3". The system should display the following message:
    1 bull 1 cow
  7. If no digit found n the random number, the following should be shown instead:
    Neither a bull nor a cow!
  8. On the other hand, if the user guess the 4 digits correctly, the system should show the following message instead:
    4 bulls! You are a genius!
  9. Ask the user to play again if 4 digits have been guessed correctly by showing the following message:
    Do you want to play a new game (Y/N)?
  10. Show the following message if user has chosen to quit the program:
    Thank you for playing Bulls and Cows!
Sample Input:  
Sample Output:  
Expected Test Command: cscript BullsAndCowsVBS.vbs

Randomize
Call WScript.StdOut.WriteLine("Bulls and Cows")
Call WScript.StdOut.WriteLine("==============")
Call WScript.StdOut.WriteLine("This is an ancient game with guessing numbers.")
Dim playNextGame,ans
Dim secretDigit(4)
Dim arr(4)
do
	Dim secret
	secret=""
	For i=0 To 3
		secretDigit(i)=CInt(RND(1)*10)
		secret=secret & secretDigit(i)
	Next 
	Call WScript.StdOut.WriteLine("A secret number of 4-digit has been draw!")
	Dim regex,bull
	Set regex=new RegExp
	regex.pattern="^\d{4}$|^quit$"
	Do
		Do
			Call WScript.StdOut.Write("Please enter a 4-digit number (or type 'quit' to exit):")
			ans=Trim("" & WScript.StdIn.ReadLine())
		Loop while Not regex.Test(ans)
		If ans="quit" Then
			
			Call WScript.StdOut.WriteLine("The secret number is: " & secret)
		Else
			Dim cow
			cow=0
			bull=0
			For i=0 To 3
				arr(i)=CINT(Mid(ans,i+1,1))
				If arr(i)=secretDigit(i) Then bull=bull+1
			Next 
			For i=0 To 3
				For j=0 To 3
					If arr(j)=secretDigit(i) Then
						cow=cow+1
						arr(j)=-1
						j=3
					End If
				Next
			Next
			cow=cow - bull
			Dim msg
			msg=""
			If cow=0 And bull=0 Then
				msg="Neither a bull nor a cow!"
			Else
				If bull=4 Then
					msg="4 bulls! You are a genius!"
				Else
					If cow=0 Then
						If bull=1 Then
							msg="1 bull only."
						Else
							msg=bull & " bulls only."
						End If
					ElseIf bull=0 Then
						If cow=1 Then
							msg="1 cow only."
						Else
							msg=cow & " cows only."
						End If
					Else
						If bull=1 Then
							msg="1 bull and "
						Else
							msg=bull & " bulls and "
						End If
						If cow=1 Then
							msg=msg & "1 cow."
						Else
							msg=msg & cow & " cows."
						End If
					End If
				End If
			End If
			Call WScript.StdOut.WriteLine(msg)
		End If
	Loop while bull<>4 And ans<>"quit"
	If ans<>"quit" Then
		Do
			Call WScript.StdOut.Write("Do you want to play a new game (Y/N)?")
			playNextGame=WScript.StdIn.ReadLine()
		Loop while  playNextGame<>"Y" And playNextGame<>"N" And playNextGame<>"y" And playNextGame<>"n"
	End If
Loop while (playNextGame="Y" Or playNextGame="y") And ans<>"quit"
Call WScript.StdOut.WriteLine("Thank you for playing Bulls and Cows!")


Date 2011/4/9
Name Add Two Numbers
Description Add Two Numbers using VBScript
Filename AddTwoNumVBS.vbs
Content
  1. Using the Standard input stream to enter different numbers or text
  2. Find two integers each time and,
  3. Display output the sum to the standard output stream.
  4. Repeat the all the steps until an EOF (End of File) signal received
Sample Input: 123 3.5 dsfasd 4 fdsafds 6
Sample Output: 126 0
Expected Test Command: cscript AddTwoNumVBS.js < testdata.txt

nCount=0
total=0
Do While Not WScript.StdIn.AtEndOfStream
	temp=WScript.StdIn.ReadLine
	arr=Split(temp," ")
	for i=0 To UBound(arr)
		If Len(arr(i))>0 Then 
			If isNumeric(arr(i)) Then
				If nCount mod 2=0 Then
					Total=CLNG(arr(i))
				Else
					Total=Total+CLNG(arr(i))
					WScript.StdOut.Write Total & " "
				End If
				nCount=nCount+1
			End If
		End If
	Next
Loop


Date 2011/4/9
Name Add Two Numbers
Description Add Two Numbers by Window Scripting File using VBScript
Filename AddTwoNumVBS.wsf
Content
  1. Using the Standard input stream to enter different numbers or text
  2. Find two integers each time and,
  3. Display output the sum to the standard output stream.
  4. Repeat the all the steps until an EOF (End of File) signal received
Sample Input: 123 3.5 dsfasd 4 fdsafds 6
Sample Output: 126 0
Expected Test Command: cscript AddTwoNumVBS.wsf < testdata.txt

<?xml version="1.0"?>
<package>
<job id="AddTwoNumVBS">
<script language="VBScript">
<![CDATA[
nCount=0
total=0
Do While(Not WScript.StdIn.AtEndOfStream)
	tempString=WScript.StdIn.ReadLine
	arr=split(tempString," ")
	for n=LBound(arr) to UBound(arr)
		If isNumeric(arr(n)) Then
			If nCount mod 2=0 Then
				total=CLng(arr(n))
			Else
				total=total+CLng(arr(n))
				WScript.StdOut.write total & " "
			End If
			nCount=nCount+1
		End If
	Next
Loop
]]>
</script>
</job>
</package>


Date 2011/4/8
Name Hello Friend
Description Ask user's name and echo greeting message to user using VBScript
Filename HelloFriendVBS.vbs
Content
  1. Using the Standard output stream to prompt your:"Your name?"
  2. Then use the input stream to receive input from user.
  3. Display the greeting message "Hello, [user name]!" using standard output stream.
  4. Repeat all the steps until users press CTRL-Z (MS windows) or CTRL-D (Mac) or receive the EOF signal.
Sample Input: Peter Pan
Sample Output: Hello, Peter Pan!
Expected Test Command: cscript HelloFriendVBS.vbs < testdata.txt

WScript.StdOut.write "Your name?"
Do While Not WScript.StdIn.AtEndOfStream
	name=WScript.StdIn.ReadLine
	WScript.StdOut.WriteLine "Hello, " & name & "!"
	WScript.StdOut.write "Your name?"
Loop 


Date 2011/4/8
Name Hello Friend
Description Ask user's name and echo greeting message to user by Window Scripting File using VBScript
Filename HelloFriendVBS.wsf
Content
  1. Using the Standard output stream to prompt your:"Your name?"
  2. Then use the input stream to receive input from user.
  3. Display the greeting message "Hello, [user name]!" using standard output stream.
  4. Repeat all the steps until users press CTRL-Z (MS windows) or CTRL-D (Mac) or receive the EOF signal.
Sample Input: Peter Pan
Sample Output: Hello, Peter Pan!
Expected Test Command: cscript HelloFriendVBS.wsf < testdata.txt

<?xml version="1.0"?>
<package>
<job id="HelloFriendVBS">
<script language="VBScript">
<![CDATA[
WScript.StdOut.Write "Your name?"
Do While Not WScript.StdIn.AtEndOfStream
	name=WScript.StdIn.ReadLine
	WScript.StdOut.WriteLine "Hello, " & name & "!"
	WScript.StdOut.Write "Your name?"
Loop
]]>
</script>
</job>
</package>


Date 2011/4/7
Name Hello World
Description Display greeting message to user using VBScript
Filename HelloWorldVBS.vbs
Content
  1. Display the greeting message "Hello, World!" using standard output stream by Windows Scripting File using JScript
Sample Input:  
Sample Output: Hello, World!
Expected Test Command: cscript HelloWorldVBS.vbs

WScript.StdOut.WriteLine "Hello, World!"


Date 2011/4/7
Name Hello World
Description Display greeting message to user using Windows Scripting File by VBScript
Filename HelloWorldVBS.wsf
Content
  1. Display the greeting message "Hello, World!" using standard output stream by Windows Scripting File using JScript
Sample Input:  
Sample Output: Hello, World!
Expected Test Command: cscript HelloWorldVBS.wsf

<?xml version="1.0"?>
<package>
<job id="HelloWorldVBS">
<script language="VBScript">
<![CDATA[
WScript.StdOut.WriteLine "Hello, World!"
]]>
</script>
</job>
</package>

6502 MPU and assembly


Latest Blog Articles from CloudGen wordpress blog!


Copyright 2008-2017

Cloud-Gen