| Date | 2011/4/10 |
|---|---|
| Name | BullsAndCows |
| Description | A number guessing game written in C-Sharp |
| Filename | BullsAndCowsCS.cs |
| Content |
|
| Sample Input: | |
| Sample Output: | |
| Expected Test Command: | csc BullsAndCowsCS.cs BullsAndCowsCS.exe |
using System;
using System.Text.RegularExpressions;
class BullAndCowsCS{
public static void Main(){
int i,j,bull=0,cow=0;
int[] secretDigit,arr;
string msg, ans, playNextGame;
secretDigit=new int[4];
arr=new int[4];
Console.WriteLine("Bulls and Cows");
Console.WriteLine("==============");
Console.WriteLine("This is an ancient game with guessing numbers.");
Random rand=new Random();
Regex regex=new Regex("^\\d{4}$|^quit$");
do{
string secret="";
playNextGame="";
for(i=0;i<4;i++){
secretDigit[i]=rand.Next(0,9);
secret+=secretDigit[i].ToString();
}
Console.WriteLine("A secret number of 4-digit has been draw!");
do{
do{
Console.Write("Please enter a 4-digit number (or type 'quit' to exit):");
ans=Console.ReadLine().Trim();
}while(!regex.IsMatch(ans));
if(ans=="quit"){
Console.WriteLine("The secret number is: " + secret);
}else{
cow=0;
bull=0;
for(i=0;i<4;i++)
if(Int32.TryParse(ans.Substring(i,1),out arr[i]))
if(arr[i]==secretDigit[i]) bull++;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(arr[j]==secretDigit[i]){
cow++;
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+="1 cow.";
else
msg+=cow + " cows.";
}
}
}
Console.WriteLine(msg);
}
}while(bull!=4 && ans!="quit");
if(ans!="quit"){
do{
Console.Write("Do you want to play a new game (Y/N)?");
playNextGame=Console.ReadLine();
}while(playNextGame!="Y" && playNextGame!="N" && playNextGame!="y" && playNextGame!="n");
}
} while ((playNextGame=="Y" || playNextGame=="y") && ans!="quit");
Console.WriteLine("Thank you for playing Bulls and Cows!");
}
}| Date | 2011/4/9 |
|---|---|
| Name | Add Two Numbers |
| Description | Add Two Numbers using C#-Sharp |
| Filename | AddTwoNumCS.cs |
| Content |
|
| Sample Input: | 123 3.5 dsfasd 4 fdsafds 6 |
| Sample Output: | 126 0 |
| Expected Test Command: | csc AddTwoNumCS.cs AddTwoNumCS.exe < testdata.txt |
class AddTwoNumCS{
public static void Main(){
string temp;
int nCount=0,nTotal=0;
do{
temp=System.Console.ReadLine();
if(temp!=null){
string[] arr=System.Text.RegularExpressions.Regex.Split(temp,@"\s+");
foreach(string value in arr){
int number;
if(System.Int32.TryParse(value,out number)){
if(nCount%2==0){
nTotal=number;
}else{
nTotal+=number;
System.Console.Write(nTotal+" ");
}
nCount++;
}
}
}
}while(temp!=null);
}
}
| Date | 2011/4/8 |
|---|---|
| Name | Hello Friend |
| Description | Ask user's name and echo greeting message to user using C#-Sharp |
| Filename | HelloFriendCS.cs |
| Content |
|
| Sample Input: | Peter Pan |
| Sample Output: | Hello, Peter Pan! |
| Expected Test Command: | csc HelloFriendCS.cs HelloFriendCS.exe < testdata.txt |
class HelloFriendCS{
public static void Main(){
string temp;
do {
System.Console.Write("Your name?");
temp=System.Console.ReadLine();
if(temp!=null) System.Console.WriteLine("Hello, "+temp);
}while(temp!=null);
}
}
| Date | 2011/4/7 |
|---|---|
| Name | Hello World |
| Description | Display greeting message to user using C#-Sharp |
| Filename | HelloWorldCS.cs |
| Content |
|
| Sample Input: | |
| Sample Output: | Hello, World! |
| Expected Test Command: | csc HelloWorldCS.cs HelloWorldJava.exe |
class HelloWorldCS{
public static void Main(){
System.Console.WriteLine("Hello, World!");
}
}