Visual C++ .NET/Structure/value struct — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:05, 26 мая 2010
value struct demo
#include "stdafx.h"
using namespace System;
using namespace System::Globalization;
public value struct Complex
{
Complex(double real, double imaginary)
{
Real = real;
Imaginary = imaginary;
}
static Complex Polar(double polarRadius, double polarPhase)
{
return Complex(polarRadius*Math::Cos(polarPhase), polarRadius*Math::Sin(polarPhase));
}
property double Real;
property double Imaginary;
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;
}
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);
}
};
using namespace System;
void main()
{
array<Complex>^ data = gcnew array<Complex>(1024);
array<Complex>^ backup = gcnew array<Complex>(1024);
// 1st test
Random^ random = gcnew Random();
for ( int i = 0; i < data->Length; i++ )
{
data[i].Real = random->NextDouble();
data[i].Imaginary = random->NextDouble();
backup[i] = data[i];
}
FourierTransform^ fft = gcnew FourierTransform(data->Length, true);
fft->Forward(data);
fft->Inverse(data);
}