c#编程题

2024-12-16 00:21:06
推荐回答(3个)
回答1:

class Program
{
static void Main(string[] args)
{
//定义一个点
Cpoint cpoint = new Cpoint(1,2);
Console.WriteLine("点的坐标为({0},{1})",cpoint.X,cpoint.Y);

//定义一条直线
Cline cline = new Cline(1, 1, 3, 3);
Console.WriteLine("直线的长度为{0}", cline.GetLength());

//定义一个矩形
Crect crect = new Crect(1, 1, 3, 3);
Console.WriteLine("矩形的周长为{0},面积为{1}", crect.GetLength(), crect.GetArea());

Console.ReadLine();
}
}

class Cpoint
{//点类

private double x;//坐标x轴的值
public double X
{
get { return x; }
set { x = value; }
}

private double y;//坐标y轴的值
public double Y
{
get { return y; }
set { y = value; }
}

public Cpoint()
{//没有参数的构造函数
this.X = 0;
this.Y = 0;
}

public Cpoint(double x, double y)
{//有参数的构造函数
this.X = x;
this.Y = y;
}
}

class Cline : Cpoint
{
//直线类

private Cpoint a = new Cpoint();//直线的一个端点
public Cpoint A
{
get { return a; }
set { a = value; }
}

private Cpoint b = new Cpoint();//直线的另一个端点
public Cpoint B
{
get { return b; }
set { b = value; }
}

//有参数的构造函数
public Cline(double a1, double a2, double b1, double b2)
{
A.X = a1;
A.Y = a2;
B.X = b1;
B.Y = b2;
}

//求直线长度
public double GetLength()
{
double m = A.X - B.X;
double n = A.Y - B.Y;

return Math.Sqrt(Math.Pow(m, 2) + Math.Pow(n, 2));
}
}

class Crect : Cpoint
{//矩形类

private Cpoint a = new Cpoint();//矩形的一个端点
public Cpoint A
{
get { return a; }
set { a = value; }
}

private Cpoint b = new Cpoint();//与a为对角的点
public Cpoint B
{
get { return b; }
set { b = value; }
}

//有参数的构造函数
public Crect(double a1, double a2, double b1, double b2)
{
A.X = a1;
A.Y = a2;
B.X = b1;
B.Y = b2;
}

//求周长
public double GetLength()
{
double m = Math.Abs(A.X - B.X);
double n = Math.Abs(A.Y - B.Y);
return 2.0 * (m + n);
}

//求面积
public double GetArea()
{
double m = Math.Abs(A.X - B.X);
double n = Math.Abs(A.Y - B.Y);
return m * n;
}
}

回答2:

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Crect rect = new Crect() { X = 1, X2 = 2, Y = 0, Y2 = 1 };
Console.WriteLine(rect.Length);
Console.WriteLine(rect.Perimeter);
Console.WriteLine(rect.Area);
}
}

internal class Cpoint
{
public double X { get; set; }
public double Y { get; set; }
}

internal class Cline : Cpoint
{
public double X2 { get; set; }
public double Y2 { get; set; }

public double Length
{
get
{
return Math.Sqrt(Math.Pow(Math.Abs(X2 - X), 2.0) + Math.Pow(Math.Abs(Y2 - Y), 2.0));
}
}
}

internal class Crect : Cline
{
public double Perimeter
{
get
{
return 2 * (Math.Abs(X2 - X) + Math.Abs(Y2 - Y));
}
}

public double Area
{
get
{
return Math.Abs(X2 - X) * Math.Abs(Y2 - Y);
}
}
}
}

回答3:

怎么你这么多题目啊,
你的cRect 是继承cline 呢还是CPoint