316
LESSON 26 OverlOading OperatOrs
ComplexNumber - double
double - ComplexNumber
1. You can use code similar to the following:
// ComplexNumber + ComplexNumber.
public static ComplexNumber operator +(ComplexNumber me, ComplexNumber other)
{
return new ComplexNumber(
me.Real + other.Real,
me.Imaginary + other.Imaginary);
}
// ComplexNumber + double.
public static ComplexNumber operator +(ComplexNumber me, double x)
{
return new ComplexNumber(me.Real + x, me.Imaginary);
}
// double + ComplexNumber.
public static ComplexNumber operator +(double x, ComplexNumber other)
{
return other + x;
}
// ComplexNumber * ComplexNumber.
public static ComplexNumber operator *(ComplexNumber me, ComplexNumber other)
{
return new ComplexNumber(
me.Real * other.Real - me.Imaginary * other.Imaginary,
me.Real * other.Imaginary + me.Imaginary * other.Real);
}
// ComplexNumber * double.
public static ComplexNumber operator *(ComplexNumber me, double x)
{
return new ComplexNumber(me.Real * x, me.Imaginary * x);
}
// double * ComplexNumber.
public static ComplexNumber operator *(double x, ComplexNumber other)
{
return other * x;
}
// Unary -.
public static ComplexNumber operator -(ComplexNumber me)
{
return new ComplexNumber(-me.Real, -me.Imaginary);
}
596906c26.indd 316 4/7/10 12:34:04 PM
Try It
317
// ComplexNumber - ComplexNumber.
public static ComplexNumber operator -(ComplexNumber me, ComplexNumber other)
{
return me + -other;
}
// ComplexNumber - double.
public static ComplexNumber operator -(ComplexNumber me, double x)
{
return new ComplexNumber(me.Real - x, me.Imaginary);
}
// double - ComplexNumber.
public static ComplexNumber operator -(double x, ComplexNumber other)
{
return -other + x;
}
Revise the main form’s code to use the new operators.
1. You can use code similar to the following:
// Perform the calculations between two ComplexNumbers.
private void calculateButton_Click(object sender, EventArgs e)
{
ComplexNumber a = new ComplexNumber(
double.Parse(real1TextBox.Text),
double.Parse(imaginary1TextBox.Text));
ComplexNumber b = new ComplexNumber(
double.Parse(real2TextBox.Text),
double.Parse(imaginary2TextBox.Text));
ComplexNumber aPlusB = a + b;
aPlusBTextBox.Text = aPlusB.ToString();
ComplexNumber aMinusB = a - b;
aMinusBTextBox.Text = aMinusB.ToString();
ComplexNumber aTimesB = a * b;
aTimesBTextBox.Text = aTimesB.ToString();
}
// Perform the calculations with a real number.
private void calculateRealOnlyButton_Click(
object sender, EventArgs e)
{
double x = double.Parse(realOnlyTextBox.Text);
ComplexNumber b = new ComplexNumber(
double.Parse(real2TextBox.Text),
double.Parse(imaginary2TextBox.Text));
ComplexNumber xPlusB = x + b;
aPlusBTextBox.Text = xPlusB.ToString();
596906c26.indd 317 4/7/10 12:34:04 PM
318
LESSON 26 OverlOading OperatOrs
ComplexNumber xMinusB = x - b;
aMinusBTextBox.Text = xMinusB.ToString();
ComplexNumber xTimesB = x * b;
aTimesBTextBox.Text = xTimesB.ToString();
}
Please select Lesson 26 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Copy the complex number program you built in this lesson’s Try It and overload the
ComplexNumber class’s / operator to perform division using this equation:
a + bi
c + di
=
ac + bd
c
2
+ d
2
+i
(())
bc – ad
c
2
+ d
2
()
Use this operator to define operators for ComplexNumber / double and double /
ComplexNumber. (Hint: Dont perform all of the calculations for these. Convert the double
into a
ComplexNumber and then use the previous definition of /.)
Change the main program to calculate A / B. Verify these calculations:
(10+11i) / (3+2i) = 4 + 1i
(15+24i) / 3 = 5 + 8i
4 / (1+1i) = 2 - 2i
You can download the solution to this exercise from the book’s web page
at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find the
solution in the Lesson26 folder.
596906c26.indd 318 4/7/10 12:34:05 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.126