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

           
         
     


Demonstates using checked keyword to detect an overflow 2


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
//  OvrFlow1.cs -- Demonstates using checked keyword to detect an overflow.
//
//          Compile this program with the following command line:
//              C:>csc OvrFlow1.cs
//
namespace nsOverflow
{
    using System;
    
    public class OvrFlow1
    {
        static public void Main ()
        {
            int large = 2147483647;
            int larger = large;
            try
            {
                larger = checked (++larger);
            }
            catch (OverflowException e)
            {
                Console.WriteLine ("The operation caused an overflow");
                Console.WriteLine (e.Message);
            }
            Console.WriteLine ("large = " + large);
            Console.WriteLine ("larger = " + larger);
        }
    }
}


           
          


Checking for overflows.

   
 


using System;
public class MainClass {
    public int CheckedAddition(short s1, short s2) {
        int z = 0;
        try {
            z = checked((short)(s1 + s2));
        } catch (OverflowException) {
            Console.WriteLine("Overflow Exception, Returning 0");
        }
        return z;
    }
    public int UncheckedAddition(short s1, short s2) {
        int z = ((short)(s1 + s2));
        return z;
    }

    public static void Main() {
        MainClass app = new MainClass();
        short s1 = 32767;
        short s2 = 32767;

        Console.WriteLine("Checked Addition is: {0}",app.CheckedAddition(s1, s2));
        Console.WriteLine("Unchecked Addition is: {0}",app.UncheckedAddition(s1, s2));
    }
}

    


Checked and Unchecked

   



using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Max value of byte is {0}.", byte.MaxValue);
        Console.WriteLine("Min value of byte is {0}.", byte.MinValue);
        byte b1 = 100;
        byte b2 = 250;

        try {
            checked {
                byte sum = (byte)(b1 + b2);
                byte b4, b5 = 100, b6 = 200;
                b4 = (byte)(b5 + b6);
                Console.WriteLine("sum = {0}", sum);
            }
        } catch (OverflowException e) {
            Console.WriteLine(e.Message);
        }

        unchecked {
            byte sum = (byte)(b1 + b2);
            Console.WriteLine("sum = {0}", sum);
        }
    }
}