JAVA编程题求答案

声明一个复数类文件complex,实现复数间的乘法和除法操作,乘法法则(a+bi)(c+di)=(ac—bd)+(ad+bc)d ,除法法则a+bi/c+di=

基本符合你的要求,代码如下:
class Complex
{
public Complex(double r, double i)
{
real = r;
imag = i;
}
public Complex mul(Complex c2)
{
Complex c3 = new Complex(0, 0);
c3.real = this.real * c2.real - this.imag * c2.imag;
c3.imag = this.real * c2.imag + this.imag * c2.real;
return c3;
}
public Complex dev(Complex c2)
{
Complex c4 = new Complex(0, 0);
c4.real = (this.real * c2.real + this.imag * c2.imag) / (c2.real * c2.real + c2.imag *

c2.imag);
c4.imag = (this.real * c2.imag - this.imag * c2.real) / (c2.real * c2.real + c2.imag *

c2.imag);
return c4;
}
public void show()
{
System.out.print("(" + real + "+" + imag + "i" + ")");
}
private double real;
private double imag;
}

public class Comp
{
public static void main(String []args)
{
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
Complex c3 = new Complex(0, 0);
Complex c4 = new Complex(0, 0);
c3 = c1.mul(c2);
c1.show();
System.out.print(" * ");
c2.show();
System.out.print(" = ");
c3.show();
System.out.println();
c4 = c1.dev(c2);
c1.show();
System.out.print(" / ");
c2.show();
System.out.print(" = ");
c4.show();
System.out.println();
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答