complex类java解决
一个复数是一个形式为a+bi的数,这里的a和b都是实数,i是-1的平方根。数字a和b分别称为复数的实部和虚部。 可以使用下面的公式完成复数的加、减、乘、除:
(a+bi)+(c+di)=(a+c)+(b+d)i;
(a+bi)-(c+di)=(a-c)+(b-d)i;
(a+bi)*(c+di)=(ac-bd)+(ad+bc)i;
(a+bi)/(c+di)=(ac+bd)/(c^2+d^2)+ (bc-ad)/(c^2+d^2)i;
还可以使用下面的公式得到复数的绝对值: |a+bi|=sqrt(a^2+b^2);
设计一个名为Complex的复数类来表示复数以及完成复数运算的add、substract、multiply、divide和 calAbs(绝对值)方法,并且覆盖toString方法以返回一个表示复数的字符串。方法toString返回字符串 a+bi。如果b是0,那么它只返回a。
实现三个构造器:Complex(a, b)、Complex(a)和Complex()。Complex()创建数字0的Complex 对象,而Complex(a)创建一个b为0的Complex对象。 还提供getA()和getB()方法以返回复数的实部和虚部。
编写一个测试程序,提示用户输入两个复数,然后显示它们做加、减、乘、除之后的结果。
import java.util.Scanner;
class Complex {
private double realPart;
private double imaginPart;
Complex(double r, double i) {
this.realPart = r;
this.imaginPart = i;
}
Complex(double real){
this.realPart=real;
this.imaginPart=0;
}
Complex() {
this.realPart = 0;
this.imaginPart = 0;
}//3个构造方法//
public double getB() {
return this.imaginPart;
}//返回imagine的值//
public double getA() {
return this.realPart;
}//返回real的值//
public Complex complexAdd(Complex a) { //传进来的complex a和b类似于c中的指针
Complex c = new Complex();//定义一个c的complex类给c初始化
c.realPart = this.realPart + a.realPart;
c.imaginPart = this.imaginPart + a.imaginPart;
return c;//返回c这个子类的值//
}
public Complex complexSubtract(Complex a) {
Complex c = new Complex();
c.realPart = this.realPart - a.realPart;
c.imaginPart = this.imaginPart - a.imaginPart;
return c;
}//减法//
public Complex mutiply(Complex a) {
Complex c = new Complex();
c.realPart = this.realPart * a.realPart - this.imaginPart * a.imaginPart;
c.imaginPart = this.realPart * a.imaginPart + this.imaginPart * a.realPart;
return c;
}
public Complex divide(Complex a) {
Complex c = new Complex();
c.realPart = (a.realPart * this.realPart + a.imaginPart * this.imaginPart) / (a.realPart * a.realPart + a.imaginPart * a.imaginPart);
c.imaginPart = (a.realPart * this.imaginPart - this.realPart * a.imaginPart) / (a.realPart * a.realPart + a.imaginPart * a.imaginPart);
return c;
}
public double calAbs() {
double temp = Math.sqrt(this.realPart * this.realPart +this.imaginPart * this.imaginPart);
return temp;
}//求模//
public String toString() {
if (this.realPart >= 0 && this.realPart < 0.01) {
if (this.imaginPart >= 0 && this.imaginPart < 0.01)
return "0.00";
else
return String.format("%.2fi", this.imaginPart);
}
else {
if (this.imaginPart >= 0 && this.imaginPart < 0.01)
return String.format("%.2f", this.realPart);
else
if (this.imaginPart > 0)
return String.format("%.2f+%.2fi", this.realPart, this.imaginPart);
else
return String.format("%.2f%.2fi", this.realPart, this.imaginPart);
}
}
}
class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double real1=in.nextDouble();
double image1=in.nextDouble();
double real2=in.nextDouble();
double image2=in.nextDouble();
Complex a=new Complex(real1,image1);
Complex b=new Complex(real2,image2);
System.out.printf("Real:%.2f imaginary:%.2f Fabs:%.2f\n",a.getA(),a.getB(),a.calAbs());
System.out.printf("Real:%.2f imaginary:%.2f Fabs:%.2f\n",b.getA(),b.getB(),b.calAbs());
System.out.println(a.complexAdd(b));
System.out.println(a.complexSubtract(b));
System.out.println(a.mutiply(b));
System.out.println(a.divide(b));
}
}
前前后后花了2天时间 看了几十篇CSDN 问了大佬终于解决了 害 铁废物了
不足之处请大家指出改正!