呵呵,又来百分大餐?

授人以渔,不授人以鱼。
程序代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Date20111002
{
class Dog
{
public bool dogWin = false;
public double locationX;
public double locationY;
public double x1,y1;
public double speed;
public double sina;
public Dog(double x, double y, int theSpeed) {
locationX = x;
locationY = y;
speed = theSpeed;
}
}
}
Rabbit.cs
程序代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Date20111002
{
class Rabbit
{
public bool rabbitWin = false;
private double X, Y;
public double speed;
public double x1, y1;
public readonly double sina;
private double rabbitDistance;
public double locationX {
get {
return X;
}
set {
if (value < 0)
{
X = 0;
Y = 0;
}
else {
X = value;
}
}
}
public double locationY {
get {
return Y;
}
set {
if (value < 0)
{
Y = 0;
X = 0;
}
else {
Y = value;
}
}
}
public double RabbitDistance {
get {
return rabbitDistance;
}
set {
if (rabbitDistance <= 0)
{
rabbitDistance = 0;
}
else {
rabbitDistance = value;
}
}
}
public Rabbit(double x, double y, int theSpeed) {
X = x;
Y = y;
speed = theSpeed;
rabbitDistance = Math.Sqrt(x*x+y*y);
sina = y / Math.Sqrt((x * x + y * y));
}
}
}
Program.cs
程序代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Date20111002
{
class Program
{
public void Star(Rabbit rabbit, Dog dog)
{
double count = 0;
double dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY));
Console.WriteLine("兔子离自己的窝距离为: {0}", rabbit.RabbitDistance);
Console.WriteLine("猎犬离兔子的距离为:{0}\n", dogDistance);
while (dog.dogWin == false && rabbit.rabbitWin == false) {
rabbit.RabbitDistance = rabbit.RabbitDistance - rabbit.speed;
rabbit.y1 = rabbit.speed * rabbit.sina;
rabbit.x1 = Math.Sqrt((rabbit.speed * rabbit.speed) - (rabbit.y1 * rabbit.y1));
rabbit.locationY = checked(rabbit.locationY - rabbit.y1);
rabbit.locationX = checked(rabbit.locationX - rabbit.x1);
Console.WriteLine("兔子已跑至坐标: ({0},{1})", rabbit.locationX, rabbit.locationY);
Console.WriteLine("兔子离自己的窝距离为: {0}", rabbit.RabbitDistance);
Debug.WriteLine("rabbit:({0},{1})", rabbit.locationX, rabbit.locationY);
Debug.WriteLine("dog:({0},{1})", dog.locationX, dog.locationY);
dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY));
dog.sina = Math.Abs(dog.locationY - rabbit.locationY) / dogDistance;
Debug.WriteLine("dog.sina:{0}", dog.sina);
if (rabbit.locationX <= dog.locationX && rabbit.locationY >= dog.locationY)
{
dog.y1 = dog.speed * dog.sina;
dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
dog.locationX -= dog.x1;
dog.locationY += dog.y1;
}
else if (rabbit.locationX >= dog.locationX && rabbit.locationY >= dog.locationY)
{
dog.y1 = dog.speed * dog.sina;
dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
dog.locationX += dog.x1;
dog.locationY += dog.y1;
}
else if (rabbit.locationX <= dog.locationX && rabbit.locationY <= dog.locationY)
{
dog.y1 = dog.speed * dog.sina;
dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
dog.locationX -= dog.x1;
dog.locationY -= dog.y1;
}
else if (rabbit.locationX >= dog.locationX && rabbit.locationY <= dog.locationY)
{
dog.y1 = dog.speed * dog.sina;
dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
dog.locationX += dog.x1;
dog.locationY -= dog.y1;
}
dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY));
Console.WriteLine("猎犬已跑至坐标: ({0},{1})", dog.locationX, dog.locationY);
Console.WriteLine("猎犬离兔子的距离为:{0}\n", dogDistance);
count++;
if ((rabbit.locationX == 0 && rabbit.locationY == 0) || rabbit.RabbitDistance == 0)
{
rabbit.rabbitWin = true;
Console.WriteLine("The rabbit win for {0} seconds !",count/20);
}
else if (dogDistance <= 0.02) {
dog.dogWin = true;
Console.WriteLine("The dog win for {0} seconds !", count/20);
}
}
}
static void Main(string[] args)
{
Program newStar = new Program();
Rabbit theRabbit = new Rabbit(8.0,3.0, 1);
Dog theDog = new Dog(16.0, 2.0, 1);
theDog.speed /= 20.0;
theRabbit.speed /= 20.0;
Debug.WriteLine("rabbit.sina:{0}\n", theRabbit.sina);
Console.WriteLine("兔子的坐标:({0},{1}), 速度:{2}", theRabbit.locationX, theRabbit.locationY, theRabbit.speed*20);
Console.WriteLine("猎犬的坐标:({0},{1}), 速度:{2}\n", theDog.locationX, theDog.locationY, theDog.speed*20);
Console.WriteLine("开始计算...\n");
newStar.Star(theRabbit, theDog);
Console.ReadKey();
}
}
}
C#写的,会C……