class test2 { //______________________________________________________________________ static void Main() { Complex a = new Complex(1, 1); Console.WriteLine("{0}", a); // (1,1i) a += 2; Console.WriteLine("{0}", a); // (3,1i) Console.ReadLine(); } //______________________________________________________________________ } }
namespace GregoryAdam.Base.MathBits { public partial struct Complex { //______________________________________________________________________ // properties public double Real { get; private set; } public double Imaginary { get; private set; } //______________________________________________________________________ // ctor public Complex(double i) : this(i, 0.0) { } //______________________________________________________________________ public Complex( double i, double j ): this() { Real = i; Imaginary = j; } //______________________________________________________________________ //______________________________________________________________________ public override int GetHashCode() { return Real.GetHashCode() ^ Imaginary.GetHashCode(); } //______________________________________________________________________ public override string ToString() { if ( Imaginary == 0.0 ) return Imaginary.ToString(); else return String.Format("({0},{1}i)", Real, Imaginary); } //______________________________________________________________________ //______________________________________________________________________ // operator addition public static Complex operator + (Complex a, Complex b) { return new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary); } //______________________________________________________________________ // operator subtraction public static Complex operator -( Complex a, Complex b ) { return new Complex(a.Real - b.Real, a.Imaginary - b.Imaginary); } //______________________________________________________________________ //______________________________________________________________________ // catches all implicit casts (int, float, decimal >> double) // implicit cast from double to Complex public static implicit operator Complex( double d ) { return new Complex(d); } //______________________________________________________________________ } }