class Complex {
private double Realpart;
private double Imagepart;
Complex(double x, double y) {
this.Realpart = x;
this.Imagepart = y;
}
Complex() {
this.Realpart = 0;
this.Imagepart = 0;
}
public double GetReal(Complex c) {
return c.Realpart;
}
public double GetImage(Complex c) {
return c.Imagepart;
}
public Complex Add(Complex c2)// 加法运算
{
Complex sum = new Complex();
sum.Realpart = this.Realpart + c2.Realpart;
sum.Imagepart = this.Imagepart + c2.Imagepart;
return sum;
}
public Complex Sub(Complex c2)// 减法运算
{
Complex dif = new Complex();
dif.Realpart = this.Realpart - c2.Realpart;
dif.Imagepart = this.Imagepart - c2.Imagepart;
return dif;
}
public Complex Mul(Complex c2)// 乘法运算
{
Complex cheng = new Complex();
cheng.Realpart = this.Realpart * c2.Realpart - this.Imagepart
* c2.Imagepart;
cheng.Imagepart = this.Realpart * c2.Imagepart + this.Imagepart
* c2.Realpart;
return cheng;
}
public Complex Div(Complex c2)// 除法运算
{
Complex Null = new Complex();
Complex chu = new Complex();
chu.Realpart = (this.Realpart * c2.Realpart + this.Imagepart
* c2.Imagepart)
/ (c2.Realpart * c2.Realpart + c2.Imagepart * c2.Imagepart);
chu.Imagepart = (this.Imagepart * c2.Realpart - this.Realpart
* c2.Imagepart)
/ (c2.Realpart * c2.Realpart + c2.Imagepart * c2.Imagepart);
if ((c2.Realpart * c2.Realpart + c2.Imagepart * c2.Imagepart) == 0) {
System.out.println("the num doesnt exist");
return Null;
} else
return chu;
}
public void Show(Complex c)// 复数类型的表示方法
{
if (c.Realpart != 0 && c.Imagepart != 0)
System.out.printf("%.2f+%.2fi", c.Realpart, c.Imagepart);
else if (c.Realpart != 0 && c.Imagepart == 0)
System.out.printf("%.2f", c.Realpart);
else if (c.Realpart == 0 && c.Imagepart != 0)
System.out.printf("%.2fi", c.Imagepart);
else if (c.Realpart == 0 && c.Imagepart == 0)
System.out.printf("%d", 0);
}
public void helpTestShow(Complex C2) {
Show(this);
System.out.println();
Show(C2);
System.out.println();
System.out.printf("he:");
Show(this.Add(C2));
System.out.println();
System.out.printf("cha:");
Show(this.Sub(C2));
System.out.println();
System.out.printf("ji:");
Show(this.Mul(C2));
System.out.println();
System.out.printf("shang:");
Show(this.Div(C2));
System.out.println();
}
}
public class Complex_ {
public static void TestComplex()// 测试代码
{
Complex C1 = new Complex(520, 0);
Complex C2 = new Complex(1314, 0);
C1.helpTestShow(C2);
Complex C3 = new Complex(0.5, 0.7);
Complex C4 = new Complex(0.5, -0.7);
C3.helpTestShow(C4);
Complex C5 = new Complex(-8, 0);
Complex C6 = new Complex(0, 10);
C5.helpTestShow(C6);
Complex C7 = new Complex(0, 0);
Complex C8 = new Complex(0, -90);
C7.helpTestShow(C8);
Complex C9 = new Complex(2, 3);
Complex C10 = new Complex(4, -9);
C9.helpTestShow(C10);
}
public static void main(String[] args) {
TestComplex();
}
}
/*
测试数据:
520.00
1314.00
he:1834.00
cha:-794.00
ji:683280.00
shang:0.40
0.50+0.70i
0.50+-0.70i
he:1.00
cha:1.40i
ji:0.74
shang:-0.32+0.95i
-8.00
10.00i
he:-8.00+10.00i
cha:-8.00+-10.00i
ji:-80.00i
shang:0.80i
0
-90.00i
he:-90.00i
cha:90.00i
ji:0
shang:0
2.00+3.00i
4.00+-9.00i
he:6.00+-6.00i
cha:-2.00+12.00i
ji:35.00+-6.00i
shang:-0.20+0.31i
*/
【java面对对象】复数类型加减乘除的实现
最新推荐文章于 2022-11-25 09:29:39 发布