一个c#的简单程序代码怎么写?

2024年11月14日 16:35
有1个网友回答
网友(1):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2Round
{
class Program
{
static void Main(string[] args)
{
try
{

Point center = InputPoint("圆心");

Console.WriteLine("请输入圆的半径:");

int r = int.Parse(Console.ReadLine());

Round c = new Round() { R = r, Center = center };
Point otherPoint = InputPoint("另一个点");

Console.WriteLine(c);
Console.WriteLine("另一个点在圆的:{0}",c.Compare(otherPoint));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

static Point InputPoint(string PointName)
{
Point result = new Point() ;

try
{
Console.WriteLine("请输入{0}的X:",PointName);
result.X = int.Parse(Console.ReadLine());
Console.WriteLine("请输入{1}的Y:", PointName);
result.Y = int.Parse(Console.ReadLine());
}
catch
{

throw;
}
return result;

}

//static void Run(int x1,int y1,int r,int x2,int y2)
//{

//}
}

public class Round
{
public Point Center { get; set; }
//public int R { get; set; }
private int _r = 0;

public int R
{
get { return _r; }
set {
if (value <0)
{
throw new Exception("半径不能小于0");

}
_r = value;
}
}

public double Area
{
get
{
return Math.PI * Math.Pow(this._r, 2);
}
}

public Position Compare(Point otherPoint)
{
Position result = Position.OutOf;
double distance = 0;

distance = this.CalDistance(otherPoint);

if (this._r>distance)
{
result = Position.In;
}
else if (this._r == distance)
{
result = Position.On;
}
return result;

}

private double CalDistance(Point otherPoint)
{
double result = 0;
result = Math.Sqrt(
Math.Pow(this.Center.X-otherPoint.X,2)
+
Math.Pow(this.Center.Y - otherPoint.Y, 2)
);

return result;
}

public override string ToString()
{
return string.Format("圆心:{0},半径:{1},面积:{2}", this.Center, this._r, this.Area);
}
}

public enum Position
{
In,
On,
OutOf
}
public struct Point
{
public int X;
public int Y;
//public int Z { get; set; }
public override string ToString()
{
return string.Format("({0},{1})", this.X, this.Y);
//return base.ToString();
}
}
}