Complex struct : Definition « Structure « Visual C++ .NET






Complex struct

 

#include "stdafx.h"
using namespace System;
using namespace System::Globalization;

public value struct Complex
{
    // Constructor
    Complex(double real, double imaginary)
    {
        Real = real;
        Imaginary = imaginary;
    }

    // Polar form initialization
    static Complex Polar(double polarRadius, double polarPhase)
    {
        return Complex(polarRadius*Math::Cos(polarPhase), polarRadius*Math::Sin(polarPhase));
    }

    // Real and imaginary parts
    property double Real;
    property double Imaginary;

    // Conjugate value
    property Complex Conjugate
    {
        Complex get()
        {
            return Complex(Real, -Imaginary);
        }
    }

    // Absolute value
    property double Absolute
    {
        double get()
        {
            return Math::Sqrt(Square);
        }
    }

    // Absolute value squared
    property double Square
    {
        double get()
        {
            return Real*Real + Imaginary*Imaginary;
        }
    }

    // Poloar Radius
    property double PolarRadius
    {
        double get()
        {
            return Absolute;
        }
    }

    // Polar Angle
    property double PolarAngle
    {
        double get()
        {
            return Math::Atan2(Imaginary, Real);
        }
    }

    // Convert-from operator
    static operator Complex(double a)
    {
        return Complex(a, 0.0);
    }

    // Unary +/- operators
    static Complex operator +(Complex a)
    {
        return a;
    }
    static Complex operator -(Complex a)
    {
        return Complex(-a.Real, -a.Imaginary);
    }

    // Unary increment/decrement operators
    static Complex operator --(Complex a)
    {
        return Complex(a.Real - 1, a.Imaginary);
    }
    static Complex operator ++(Complex a)
    {
        return Complex(a.Real + 1, a.Imaginary);
    }

    // Comparison operators
    static bool operator ==(Complex a, Complex b)
    {
        return a.Real == b.Real && a.Imaginary == b.Imaginary;
    }
    static bool operator !=(Complex a, Complex b)
    {
        return a.Real != b.Real || a.Imaginary == b.Imaginary;
    }

    // Plus operators
    static Complex operator +(Complex a, Complex b)
    {
        return Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
    }
    static Complex operator +(Complex a, double b)
    {
        return Complex(a.Real + b, a.Imaginary);
    }

    // Minus operators
    static Complex operator -(Complex a, Complex b)
    {
        return Complex(a.Real - b.Real, a.Imaginary - b.Imaginary);
    }
    static Complex operator -(Complex a, double b)
    {
        return Complex(a.Real - b, a.Imaginary);
    }

    // Multiplication operators
    static Complex operator *(Complex a, Complex b)
    {
        return Complex(a.Real*b.Real - a.Imaginary*b.Imaginary, a.Real*b.Imaginary + a.Imaginary*b.Real);
    }
    static Complex operator *(Complex a, double b)
    {
        return Complex(a.Real*b, a.Imaginary*b);
    }

    // Division operators
    static Complex operator /(Complex a, Complex b)
    {
        double d = b.Square;
        return Complex((a.Real*b.Real + a.Imaginary*b.Imaginary)/d, (b.Real*a.Imaginary - a.Real*b.Imaginary)/d);
    }
    static Complex operator /(Complex a, double b)
    {
        return Complex(a.Real/b, a.Imaginary/b);
    }
    static Complex operator /(double a, Complex b)
    {
        return Complex(a, 0)/b;
    }

    // Friendly alrernate operator methods
    static Complex Plus(Complex a)
    {
        return a;
    }
    static Complex Negate(Complex a)
    {
        return Complex(-a.Real, -a.Imaginary);
    }
    static Complex Decrement(Complex a)
    {
        return Complex(a.Real - 1, a.Imaginary);
    }
    static Complex Increment(Complex a)
    {
        return Complex(a.Real + 1, a.Imaginary);
    }
    static Complex Add(Complex a, Complex b)
    {
        return Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
    }
    static Complex Add(Complex a, double b)
    {
        return Complex(a.Real + b, a.Imaginary);
    }
    static Complex Subtract(Complex a, Complex b)
    {
        return Complex(a.Real - b.Real, a.Imaginary - b.Imaginary);
    }
    static Complex Subtract(Complex a, double b)
    {
        return Complex(a.Real - b, a.Imaginary);
    }
    static Complex Multiply(Complex a, Complex b)
    {
        return Complex(a.Real*b.Real - a.Imaginary*b.Imaginary, a.Real*b.Imaginary + a.Imaginary*b.Real);
    }
    static Complex Multiply(Complex a, double b)
    {
        return Complex(a.Real*b, a.Imaginary*b);
    }
    static Complex Divide(Complex a, Complex b)
    {
        double d = b.Square;
        return Complex((a.Real*b.Real + a.Imaginary*b.Imaginary)/d, (b.Real*a.Imaginary - a.Real*b.Imaginary)/d);
    }
    static Complex Divide(Complex a, double b)
    {
        return Complex(a.Real/b, a.Imaginary/b);
    }
    static Complex Divide(double a, Complex b)
    {
        return Complex(a, 0)/b;
    }

    // Misc required overloads
    virtual bool Equals(Object^ obj) override
    {
        Complex^ compareTo = dynamic_cast<Complex^>(obj);
        if ( compareTo )
        {
            return *this == *compareTo;
        }
        else
        {
            return false;
        }
    }
    virtual int GetHashCode() override
    {
        return Real.GetHashCode() ^ Imaginary.GetHashCode();
    }
    virtual String^ ToString() override
    {
        return String::Format(CultureInfo::CurrentCulture->NumberFormat,
            "({0}, {1})", Real, Imaginary);
    }
};

void main()
{
    Complex a(11, 12), b(-13, 14);
    a = 1/a;
    a = 2;

    Console::WriteLine("a*b={0}", a*b);
}

   
  








Related examples in the same category

1.Declare a Structure Type
2.Create a Structure
3.Initialize a Structure
4.Access a Structure
5.struct implements IComparable