祇要簡單的控制台就行了```
為了要遇見妳``我連呼吸都反複練習`
using System;
namespace 重载_复数加减
{
public struct Complex
{
public int realpart;
public int imaginarypart;
public Complex(int realpart, int imaginarypart)
{
this.realpart = realpart;
this.imaginarypart = imaginarypart;
}
// Declare which operator to overload (+), the types
// that can be added (two Complex objects), and the
// return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.realpart + c2.realpart, c1.imaginarypart + c2.imaginarypart);
}
// Override the ToString method to display an complex number in the suitable format:
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex (c1.realpart - c2.realpart ,c1.imaginarypart - c2.imaginarypart );
}
public override string ToString()
{
return(String.Format("{0} + {1}i", realpart, imaginarypart));
}
public static void Main()
{
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);
// Add two Complex objects (num1 and num2) through the
// overloaded plus operator:
Complex sum = num1 + num2;
Complex sub = num1 - num2;
// Print the numbers and the sum using the overriden ToString method:
Console.WriteLine("First complex number: {0}",num1);
Console.WriteLine("Second complex number: {0}",num2);
Console.WriteLine("The sum of the two numbers: {0}",sum);
if( sub.imaginarypart <0 )
{
Console.WriteLine("The sub of the two numbers: {0}{1}i",sub.realpart ,sub.imaginarypart );
}
else
Console.WriteLine("The sub of the two numbers: {0}",sub);
Console.ReadLine ();
}
}
}