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

Home
Visual C++ .NET
1.2D
2.Class
3.Collections
4.Data Type
5.Database ADO.net
6.Delegate
7.Development
8.File Directory
9.Function
10.Generics
11.GUI Form
12.Language Basics
13.Network
14.Reflection
15.Security
16.Statement
17.Structure
18.Thread
19.XML
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Visual C++ .NET » Structure » Definition 
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^ objoverride
    {
        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(1112), b(-1314);
    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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.