// 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