Complex number with logic operators : Logic Operator Overload « Operator Overload « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Operator Overload » Logic Operator Overload 
8.2.1.Complex number with logic operators
using System;

public struct Complex : IComparable, IEquatable<Complex>, IComparable<Complex>{
    public Complexdouble real, double img ) {
        this.real = real;
        this.img = img;
    }

    public override bool Equalsobject other ) {
        bool result = false;
        ifother is Complex ) {
            result = Equals( (Complexother );
        }
        return result;
    }

    public bool EqualsComplex that ) {
        return (this.real == that.real && this.img == that.img);
    }

    public override int GetHashCode() {
        return (intthis.Magnitude;
    }

    public int CompareToComplex that ) {
        int result;
        ifEqualsthat ) ) {
            result = 0;
        else ifthis.Magnitude > that.Magnitude ) {
            result = 1;
        else {
            result = -1;
        }

        return result;
    }

    int IComparable.CompareToobject other ) {
        if!(other is Complex) ) {
            throw new ArgumentException"Bad Comparison" );
        }

        return CompareTo( (Complexother );
    }

    public override string ToString() {
        return String.Format"({0}, {1})", real, img );
    }

    public double Magnitude {
        get {
            return Math.SqrtMath.Pow(this.real, 2+ Math.Pow(this.img, 2) );
        }
    }

    public static bool operator==Complex lhs, Complex rhs ) {
        return lhs.Equalsrhs );
    }

    public static bool operator!=Complex lhs, Complex rhs ) {
        return !lhs.Equalsrhs );
    }

    public static bool operator<Complex lhs, Complex rhs ) {
        return lhs.CompareTorhs 0;
    }

    public static bool operator>Complex lhs, Complex rhs ) {
        return lhs.CompareTorhs 0;
    }

    public static bool operator<=Complex lhs, Complex rhs ) {
        return lhs.CompareTorhs <= 0;
    }

    public static bool operator>=Complex lhs, Complex rhs ) {
        return lhs.CompareTorhs >= 0;
    }

    private double real;
    private double img;
}

public class MainClass
{
    static void Main() {
        Complex cpx1 = new Complex1.03.0 );
        Complex cpx2 = new Complex1.02.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 );
    }
}
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
8.2.Logic Operator Overload
8.2.1.Complex number with logic operators
8.2.2.Point: Overloaded Operators
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.