Last active
August 29, 2015 14:26
-
-
Save yanisurbis/c8cb143d43d9186ba8e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication3 | |
{ | |
class My | |
{ | |
public static void Main() | |
{ | |
MyPoint n = new MyPoint { X = 1, Y = 2, Z = 3 }; | |
MyPoint l = new MyPoint { X = 2, Y = 3, Z = 4 }; | |
//if (n.CompareTo(l) ) | |
l.FillArray(5); | |
MyPoint[] arr = l.GetArr(); | |
Array.Sort(arr, MyPoint.SortByX); | |
foreach(var i in arr) | |
Console.WriteLine(i.X); | |
Console.WriteLine("\nSECOND FASE : ITERATORS"); | |
Console.WriteLine(l[1]); | |
l[1] = new MyPoint { X = 2, Y = 3, Z = 4 }; | |
Console.WriteLine(l[1]); | |
Console.WriteLine(l["second"]); | |
Console.WriteLine("\nSECOND FASE : OPETORS OVERLOADING"); | |
Console.WriteLine(n + l); | |
Console.WriteLine(n + 123); | |
Console.Read(); | |
} | |
} | |
class MyPoint : IEnumerable, ICloneable, IComparable | |
{ | |
int x; | |
int y; | |
int z; | |
MyPoint[] arr; | |
public int X | |
{ | |
get { return x; } | |
set { x = value; } | |
} | |
public int Y | |
{ | |
get { return y; } | |
set { y = value; } | |
} | |
public int Z | |
{ | |
get { return z; } | |
set { z = value; } | |
} | |
public static IComparer SortByX | |
{ | |
get | |
{ | |
return (IComparer)new PointComparer(); | |
} | |
} | |
public MyPoint() | |
{ | |
Random r = new Random(); | |
x = r.Next(1, 101); | |
y = r.Next(1, 101); | |
z = r.Next(1, 101); | |
} | |
public MyPoint this[int index] | |
{ | |
get | |
{ | |
if (arr == null) | |
{ | |
Console.WriteLine("Empty!"); | |
return null; | |
} | |
return arr[index]; | |
} | |
set | |
{ | |
if (arr == null) | |
{ | |
Console.WriteLine("Empty"); | |
} | |
else | |
{ | |
arr[index] = value; | |
} | |
} | |
} | |
public MyPoint this[string s] | |
{ | |
get | |
{ | |
if (arr == null) | |
{ | |
Console.WriteLine("Empty!"); | |
return null; | |
} | |
if (s == "first") | |
return arr[1]; | |
else if (s == "second") | |
return arr[2]; | |
return new MyPoint(); | |
} | |
set | |
{ | |
if (arr == null) | |
{ | |
Console.WriteLine("Empty"); | |
} | |
else | |
{ | |
if (s == "first") | |
arr[1] = value; | |
else if (s == "second") | |
arr[2] = value; | |
} | |
} | |
} | |
public MyPoint[] GetArr() | |
{ | |
return arr; | |
} | |
public MyPoint(int x, int y, int z) | |
{ | |
X = x; | |
Y = y; | |
Z = z; | |
} | |
public int CompareTo(object o) | |
{ | |
MyPoint p = o as MyPoint; | |
if (p != null) | |
{ | |
if (this.X > p.X) | |
{ | |
return -1; | |
} | |
else | |
{ | |
if (this.X > p.X) | |
return 1; | |
else | |
return 0; | |
} | |
} | |
return 0; | |
} | |
public object Clone() | |
{ | |
return new MyPoint { X = this.X, Y = this.Y, Z = this.Z }; | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
return new MyEnumerator(this); | |
} | |
public IEnumerable GetPoints(bool b) | |
{ | |
return new MyEnumerable(this); | |
} | |
class MyEnumerable : IEnumerable | |
{ | |
MyPoint p; | |
public MyEnumerable(MyPoint p) | |
{ | |
this.p = p; | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
return new MyPoint.MyEnumerator(p); | |
} | |
} | |
class MyEnumerator : IEnumerator | |
{ | |
MyPoint[] arr; | |
int len; | |
int i; | |
public MyEnumerator(MyPoint x) | |
{ | |
arr = x.arr; | |
len = x.arr.Length; | |
i = 0; | |
} | |
public bool MoveNext() | |
{ | |
return i != len ? true : false; | |
} | |
public object Current | |
{ | |
get | |
{ | |
return arr[i++]; | |
} | |
} | |
public void Reset() | |
{ | |
i = 0; | |
} | |
} | |
public void FillArray(int n) | |
{ | |
arr = new MyPoint[n]; | |
for (int i = 0; i < n; i++) | |
arr[i] = new MyPoint { X = i, Y = i + 1, Z = i + 2 }; | |
} | |
public override string ToString() | |
{ | |
return String.Format("First field = {0}\nSecond field = {1}\nThird field = {2}\n", x, y, z); | |
} | |
public override bool Equals(object obj) | |
{ | |
MyPoint p = obj as MyPoint; | |
if (p != null) | |
{ | |
if (this.x == p.x && this.y == p.y && this.Z == p.Z) | |
return true; | |
} | |
return false; | |
} | |
public override int GetHashCode() | |
{ | |
return this.ToString().GetHashCode(); | |
} | |
public static MyPoint operator + (MyPoint p1, MyPoint p2) | |
{ | |
//null reference | |
return new MyPoint { X = p1.X + p2.X, Y = p1.Y + p2.Y, Z = p1.Z + p2.Z }; | |
} | |
public static MyPoint operator + (MyPoint p1, int i) | |
{ | |
return new MyPoint { X = p1.X + i, Y = p1.Y + i, Z = p1.Z + i }; | |
} | |
public static MyPoint operator ++ (MyPoint p1) | |
{ | |
return new MyPoint { X = p1.X + 1, Y = p1.Y + 1, Z = p1.Z + 1 }; | |
} | |
public static bool operator == (MyPoint p1, MyPoint p2) | |
{ | |
if (p1 == null || p2 == null) | |
return false; | |
return p1.Equals(p2); | |
} | |
public static bool operator != (MyPoint p1, MyPoint p2) | |
{ | |
if (p1 == null || p2 == null) | |
return false; | |
return !(p1.Equals(p2)); | |
} | |
} | |
class PointComparer : IComparer | |
{ | |
public int Compare (object o1, object o2) | |
{ | |
MyPoint p1 = o1 as MyPoint; | |
MyPoint p2 = o2 as MyPoint; | |
if (p1 != null && p2 != null) | |
{ | |
return p1.CompareTo(p2); | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment