重载操作符+实现对复数形式的运算.复数形式为a+bi两个复数相加为实数部和虚数部分别相加如1+2i和3+4i运算
问题描述:
重载操作符+实现对复数形式的运算.复数形式为a+bi两个复数相加为实数部和虚数部分别相加如1+2i和3+4i运算
在线等------重载操作符+,实现对复数形式的运算.复数形式为a+bi,两个复数相加为实数部和虚数部分别相加,如1+2i和3+4i运算,结果为4+6i
答
class ClassMath
{
public int Real=0;
public int Imaginary=0;
public override stringToString()
{
return this.Real.ToString()+"+"+this.Imaginary.ToString()+"i";
}
public static ClassMath operator +(ClassMath a, ClassMath b)
{
ClassMath result = new ClassMath();
result.Imaginary = a.Imaginary + b.Imaginary;
result.Real = a.Real + b.Real;
return result;
}
}传入值是怎么传入的啊1+2i,3+4i,你只是写了+重载ClassMath c1 = new ClassMath();ClassMath c2 = new ClassMath();c1.Real = 1; c1.Imaginary = 2;c2.Real = 4; c2.Imaginary = 6;ClassMath c3 = c1 + c2;