class complex
{
double real,imag;
complex () {}
complex(double r,double i)
{ real=r;
imag=i;
}
complex addition(complex x,complex y)
{ this.real=x.real+y.real;
this.imag=x.imag+y.imag;
return(this) ;
}
complex subtraction(complex x,complex y)
{ this.real=x.real-y.real;
this.imag=x.imag-y.imag;
return(this) ;
}
complex multiplication(complex x,complex y)
{ this.real=(x.real)*(y.real)-(x.imag)*(y.imag);
this.imag=(x.real)*(y.imag)+(x.imag)*(y.real);
return(this) ;
}
complex division(complex x,complex y)
{ this.real=((x.real)*(y.real)+(x.imag)*(y.imag))/((y.real)*(y.real)+(y.imag)*(y.imag));
this.imag=((x.imag)*(y.real)-(x.real)*(y.imag))/((y.real)*(y.real)+(y.imag)*(y.imag));
return(this) ;
}
}
public class C
{
public static void main(String args[])
{
complex c1=new complex(2,4);
complex c2=new complex(4,8);
complex c=new complex();
c.addition(c1,c2);
System.out.println("c=c1(2,4)+c2(4,8)"+" "+c.real+","+c.imag);
c.subtraction(c1,c2);
System.out.println("c=c1(2,4)-c2(4,8)"+" "+c.real+","+c.imag);
c.multiplication(c1,c2);
System.out.println("c=c1(2,4)*c2(4,8)"+" "+c.real+","+c.imag);
c.division(c1,c2);
System.out.println("c=c1(2,4)/c2(4,8)"+" "+c.real+","+c.imag);
}
}
name C.java
运行结果:c=c1(2,4)+c2(4,8) 6.0 ,12.0
c=c1(2,4)-c2(4,8) -2.0 ,-4.0
c=c1(2,4)*c2(4,8) -24.0,32.0
c=c1(2,4)/c2(4,8) 0.5,0.0
求助:为什么我在函数中用getReal,getImag分别返回各函数的实部和虚部实现不了?