Csharp/CSharp Tutorial/Operator Overload/Logic Operator Overload
Complex number with logic operators
<source lang="csharp">using System; public struct Complex : IComparable, IEquatable<Complex>, IComparable<Complex>{
public Complex( double real, double img ) { this.real = real; this.img = img; } public override bool Equals( object other ) { bool result = false; if( other is Complex ) { result = Equals( (Complex) other ); } return result; } public bool Equals( Complex that ) { return (this.real == that.real && this.img == that.img); } public override int GetHashCode() { return (int) this.Magnitude; } public int CompareTo( Complex that ) { int result; if( Equals( that ) ) { result = 0; } else if( this.Magnitude > that.Magnitude ) { result = 1; } else { result = -1; } return result; } int IComparable.rupareTo( object other ) { if( !(other is Complex) ) { throw new ArgumentException( "Bad Comparison" ); } return CompareTo( (Complex) other ); } public override string ToString() { return String.Format( "({0}, {1})", real, img ); } public double Magnitude { get { return Math.Sqrt( Math.Pow(this.real, 2) + Math.Pow(this.img, 2) ); } } public static bool operator==( Complex lhs, Complex rhs ) { return lhs.Equals( rhs ); } public static bool operator!=( Complex lhs, Complex rhs ) { return !lhs.Equals( rhs ); } public static bool operator<( Complex lhs, Complex rhs ) { return lhs.rupareTo( rhs ) < 0; } public static bool operator>( Complex lhs, Complex rhs ) { return lhs.rupareTo( rhs ) > 0; } public static bool operator<=( Complex lhs, Complex rhs ) { return lhs.rupareTo( rhs ) <= 0; } public static bool operator>=( Complex lhs, Complex rhs ) { return lhs.rupareTo( rhs ) >= 0; } private double real; private double img;
} public class MainClass {
static void Main() { Complex cpx1 = new Complex( 1.0, 3.0 ); Complex cpx2 = new Complex( 1.0, 2.0 ); Console.WriteLine( "cpx1 = {0}, cpx1.Magnitude = {1}", cpx1, cpx1.Magnitude ); Console.WriteLine( "cpx2 = {0}, cpx2.Magnitude = {1}\n", cpx2, cpx2.Magnitude ); Console.WriteLine( "cpx1 == cpx2 ? {0}", cpx1 == cpx2 ); Console.WriteLine( "cpx1 != cpx2 ? {0}", cpx1 != cpx2 ); Console.WriteLine( "cpx1 < cpx2 ? {0}", cpx1 < cpx2 ); Console.WriteLine( "cpx1 > cpx2 ? {0}", cpx1 > cpx2 ); Console.WriteLine( "cpx1 <= cpx2 ? {0}", cpx1 <= cpx2 ); Console.WriteLine( "cpx1 >= cpx2 ? {0}", cpx1 >= cpx2 ); }
}</source>
cpx1 = (1, 3), cpx1.Magnitude = 3.16227766016838 cpx2 = (1, 2), cpx2.Magnitude = 2.23606797749979 cpx1 == cpx2 ? False cpx1 != cpx2 ? True cpx1 < cpx2 ? False cpx1 > cpx2 ? True cpx1 <= cpx2 ? False cpx1 >= cpx2 ? True
Point: Overloaded Operators
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; public struct Point : IComparable {
private int x, y; public Point(int xPos, int yPos) { x = xPos; y = yPos; } public static Point operator +(Point p1, Point p2) { return new Point(p1.x + p2.x, p1.y + p2.y); } public static Point operator -(Point p1, Point p2) { return new Point(p1.x - p2.x, p1.y - p2.y); } public static bool operator ==(Point p1, Point p2) { return p1.Equals(p2); } public static bool operator !=(Point p1, Point p2) { return !p1.Equals(p2); } public static bool operator <(Point p1, Point p2) { return (p1.rupareTo(p2) < 0); } public static bool operator >(Point p1, Point p2) { return (p1.rupareTo(p2) > 0); } public static bool operator <=(Point p1, Point p2) { return (p1.rupareTo(p2) <= 0); } public static bool operator >=(Point p1, Point p2) { return (p1.rupareTo(p2) >= 0); } public static Point operator ++(Point p1) { return new Point(p1.x + 1, p1.y + 1); } public static Point operator --(Point p1) { return new Point(p1.x - 1, p1.y - 1); } public override bool Equals(object o) { if (o is Point) { if (((Point)o).x == this.x && ((Point)o).y == this.y) return true; } return false; } public override int GetHashCode() { return this.ToString().GetHashCode(); } public override string ToString() { return string.Format("[{0}, {1}]", this.x, this.y); } public int CompareTo(object obj) { if (obj is Point) { Point p = (Point)obj; if (this.x > p.x && this.y > p.y) return 1; if (this.x < p.x && this.y < p.y) return -1; else return 0; } else throw new ArgumentException(); } public static Point Add(Point p1, Point p2) { return p1 + p2; } public static Point Subtract(Point p1, Point p2) { return p1 - p2; }
} class Program {
static void Main(string[] args) { Point ptOne = new Point(100, 100); Point ptTwo = new Point(40, 40); Console.WriteLine("ptOne = {0}", ptOne); Console.WriteLine("ptTwo = {0}", ptTwo); Console.WriteLine("ptOne + ptTwo: {0} ", ptOne + ptTwo); Console.WriteLine("Point.Add(ptOne, ptTwo): {0} ", Point.Add(ptOne, ptTwo)); Console.WriteLine("ptOne - ptTwo: {0} ", ptOne - ptTwo); Console.WriteLine("Point.Subtract(ptOne, ptTwo): {0} ", Point.Subtract(ptOne, ptTwo)); Point ptThree = new Point(90, 5); Console.WriteLine("ptThree = {0}", ptThree); Console.WriteLine("ptThree += ptTwo: {0}", ptThree += ptTwo); Point ptFour = new Point(0, 500); Console.WriteLine("ptFour = {0}", ptFour); Console.WriteLine("ptFour -= ptThree: {0}", ptFour -= ptThree); Point ptFive = new Point(10, 10); Console.WriteLine("ptFive = {0}", ptFive); Console.WriteLine("++ptFive = {0}", ++ptFive); Console.WriteLine("--ptFive = {0}", --ptFive); Console.WriteLine("ptOne == ptTwo : {0}", ptOne == ptTwo); Console.WriteLine("ptOne != ptTwo : {0}", ptOne != ptTwo); Console.WriteLine("ptOne < ptTwo : {0}", ptOne < ptTwo); Console.WriteLine("ptOne > ptTwo : {0}", ptOne > ptTwo); }
}</source>