A Complex Number Class : Complex « Data Types « C# / C Sharp






A Complex Number Class

A Complex Number Class
  
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 25 - Operator Overloading\A Complex Number Class
// copyright 2000 Eric Gunnerson
using System;

struct Complex
{
    float real;
    float imaginary;
    
    public Complex(float real, float imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }
    
    public float Real
    {
        get
        {
            return(real);
        }
        set
        {
            real = value;
        }
    }
    
    public float Imaginary
    {
        get
        {
            return(imaginary);
        }
        set
        {
            imaginary = value;
        }
    }
    
    public override string ToString()
    {
        return(String.Format("({0}, {1}i)", real, imaginary));
    }
    
    public static bool operator==(Complex c1, Complex c2)
    {
        if ((c1.real == c2.real) &&
        (c1.imaginary == c2.imaginary))
        return(true);
        else
        return(false);
    }
    
    public static bool operator!=(Complex c1, Complex c2)
    {
        return(!(c1 == c2));
    }
    
    public override bool Equals(object o2)
    {
        Complex c2 = (Complex) o2;
        
        return(this == c2);
    }
    
    public override int GetHashCode()
    {
        return(real.GetHashCode() ^ imaginary.GetHashCode());
    }
    
    public static Complex operator+(Complex c1, Complex c2)
    {
        return(new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary));
    }
    
    public static Complex operator-(Complex c1, Complex c2)
    {
        return(new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary));
    }
    
    // product of two complex numbers
    public static Complex operator*(Complex c1, Complex c2)
    {
        return(new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
        c1.real * c2.imaginary + c2.real * c1.imaginary));
    }
    
    // quotient of two complex numbers
    public static Complex operator/(Complex c1, Complex c2)
    {
        if ((c2.real == 0.0f) &&
        (c2.imaginary == 0.0f))
        throw new DivideByZeroException("Can't divide by zero Complex number");
        
        float newReal = 
        (c1.real * c2.real + c1.imaginary * c2.imaginary) /
        (c2.real * c2.real + c2.imaginary * c2.imaginary);
        float newImaginary = 
        (c2.real * c1.imaginary - c1.real * c2.imaginary) /
        (c2.real * c2.real + c2.imaginary * c2.imaginary);
        
        return(new Complex(newReal, newImaginary));
    }
    
    // non-operator versions for other languages
    public static Complex Add(Complex c1, Complex c2)
    {
        return(c1 + c2);
    }
    
    public static Complex Subtract(Complex c1, Complex c2)
    {
        return(c1 - c2);
    }
    
    public static Complex Multiply(Complex c1, Complex c2)
    {
        return(c1 * c2);
    }
    
    public static Complex Divide(Complex c1, Complex c2)
    {
        return(c1 / c2);
    }
}

public class AComplexNumberClass
{
    public static void Main()
    {
        Complex c1 = new Complex(3, 1);
        Complex c2 = new Complex(1, 2);
        
        Console.WriteLine("c1 == c2: {0}", c1 == c2);
        Console.WriteLine("c1 != c2: {0}", c1 != c2);
        Console.WriteLine("c1 + c2 = {0}", c1 + c2);
        Console.WriteLine("c1 - c2 = {0}", c1 - c2);
        Console.WriteLine("c1 * c2 = {0}", c1 * c2);
        Console.WriteLine("c1 / c2 = {0}", c1 / c2);
    }
}

           
         
    
  








Related examples in the same category

1.Complex number class
2.Class to do math on complex numbers.
3.Initializes a new instance of the Complex structure using the specified real and imaginary values.
4.Complex Structure represents a complex number.
5.Assign a Double to a complex number
6.Cast a Decimal to a complex number
7.Assign the return value of a method to a Complex variable
8.Assign the value returned by an operator to a Complex variable
9.Instantiate a complex number from its polar coordinates
10.Complex.Exp
11.Complex.Sqrt
12.Divide and multiply
13.Gets the absolute value (or magnitude) of a complex number.
14.Returns the angle that is the arc cosine of the specified complex number.
15.Adds two complex numbers and returns the result.
16.Adds two complex numbers.
17.Returns the angle that is the arc sine of the specified complex number.
18.Returns the angle that is the arc tangent of the specified complex number.
19.Computes the conjugate of a complex number and returns the result.
20.Divides one complex number by another and returns the result.
21.Returns a value that indicates whether the current instance and a specified complex number have the same value.
22.Returns e raised to the power specified by a complex number.
23.Creates a complex number from a point's polar coordinates.
24.Gets the imaginary component of the current Complex object.
25.Returns a new Complex instance with a real number equal to zero and an imaginary number equal to one.
26.Returns the product of two complex numbers.
27.Returns the additive inverse of a specified complex number.
28.Returns a new Complex instance with a real number equal to one and an imaginary number equal to zero.
29.Returns a specified complex number raised to a power specified by a double-precision floating-point number.
30.Returns the multiplicative inverse of a complex number.
31.Subtracts one complex number from another and returns the result.
32.Converts current complex number to string in Cartesian form by using the specified culture-specific formatting information.
33.Converts current complex number to string in Cartesian form by using the specified format for its real and imaginary parts.
34.Converts complex number to string representation in Cartesian form by using the specified format and culture-specific format information for its real and imaginary parts.
35.Converts the value of the current complex number to its equivalent string representation in Cartesian form.
36.Returns a new Complex instance with a real number equal to zero and an imaginary number equal to zero.