addoil,seo hk

Examplets.org

PHP Daily (November 24, 2017)

 

Date 2011/4/10
Name BullsAndCows
Description A number guessing game written in PHP
Filename BullsAndCowsPHP.php
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: php BullsAndCowsPHP.php

<?php
echo("Bulls and Cows\n");
echo("==============\n");
echo("This is an ancient game with guessing numbers.\n");
$secretDigit=array();
$arr=array();
do{
	$secret="";
	for($i=0;$i<4;$i=$i+1){
		$secretDigit[$i]=rand(0,9);
		$secret=$secret.$secretDigit[$i];
	}
	echo("A secret number of 4-digit has been draw!\n");
	do{
		do{
			echo("Please enter a 4-digit number (or type 'quit' to exit):");
			$ans=trim(fgets(STDIN));
		}while(!preg_match("/^\d{4}$|^quit$/",$ans));
		if($ans=="quit"){
			echo("The secret number is: $secret\n");
		}else{
			$cow=0;
			$bull=0;
			
			for($i=0;$i<4;$i=$i+1){
				$arr[$i]=substr($ans,$i,1);
				if($arr[$i]==$secretDigit[$i])
					$bull=$bull+1;
			}
			for($i=0;$i<4;$i=$i+1)
				for($j=0;$j<4;$j=$j+1)
					if($arr[$j]==$secretDigit[$i]){
						$cow=$cow+1;
						$arr[$j]=-1;
						break;
					}
			$cow-=$bull;
			$msg="";
			if($cow==0 && $bull==0){
				$msg="Neither a bull nor a cow!";
			}else {
				if($bull==4){
					$msg="4 bulls! You are a genius!";
				}else{
					if($cow==0){
						if($bull==1)
							$msg="1 bull only.";
						else
							$msg="$bull bulls only.";
					}else if($bull==0){
						if($cow==1)
							$msg="1 cow only.";
						else
							$msg="$cow cows only.";
					}else {
						if($bull==1)
							$msg="1 bull and ";
						else
							$msg="$bull bulls and ";
						if($cow==1)
							$msg="$msg 1 cow.";
						else
							$msg="$msg$cow cows.";
					}
				}
			}
			echo("$msg\n");
		}
	}while($bull!=4 && $ans!="quit");
	if($ans!="quit")
		do{
			echo("Do you want to play a new game (Y/N)?");
			$playNextGame=trim(fgets(STDIN));
		}while(strtoupper($playNextGame)!="Y" && strtoupper($playNextGame)!="N");
}while(strtoupper($playNextGame)=="Y" && $ans!="quit");
echo("Thank you for playing Bulls and Cows!\n");
?>


Date 2011/4/9
Name Add Two Numbers
Description Add Two Numbers using PHP
Filename AddTwoNumPHP.php
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: PHP AddTwoNumPHP.php < testdata.txt

<?php
$total=0;
$nCount=0;
while(!feof(STDIN)){
	$temp=trim(fgets(STDIN));
	if(!feof(STDIN)){
		$arr=preg_split("/\\s+/",$temp);
		foreach($arr as $val){
			if(is_numeric($val)){		
				if($nCount % 2==0) $total=floor($val);
				else {
					$total+=floor($val);
					echo("$total ");
				}
				$nCount+=1;
			}
		}
	}
}
?>


Date 2011/4/8
Name Hello Friend
Description Ask user's name and echo greeting message to user using PHP
Filename HelloFriendPHP.php
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: php HelloFriendPHP.php < testdata.txt

<?php
while(!feof(STDIN)){
	print("Your name?");
	$name=trim(fgets(STDIN));
	if(!feof(STDIN)) echo("Hello, $name!\n");
}
?>


Date 2011/4/7
Name Hello World
Description Display greeting message to user using PHP
Filename HelloWorldPHP.php
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: php HelloWorldPHP.php

<?php echo("Hello, World!\n");?>

6502 MPU and assembly


Latest Blog Articles from CloudGen wordpress blog!


Copyright 2008-2017

SEO HERO