public class Complex
{
double real
double imaginary
public static final Complex ZERO = new 衫樱Complex (0, 0)
public static final Complex ONE = new Complex (1, 0)
public static final Complex I = new Complex (0, 1)
public Complex ( double real, double imaginary )
{
this.real = real
this.imaginary = imaginary
}
public double magnitude ()
{
return Math.sqrt (this.real * this.real + this.imaginary * this.imaginary)
}
public Complex negative ()
{
return new Complex (-real, -imaginary)
}
public double valueOf ()
{
return 或返丛this.real
}
public Complex add ( Complex a, Complex b )
{
return new Complex (a.real + b.real, 世歼a.imaginary + b.imaginary)
}
public Complex subtract ( Complex a, Complex b )
{
return new Complex (a.real - b.real, a.imaginary - b.imaginary)
}
public Complex multiply ( Complex a, Complex b )
{
return new Complex (a.real * b.real - a.imaginary * b.imaginary, a.real * b.imaginary + a.imaginary * b.real)
}
@Override
public String toString ()
{
StringBuilder builder = new StringBuilder ()
builder.append ("Complex [real=").append (real).append (", imaginary=").append (imaginary).append ("]")
return builder.toString ()
}
}
public class $ {public static void main(String... _) {
Complex a = new Complex(100, 10)
Complex b = new Complex(200, 5)
System.out.println(a)
System.out.println(b)
a.add(b)
System.out.println(a)
a.sub(b)
a.sub(b)
陆雹 System.out.println(a)
}
}
class Complex {
private double RealPart
private double ImaginaryRart
public Complex() {
}
public Complex(double realPart, double imaginaryRart) {
super()
RealPart = realPart
ImaginaryRart = imaginaryRart
}
public double getRealPart() {
return RealPart
}
public void setRealPart(double realPart) {
RealPart = realPart
}
public double getImaginaryRart() {
return ImaginaryRart
}
public void setImaginaryRart(double imaginaryRart) {
ImaginaryRart = imaginaryRart
}
public void add(Complex a) {
RealPart = RealPart + 铅纤a.RealPart
ImaginaryRart = ImaginaryRart + a.ImaginaryRart
}
public void sub(Complex a) {
RealPart = RealPart - a.RealPart
ImaginaryRart = ImaginaryRart - a.ImaginaryRart
}
public String toString() {
return "(" + RealPart + "," + ImaginaryRart + ")"
早激帆 }
}
public int compareTo(复数a){if(this.模-a.模>0){
return1
}else if(this.模-a.模<滑塌数0){
return -1
}else if(this.模-a.模=0){
return 0
}
}
其中复数得独立建立一个类,然衫芹后属性为两个int值,分别代表实部和虚信首部,然后求模很方便
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)