-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOperatorOverloadingFull.cs
82 lines (65 loc) · 1.92 KB
/
OperatorOverloadingFull.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public class Coordinate
{
public int X { get; private set; }
public int Y { get; private set; }
public int Z { get; private set; }
public Coordinate(int x, int y, int z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public static Coordinate operator +(in Coordinate a, in Coordinate b) =>
new Coordinate (a.X + b.X , a.Y + b.Y , a.Z + b.Z);
public static Coordinate operator -(in Coordinate a, in Coordinate b) =>
new Coordinate(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
public static Coordinate operator *(in Coordinate a, in Coordinate b) =>
new Coordinate(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
public static bool operator ==(in Coordinate a, in Coordinate b)
{
if(a.X == b.X && a.Y == b.Y && a.Z == b.Z)
return true;
return false;
}
public static bool operator !=(in Coordinate a, in Coordinate b) => !(a == b);
public static Coordinate operator ++(Coordinate a) =>
new Coordinate(++a.X,++a.Y,++a.Z);
public static Coordinate operator --(Coordinate a) =>
new Coordinate(--a.X, --a.Y, --a.Z);
public static Coordinate operator %(in Coordinate a, in Coordinate b)
{
if( b.X != 0 && b.Y != 0 && b.Z != 0)
return new Coordinate(a.X % b.X, a.Y % b.Y, a.Z % b.Z);
else
throw new DivideByZeroException();
}
public static Coordinate operator /(in Coordinate a, in Coordinate b)
{
if( b.X != 0 && b.Y != 0 && b.Z != 0)
return new Coordinate(a.X / b.X, a.Y / b.Y, a.Z / b.Z);
else
throw new DivideByZeroException();
}
public static (double X, double Y, double Z) operator
^(in Coordinate a, in Coordinate b)
{
double x = Math.Pow(a.X, b.X);
double y = Math.Pow(a.Y, b.Y);
double z = Math.Pow(a.Z, b.Z);
return (x,y,z);
}
public override bool Equals(object obj)
{
if(obj is not null)
{
Coordinate coordinate = (Coordinate)obj;
return this == coordinate;
}
return false;
}
public override int GetHashCode()
{
string hash = $"{X}+{Y}+{Z}";
return hash.GetHashCode();
}
}