Класс Прямоугольник. Конструкторы. Деструктор. Перегрузки - C#
Формулировка задачи:
Здравствуйте, помогите, пожалуйста, с задачкой.
Разработать класс «Прямоугольник». Определить в нем конструкторы
и деструктор, перегрузить операцию пересечения прямоугольников
(операция “*”), операцию вычисления площади прямоугольника
операции сравнения (по площади), операцию преобразования в
символьную строку и метод получения объекта-прямоугольника из
строки
Пока что смогла написать только это, вообще ничего не могу понять(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{Console.ReadLine();
}
}
//класс "Прямоугольник"
class Rectangle
{
public Rectangle( )
{
}
public Rectangle(double a = 2, double b=3)
{
double s;
s = a * b;
Console.WriteLine("s={0}",s);
Console.ReadLine();
}
~Rectangle()
{
}
/* static public Rectangle operator * (Rectangle Rect1, Rectangle Rect2)
{
return new Rectangle (Rect1 * Rect2);
}
static public implicit operator string(Rectangle Rect1)
{
}*/
}
}Решение задачи: «Класс Прямоугольник. Конструкторы. Деструктор. Перегрузки»
textual
Листинг программы
internal struct Point
{
public Point(double x, double y) : this()
{
X = x;
Y = y;
}
public double X { get; set; }
public double Y { get; set; }
public override string ToString()
{
return string.Format("({0}; {1})", X, Y);
}
public static double Length(Point left, Point right)
{
return Math.Sqrt((right.X - left.X)*(right.X - left.X) + (right.Y - left.Y)*(right.Y - left.Y));
}
}
internal class Rectangle
{
private readonly Point _leftBottom;
private readonly Point _rightTop;
private double? _square;
public Rectangle(double left, double bottom, double right, double top)
: this(new Point(left, bottom), new Point(right, top))
{
}
public Rectangle(string input)
{
const string pattern = @"\d+([,.]\d+)*";
MatchCollection matchCollection = Regex.Matches(input, pattern);
double x, y;
Double.TryParse(matchCollection[0].Value, out x);
Double.TryParse(matchCollection[1].Value, out y);
_leftBottom = new Point(x, y);
Double.TryParse(matchCollection[2].Value, out x);
Double.TryParse(matchCollection[3].Value, out y);
_rightTop = new Point(x, y);
}
public Rectangle(Point leftBottom, Point rightTop)
{
_leftBottom = leftBottom;
_rightTop = rightTop;
}
public double Square
{
get
{
if (_square == null)
{
Point leftTop = new Point(_leftBottom.X, _rightTop.Y);
_square = Point.Length(_leftBottom, leftTop) *
Point.Length(_rightTop, leftTop);
}
return (double) _square;
}
}
public static bool operator ==(Rectangle left, Rectangle right)
{
if (ReferenceEquals(left, null)) return false;
if (ReferenceEquals(right, null)) return false;
return left.Square.CompareTo(right.Square) == 0;
}
public static bool operator >(Rectangle left, Rectangle right)
{
return left.Square.CompareTo(right.Square) > 0;
}
public static bool operator >=(Rectangle left, Rectangle right)
{
return left.Square.CompareTo(right.Square) >= 0;
}
public static bool operator !=(Rectangle left, Rectangle right)
{
return !(left == right);
}
public static bool operator <(Rectangle left, Rectangle right)
{
return left.Square.CompareTo(right.Square) < 0;
}
public static bool operator <=(Rectangle left, Rectangle right)
{
return left.Square.CompareTo(right.Square) <= 0;
}
public static bool operator *(Rectangle left, Rectangle right)
{
return
left._leftBottom.X < right._rightTop.X &&
left._rightTop.X > right._leftBottom.X &&
left._leftBottom.Y < right._rightTop.Y &&
left._rightTop.Y > right._leftBottom.Y;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Rectangle) obj);
}
public override int GetHashCode()
{
return _leftBottom.GetHashCode() ^ _rightTop.GetHashCode() ^ Square.GetHashCode();
}
public override string ToString()
{
return string.Format("LeftBottom: {0}, RightTop: {1}", _leftBottom, _rightTop);
}
protected bool Equals(Rectangle other)
{
return ReferenceEquals(this, other);
}
}