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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Data Types » ComplexScreenshots 
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 = (Complexo2;
        
        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(31);
        Complex c2 = new Complex(12);
        
        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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.