110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComplexSp
{
class Complex
{
public int re;
public int im;
public Complex ()
{
re = 0;
im = 0;
}
public Complex (int num1)
{
re = num1;
im = 0;
}
public Complex (int num1,int num2)
{
re = num1;
im = num2;
}
public static Complex operator +(Complex obj1,Complex obj2)
{
return new Complex(obj1.re + obj2.re, obj1.im + obj2.im);
}
public static Complex operator -(Complex obj1,Complex obj2)
{
return new Complex(obj1.re - obj2.re, obj1.im - obj2.im);
}
static void Main(string[] args)
{
Complex comp1 = new Complex(1, 2);
Complex comp2 = new Complex(3, 4);
Complex comp3 = new Complex();
comp3 = comp1 + comp2;
Console.WriteLine ("{0}+{1}i",comp3 .re ,comp3.im);
}
}
}
public static bool operator ==(Complex obj1,Complex obj2)
{
if(obj1==null&&obj2==null)
return true;
return obj1!=null&&obj2!=null&&obj1.re == obj2.re && obj1.im == obj2.im;
}