Wednesday, May 13, 2009

LAB 15

Lab 15 Be sure to put each class in its own file. Enhance the ComplexNumber class by overloading the / (division) operator. The implementation does not have to be mathematically precise. Accept the input values from the keyboard. Put the whole thing in a loop and provide an appropriate way to terminate the loop.

// Fig 11.18: OperatorOverloading.cs
// Overloading operators for complex numbers.
using System;

public class ComplexTest
{
public static void Main( string[] args )
{
// declare two variables to store complex numbers
// to be entered by user
ComplexNumber x, y;
string inputkey;
inputkey = "n";
while (inputkey != "q" && inputkey != "Q")

{
// prompt the user to enter the first complex number
Console.Write( "Enter the real part of complex number x: " );
double realPart = Convert.ToDouble( Console.ReadLine() );
Console.Write(
"Enter the imaginary part of complex number x: " );
double imaginaryPart = Convert.ToDouble( Console.ReadLine() );
x = new ComplexNumber( realPart, imaginaryPart );

// prompt the user to enter the second complex number
Console.Write( "\nEnter the real part of complex number y: " );
realPart = Convert.ToDouble( Console.ReadLine() );
Console.Write(
"Enter the imaginary part of complex number y: " );
imaginaryPart = Convert.ToDouble( Console.ReadLine() );
y = new ComplexNumber( realPart, imaginaryPart );
// display the results of calculations with x and y
Console.WriteLine();
Console.WriteLine( "{0} + {1} = {2}", x, y, x + y );
Console.WriteLine( "{0} - {1} = {2}", x, y, x - y );
Console.WriteLine( "{0} * {1} = {2}", x, y, x * y );
Console.WriteLine("{0} / {1} = {2}", x, y, x / y);
Console.WriteLine("Press Enter to continue, Press q to quit!");
inputkey = Convert.ToString(Console.ReadLine());

Console.WriteLine("");

} //while loop
Console.WriteLine("Thank you!");

} // end method Main
} // end class ComplexTest


// Fig. 11.17: ComplexNumber.cs
// Class that overloads operators for adding, subtracting
// and multiplying complex numbers.
using System;

public class ComplexNumber
{
private double real; // real component of the complex number
private double imaginary; // imaginary component of the complex number

// constructor
public ComplexNumber( double a, double b )
{
real = a;
imaginary = b;
} // end constructor

// return string representation of ComplexNumber
public override string ToString()
{
return string.Format( "({0} {1} {2}i)",
Real, ( Imaginary < 0 ? "-" : "+" ), Math.Abs( Imaginary ) );
} // end method ToString

// read-only property that gets the real component
public double Real
{
get
{
return real;
} // end get
} // end property Real

// read-only property that gets the imaginary component
public double Imaginary
{
get
{
return imaginary;
} // end get
} // end property Imaginary

// overload the addition operator
public static ComplexNumber operator+(
ComplexNumber x, ComplexNumber y )
{
return new ComplexNumber( x.Real + y.Real,
x.Imaginary + y.Imaginary );
} // end operator +

// overload the subtraction operator
public static ComplexNumber operator-(
ComplexNumber x, ComplexNumber y )
{
return new ComplexNumber( x.Real - y.Real,
x.Imaginary - y.Imaginary );
} // end operator -

// overload the multiplication operator
public static ComplexNumber operator*(
ComplexNumber x, ComplexNumber y )
{
return new ComplexNumber(
x.Real * y.Real - x.Imaginary * y.Imaginary,
x.Real * y.Imaginary + y.Real * x.Imaginary );
} // end operator *
public static ComplexNumber operator /(
ComplexNumber x, ComplexNumber y)
{
return new ComplexNumber(
x.Real / y.Real - x.Imaginary / y.Imaginary,
x.Real / y.Imaginary + y.Real / x.Imaginary);
} // end operator /
} // end class ComplexNumber

No comments:

Post a Comment