Visual C++ .NET/Structure/Definition

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Access a Structure

 
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
struct MyInfo {
         int x,y;
};
int square(int a) {
         return a * a;
}
int main(void) {
         MyInfo info;
         info.x = 10;
         info.y = 20;
         Console::WriteLine(square(info.x));
}


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);
}


Create a Structure

 

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
struct Point { int x; int y; };
struct TwoPoints { Point point1; Point point2; };
int main(void)
{
    TwoPoints myPoints;
    myPoints.point1.x = 10;
    myPoints.point1.y = 15;
    myPoints.point2.x = 13;
    myPoints.point2.y = 25;
    Console::WriteLine(myPoints.point1.x);
    Console::WriteLine(myPoints.point1.y);
    Console::WriteLine(myPoints.point2.x);
    Console::WriteLine(myPoints.point2.y);
    return 0;
}


Declare a Structure Type

 
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
struct Author {
    char *Name;
    char *DateOfBirth;
    char *HomeTown;
};
struct Book{
    char *Title;
    char *Artist;
    Author author;
    int YearReleased;
};
int main(void)
{
   Book myBook;
   myBook.Title = "C++";
   myBook.Artist = "nfex";
   myBook.author.Name = "Jack";
   
}


Initialize a Structure

 
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
struct Book {
    char *Title;
    char *Artist;
    int YearReleased;
    Book(char *t, char *a, int y) {
        Title = t; Artist = a; YearReleased = y;
    }
};
int main(void)
{
    Book b1("A","B",2001);
    Book *b2 = new Book("A","B",1974);
    Console::WriteLine(b1.Artist);
    Console::WriteLine(b2->Artist);
    return 0;
}


struct implements IComparable

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
public value struct Vector: public IComparable
{
    property double X;
    property double Y;
    property double Magnitude
    {
        double get()
        {
            return Math::Sqrt(X*X + Y*Y);
        }
    }
    virtual int CompareTo(Object^ obj)
    {
        Vector^ v = *dynamic_cast<Vector^>(obj);
        if ( v )
        {
            if ( this->Magnitude == v->Magnitude )
                return 0;
            else if ( this->Magnitude < v->Magnitude )
                return -1;
            else
                return 1;
        }
        else
            throw gcnew ArgumentException(
                "Vector::CompareTo: obj must be of type "Vector"");
    }
    // Comparison operators
    static bool operator ==(Vector a, Vector b)
    {
        return a.X == b.X && a.Y == b.Y;
    }
    static bool operator !=(Vector a, Vector b)
    {
        return a.X != b.X || a.Y == b.Y;
    }
    static bool operator >(Vector a, Vector b)
    {
        return a.Magnitude > b.Magnitude;
    }
    static bool operator <(Vector a, Vector b)
    {
        return a.Magnitude < b.Magnitude;
    }
    // Misc required overloads
    virtual bool Equals(Object^ obj) override
    {
        Vector^ compareTo = dynamic_cast<Vector^>(obj);
        if ( compareTo )
        {
            return *this == *compareTo;
        }
        else
        {
            return false;
        }
    }
    virtual int GetHashCode() override
    {
        return X.GetHashCode() ^ Y.GetHashCode();
    }
    virtual String^ ToString() override
    {
        return String::Format(CultureInfo::CurrentCulture->NumberFormat,
            "[{0}, {1}]", X, Y);
    }
};

void main()
{
    array<Vector>^ data = gcnew array<Vector>(10);
    // Sorting test
    Random^ random = gcnew Random();
    for ( int i = 0; i < data->Length; i++ )
    {
        data[i].X = random->NextDouble();
        data[i].Y = random->NextDouble();
    }
    Array::Sort(data);
    for ( int i = 0; i < data->Length; i++ )
    {
        Console::WriteLine("data[{0}]={1}", i, data[i].Magnitude);
    }
}